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)
186 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
189 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
191 void *bitmap = hisi_hba->slot_index_tags;
193 set_bit(slot_idx, bitmap);
196 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
199 void *bitmap = hisi_hba->slot_index_tags;
201 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
202 hisi_hba->last_slot_index + 1);
203 if (index >= hisi_hba->slot_index_count) {
204 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
206 if (index >= hisi_hba->slot_index_count)
207 return -SAS_QUEUE_FULL;
209 hisi_sas_slot_index_set(hisi_hba, index);
211 hisi_hba->last_slot_index = index;
216 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
220 for (i = 0; i < hisi_hba->slot_index_count; ++i)
221 hisi_sas_slot_index_clear(hisi_hba, i);
224 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
225 struct hisi_sas_slot *slot)
227 struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
231 struct device *dev = hisi_hba->dev;
233 if (!task->lldd_task)
236 task->lldd_task = NULL;
238 if (!sas_protocol_ata(task->task_proto))
240 dma_unmap_sg(dev, task->scatter,
246 spin_lock_irqsave(&dq->lock, flags);
247 list_del_init(&slot->entry);
248 spin_unlock_irqrestore(&dq->lock, flags);
250 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
252 spin_lock_irqsave(&hisi_hba->lock, flags);
253 hisi_sas_slot_index_free(hisi_hba, slot->idx);
254 spin_unlock_irqrestore(&hisi_hba->lock, flags);
256 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
258 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
259 struct hisi_sas_slot *slot)
261 hisi_hba->hw->prep_smp(hisi_hba, slot);
264 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
265 struct hisi_sas_slot *slot)
267 hisi_hba->hw->prep_ssp(hisi_hba, slot);
270 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
271 struct hisi_sas_slot *slot)
273 hisi_hba->hw->prep_stp(hisi_hba, slot);
276 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
277 struct hisi_sas_slot *slot,
278 int device_id, int abort_flag, int tag_to_abort)
280 hisi_hba->hw->prep_abort(hisi_hba, slot,
281 device_id, abort_flag, tag_to_abort);
284 static int hisi_sas_task_prep(struct sas_task *task,
285 struct hisi_sas_dq **dq_pointer,
286 bool is_tmf, struct hisi_sas_tmf_task *tmf,
289 struct domain_device *device = task->dev;
290 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
291 struct hisi_sas_device *sas_dev = device->lldd_dev;
292 struct hisi_sas_port *port;
293 struct hisi_sas_slot *slot;
294 struct hisi_sas_cmd_hdr *cmd_hdr_base;
295 struct asd_sas_port *sas_port = device->port;
296 struct device *dev = hisi_hba->dev;
297 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
298 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
299 struct hisi_sas_dq *dq;
304 struct task_status_struct *ts = &task->task_status;
306 ts->resp = SAS_TASK_UNDELIVERED;
307 ts->stat = SAS_PHY_DOWN;
309 * libsas will use dev->port, should
310 * not call task_done for sata
312 if (device->dev_type != SAS_SATA_DEV)
313 task->task_done(task);
317 if (DEV_IS_GONE(sas_dev)) {
319 dev_info(dev, "task prep: device %d not ready\n",
322 dev_info(dev, "task prep: device %016llx not ready\n",
323 SAS_ADDR(device->sas_addr));
328 *dq_pointer = dq = sas_dev->dq;
330 port = to_hisi_sas_port(sas_port);
331 if (port && !port->port_attached) {
332 dev_info(dev, "task prep: %s port%d not attach device\n",
333 (dev_is_sata(device)) ?
340 if (!sas_protocol_ata(task->task_proto)) {
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;
375 n_elem = task->num_scatter;
377 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
378 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
381 goto err_out_dma_unmap;
384 spin_lock_irqsave(&hisi_hba->lock, flags);
385 if (hisi_hba->hw->slot_index_alloc)
386 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
389 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
390 spin_unlock_irqrestore(&hisi_hba->lock, flags);
392 goto err_out_dma_unmap;
394 slot = &hisi_hba->slot_info[slot_idx];
396 spin_lock_irqsave(&dq->lock, flags);
397 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
398 if (wr_q_index < 0) {
399 spin_unlock_irqrestore(&dq->lock, flags);
404 list_add_tail(&slot->delivery, &dq->list);
405 list_add_tail(&slot->entry, &sas_dev->list);
406 spin_unlock_irqrestore(&dq->lock, flags);
408 dlvry_queue = dq->id;
409 dlvry_queue_slot = wr_q_index;
411 slot->n_elem = n_elem;
412 slot->dlvry_queue = dlvry_queue;
413 slot->dlvry_queue_slot = dlvry_queue_slot;
414 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
415 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
419 slot->is_internal = is_tmf;
420 task->lldd_task = slot;
422 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
423 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
424 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
426 switch (task->task_proto) {
427 case SAS_PROTOCOL_SMP:
428 hisi_sas_task_prep_smp(hisi_hba, slot);
430 case SAS_PROTOCOL_SSP:
431 hisi_sas_task_prep_ssp(hisi_hba, slot);
433 case SAS_PROTOCOL_SATA:
434 case SAS_PROTOCOL_STP:
435 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
436 hisi_sas_task_prep_ata(hisi_hba, slot);
439 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
444 spin_lock_irqsave(&task->task_state_lock, flags);
445 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
446 spin_unlock_irqrestore(&task->task_state_lock, flags);
449 WRITE_ONCE(slot->ready, 1);
454 spin_lock_irqsave(&hisi_hba->lock, flags);
455 hisi_sas_slot_index_free(hisi_hba, slot_idx);
456 spin_unlock_irqrestore(&hisi_hba->lock, flags);
458 if (!sas_protocol_ata(task->task_proto)) {
459 if (task->num_scatter) {
460 dma_unmap_sg(dev, task->scatter, task->num_scatter,
462 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
464 dma_unmap_sg(dev, &task->smp_task.smp_req,
467 dma_unmap_sg(dev, &task->smp_task.smp_resp,
472 dev_err(dev, "task prep: failed[%d]!\n", rc);
476 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
477 bool is_tmf, struct hisi_sas_tmf_task *tmf)
482 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
483 struct device *dev = hisi_hba->dev;
484 struct hisi_sas_dq *dq = NULL;
486 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
490 down(&hisi_hba->sem);
494 /* protect task_prep and start_delivery sequence */
495 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
497 dev_err(dev, "task exec: failed[%d]!\n", rc);
500 spin_lock_irqsave(&dq->lock, flags);
501 hisi_hba->hw->start_delivery(dq);
502 spin_unlock_irqrestore(&dq->lock, flags);
508 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
510 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
511 struct asd_sas_phy *sas_phy = &phy->sas_phy;
512 struct sas_ha_struct *sas_ha;
514 if (!phy->phy_attached)
517 sas_ha = &hisi_hba->sha;
518 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
521 struct sas_phy *sphy = sas_phy->phy;
523 sphy->negotiated_linkrate = sas_phy->linkrate;
524 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
525 sphy->maximum_linkrate_hw =
526 hisi_hba->hw->phy_get_max_linkrate();
527 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
528 sphy->minimum_linkrate = phy->minimum_linkrate;
530 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
531 sphy->maximum_linkrate = phy->maximum_linkrate;
534 if (phy->phy_type & PORT_TYPE_SAS) {
535 struct sas_identify_frame *id;
537 id = (struct sas_identify_frame *)phy->frame_rcvd;
538 id->dev_type = phy->identify.device_type;
539 id->initiator_bits = SAS_PROTOCOL_ALL;
540 id->target_bits = phy->identify.target_port_protocols;
541 } else if (phy->phy_type & PORT_TYPE_SATA) {
545 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
546 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
549 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
551 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
552 struct hisi_sas_device *sas_dev = NULL;
554 int last = hisi_hba->last_dev_id;
555 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
558 spin_lock_irqsave(&hisi_hba->lock, flags);
559 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
560 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
561 int queue = i % hisi_hba->queue_count;
562 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
564 hisi_hba->devices[i].device_id = i;
565 sas_dev = &hisi_hba->devices[i];
566 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
567 sas_dev->dev_type = device->dev_type;
568 sas_dev->hisi_hba = hisi_hba;
569 sas_dev->sas_device = device;
571 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
576 hisi_hba->last_dev_id = i;
577 spin_unlock_irqrestore(&hisi_hba->lock, flags);
582 #define HISI_SAS_SRST_ATA_DISK_CNT 3
583 static int hisi_sas_init_device(struct domain_device *device)
585 int rc = TMF_RESP_FUNC_COMPLETE;
587 struct hisi_sas_tmf_task tmf_task;
588 int retry = HISI_SAS_SRST_ATA_DISK_CNT;
589 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
591 switch (device->dev_type) {
593 int_to_scsilun(0, &lun);
595 tmf_task.tmf = TMF_CLEAR_TASK_SET;
596 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
598 if (rc == TMF_RESP_FUNC_COMPLETE)
599 hisi_sas_release_task(hisi_hba, device);
603 case SAS_SATA_PM_PORT:
604 case SAS_SATA_PENDING:
605 while (retry-- > 0) {
606 rc = hisi_sas_softreset_ata_disk(device);
618 static int hisi_sas_dev_found(struct domain_device *device)
620 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
621 struct domain_device *parent_dev = device->parent;
622 struct hisi_sas_device *sas_dev;
623 struct device *dev = hisi_hba->dev;
626 if (hisi_hba->hw->alloc_dev)
627 sas_dev = hisi_hba->hw->alloc_dev(device);
629 sas_dev = hisi_sas_alloc_dev(device);
631 dev_err(dev, "fail alloc dev: max support %d devices\n",
632 HISI_SAS_MAX_DEVICES);
636 device->lldd_dev = sas_dev;
637 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
639 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
641 u8 phy_num = parent_dev->ex_dev.num_phys;
644 for (phy_no = 0; phy_no < phy_num; phy_no++) {
645 phy = &parent_dev->ex_dev.ex_phy[phy_no];
646 if (SAS_ADDR(phy->attached_sas_addr) ==
647 SAS_ADDR(device->sas_addr))
651 if (phy_no == phy_num) {
652 dev_info(dev, "dev found: no attached "
653 "dev:%016llx at ex:%016llx\n",
654 SAS_ADDR(device->sas_addr),
655 SAS_ADDR(parent_dev->sas_addr));
661 dev_info(dev, "dev[%d:%x] found\n",
662 sas_dev->device_id, sas_dev->dev_type);
664 rc = hisi_sas_init_device(device);
670 hisi_sas_dev_gone(device);
674 int hisi_sas_slave_configure(struct scsi_device *sdev)
676 struct domain_device *dev = sdev_to_domain_dev(sdev);
677 int ret = sas_slave_configure(sdev);
681 if (!dev_is_sata(dev))
682 sas_change_queue_depth(sdev, 64);
686 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
688 void hisi_sas_scan_start(struct Scsi_Host *shost)
690 struct hisi_hba *hisi_hba = shost_priv(shost);
692 hisi_hba->hw->phys_init(hisi_hba);
694 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
696 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
698 struct hisi_hba *hisi_hba = shost_priv(shost);
699 struct sas_ha_struct *sha = &hisi_hba->sha;
701 /* Wait for PHY up interrupt to occur */
708 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
710 static void hisi_sas_phyup_work(struct work_struct *work)
712 struct hisi_sas_phy *phy =
713 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
714 struct hisi_hba *hisi_hba = phy->hisi_hba;
715 struct asd_sas_phy *sas_phy = &phy->sas_phy;
716 int phy_no = sas_phy->id;
718 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
719 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
722 static void hisi_sas_linkreset_work(struct work_struct *work)
724 struct hisi_sas_phy *phy =
725 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
726 struct asd_sas_phy *sas_phy = &phy->sas_phy;
728 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
731 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
732 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
733 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
736 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
737 enum hisi_sas_phy_event event)
739 struct hisi_hba *hisi_hba = phy->hisi_hba;
741 if (WARN_ON(event >= HISI_PHYES_NUM))
744 return queue_work(hisi_hba->wq, &phy->works[event]);
746 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
748 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
750 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
751 struct asd_sas_phy *sas_phy = &phy->sas_phy;
754 phy->hisi_hba = hisi_hba;
756 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
757 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
758 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
759 sas_phy->class = SAS;
760 sas_phy->iproto = SAS_PROTOCOL_ALL;
762 sas_phy->type = PHY_TYPE_PHYSICAL;
763 sas_phy->role = PHY_ROLE_INITIATOR;
764 sas_phy->oob_mode = OOB_NOT_CONNECTED;
765 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
766 sas_phy->id = phy_no;
767 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
768 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
769 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
770 sas_phy->lldd_phy = phy;
772 for (i = 0; i < HISI_PHYES_NUM; i++)
773 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
775 spin_lock_init(&phy->lock);
778 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
780 struct sas_ha_struct *sas_ha = sas_phy->ha;
781 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
782 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
783 struct asd_sas_port *sas_port = sas_phy->port;
784 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
790 spin_lock_irqsave(&hisi_hba->lock, flags);
791 port->port_attached = 1;
792 port->id = phy->port_id;
794 sas_port->lldd_port = port;
795 spin_unlock_irqrestore(&hisi_hba->lock, flags);
798 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
799 struct hisi_sas_slot *slot)
803 struct task_status_struct *ts;
805 ts = &task->task_status;
807 ts->resp = SAS_TASK_COMPLETE;
808 ts->stat = SAS_ABORTED_TASK;
809 spin_lock_irqsave(&task->task_state_lock, flags);
810 task->task_state_flags &=
811 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
812 task->task_state_flags |= SAS_TASK_STATE_DONE;
813 spin_unlock_irqrestore(&task->task_state_lock, flags);
816 hisi_sas_slot_task_free(hisi_hba, task, slot);
819 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
820 struct domain_device *device)
822 struct hisi_sas_slot *slot, *slot2;
823 struct hisi_sas_device *sas_dev = device->lldd_dev;
825 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
826 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
829 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
831 struct hisi_sas_device *sas_dev;
832 struct domain_device *device;
835 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
836 sas_dev = &hisi_hba->devices[i];
837 device = sas_dev->sas_device;
839 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
843 hisi_sas_release_task(hisi_hba, device);
846 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
848 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
849 struct domain_device *device)
851 if (hisi_hba->hw->dereg_device)
852 hisi_hba->hw->dereg_device(hisi_hba, device);
855 static void hisi_sas_dev_gone(struct domain_device *device)
857 struct hisi_sas_device *sas_dev = device->lldd_dev;
858 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
859 struct device *dev = hisi_hba->dev;
861 dev_info(dev, "dev[%d:%x] is gone\n",
862 sas_dev->device_id, sas_dev->dev_type);
864 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
865 hisi_sas_internal_task_abort(hisi_hba, device,
866 HISI_SAS_INT_ABT_DEV, 0);
868 hisi_sas_dereg_device(hisi_hba, device);
870 down(&hisi_hba->sem);
871 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
873 device->lldd_dev = NULL;
876 if (hisi_hba->hw->free_device)
877 hisi_hba->hw->free_device(sas_dev);
878 sas_dev->dev_type = SAS_PHY_UNUSED;
881 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
883 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
886 static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
887 struct sas_phy_linkrates *r)
889 struct sas_phy_linkrates _r;
891 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
892 struct asd_sas_phy *sas_phy = &phy->sas_phy;
893 enum sas_linkrate min, max;
895 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
896 max = sas_phy->phy->maximum_linkrate;
897 min = r->minimum_linkrate;
898 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
899 max = r->maximum_linkrate;
900 min = sas_phy->phy->minimum_linkrate;
904 _r.maximum_linkrate = max;
905 _r.minimum_linkrate = min;
907 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
909 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
910 hisi_hba->hw->phy_start(hisi_hba, phy_no);
913 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
916 struct sas_ha_struct *sas_ha = sas_phy->ha;
917 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
918 int phy_no = sas_phy->id;
921 case PHY_FUNC_HARD_RESET:
922 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
925 case PHY_FUNC_LINK_RESET:
926 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
928 hisi_hba->hw->phy_start(hisi_hba, phy_no);
931 case PHY_FUNC_DISABLE:
932 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
935 case PHY_FUNC_SET_LINK_RATE:
936 hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
938 case PHY_FUNC_GET_EVENTS:
939 if (hisi_hba->hw->get_events) {
940 hisi_hba->hw->get_events(hisi_hba, phy_no);
944 case PHY_FUNC_RELEASE_SPINUP_HOLD:
951 static void hisi_sas_task_done(struct sas_task *task)
953 if (!del_timer(&task->slow_task->timer))
955 complete(&task->slow_task->completion);
958 static void hisi_sas_tmf_timedout(struct timer_list *t)
960 struct sas_task_slow *slow = from_timer(slow, t, timer);
961 struct sas_task *task = slow->task;
964 spin_lock_irqsave(&task->task_state_lock, flags);
965 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
966 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
967 spin_unlock_irqrestore(&task->task_state_lock, flags);
969 complete(&task->slow_task->completion);
972 #define TASK_TIMEOUT 20
974 #define INTERNAL_ABORT_TIMEOUT 6
975 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
976 void *parameter, u32 para_len,
977 struct hisi_sas_tmf_task *tmf)
979 struct hisi_sas_device *sas_dev = device->lldd_dev;
980 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
981 struct device *dev = hisi_hba->dev;
982 struct sas_task *task;
985 for (retry = 0; retry < TASK_RETRY; retry++) {
986 task = sas_alloc_slow_task(GFP_KERNEL);
991 task->task_proto = device->tproto;
993 if (dev_is_sata(device)) {
994 task->ata_task.device_control_reg_update = 1;
995 memcpy(&task->ata_task.fis, parameter, para_len);
997 memcpy(&task->ssp_task, parameter, para_len);
999 task->task_done = hisi_sas_task_done;
1001 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1002 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1003 add_timer(&task->slow_task->timer);
1005 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1008 del_timer(&task->slow_task->timer);
1009 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1014 wait_for_completion(&task->slow_task->completion);
1015 res = TMF_RESP_FUNC_FAILED;
1016 /* Even TMF timed out, return direct. */
1017 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1018 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1019 struct hisi_sas_slot *slot = task->lldd_task;
1021 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1027 dev_err(dev, "abort tmf: TMF task timeout\n");
1030 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1031 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1032 res = TMF_RESP_FUNC_COMPLETE;
1036 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1037 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1038 res = TMF_RESP_FUNC_SUCC;
1042 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1043 task->task_status.stat == SAS_DATA_UNDERRUN) {
1044 /* no error, but return the number of bytes of
1047 dev_warn(dev, "abort tmf: task to dev %016llx "
1048 "resp: 0x%x sts 0x%x underrun\n",
1049 SAS_ADDR(device->sas_addr),
1050 task->task_status.resp,
1051 task->task_status.stat);
1052 res = task->task_status.residual;
1056 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1057 task->task_status.stat == SAS_DATA_OVERRUN) {
1058 dev_warn(dev, "abort tmf: blocked task error\n");
1063 dev_warn(dev, "abort tmf: task to dev "
1064 "%016llx resp: 0x%x status 0x%x\n",
1065 SAS_ADDR(device->sas_addr), task->task_status.resp,
1066 task->task_status.stat);
1067 sas_free_task(task);
1071 if (retry == TASK_RETRY)
1072 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1073 sas_free_task(task);
1077 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1078 bool reset, int pmp, u8 *fis)
1080 struct ata_taskfile tf;
1082 ata_tf_init(dev, &tf);
1086 tf.ctl &= ~ATA_SRST;
1087 tf.command = ATA_CMD_DEV_RESET;
1088 ata_tf_to_fis(&tf, pmp, 0, fis);
1091 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1094 struct ata_port *ap = device->sata_dev.ap;
1095 struct ata_link *link;
1096 int rc = TMF_RESP_FUNC_FAILED;
1097 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1098 struct device *dev = hisi_hba->dev;
1099 int s = sizeof(struct host_to_dev_fis);
1101 ata_for_each_link(link, ap, EDGE) {
1102 int pmp = sata_srst_pmp(link);
1104 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1105 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1106 if (rc != TMF_RESP_FUNC_COMPLETE)
1110 if (rc == TMF_RESP_FUNC_COMPLETE) {
1111 ata_for_each_link(link, ap, EDGE) {
1112 int pmp = sata_srst_pmp(link);
1114 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1115 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1117 if (rc != TMF_RESP_FUNC_COMPLETE)
1118 dev_err(dev, "ata disk de-reset failed\n");
1121 dev_err(dev, "ata disk reset failed\n");
1124 if (rc == TMF_RESP_FUNC_COMPLETE)
1125 hisi_sas_release_task(hisi_hba, device);
1130 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1131 u8 *lun, struct hisi_sas_tmf_task *tmf)
1133 struct sas_ssp_task ssp_task;
1135 if (!(device->tproto & SAS_PROTOCOL_SSP))
1136 return TMF_RESP_FUNC_ESUPP;
1138 memcpy(ssp_task.LUN, lun, 8);
1140 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1141 sizeof(ssp_task), tmf);
1144 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1146 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1149 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1150 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1151 struct domain_device *device = sas_dev->sas_device;
1152 struct asd_sas_port *sas_port;
1153 struct hisi_sas_port *port;
1154 struct hisi_sas_phy *phy = NULL;
1155 struct asd_sas_phy *sas_phy;
1157 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1158 || !device || !device->port)
1161 sas_port = device->port;
1162 port = to_hisi_sas_port(sas_port);
1164 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1165 if (state & BIT(sas_phy->id)) {
1166 phy = sas_phy->lldd_phy;
1171 port->id = phy->port_id;
1173 /* Update linkrate of directly attached device. */
1174 if (!device->parent)
1175 device->linkrate = phy->sas_phy.linkrate;
1177 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1183 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1186 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1187 struct asd_sas_port *_sas_port = NULL;
1190 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1191 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1192 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1193 struct asd_sas_port *sas_port = sas_phy->port;
1194 bool do_port_check = !!(_sas_port != sas_port);
1196 if (!sas_phy->phy->enabled)
1199 /* Report PHY state change to libsas */
1200 if (state & BIT(phy_no)) {
1201 if (do_port_check && sas_port && sas_port->port_dev) {
1202 struct domain_device *dev = sas_port->port_dev;
1204 _sas_port = sas_port;
1206 if (DEV_IS_EXPANDER(dev->dev_type))
1207 sas_ha->notify_port_event(sas_phy,
1208 PORTE_BROADCAST_RCVD);
1210 } else if (old_state & (1 << phy_no))
1211 /* PHY down but was up before */
1212 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1217 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1219 struct hisi_sas_device *sas_dev;
1220 struct domain_device *device;
1223 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1224 sas_dev = &hisi_hba->devices[i];
1225 device = sas_dev->sas_device;
1227 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1230 hisi_sas_init_device(device);
1234 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1235 struct asd_sas_port *sas_port,
1236 struct domain_device *device)
1238 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1239 struct ata_port *ap = device->sata_dev.ap;
1240 struct device *dev = hisi_hba->dev;
1241 int s = sizeof(struct host_to_dev_fis);
1242 int rc = TMF_RESP_FUNC_FAILED;
1243 struct asd_sas_phy *sas_phy;
1244 struct ata_link *link;
1248 state = hisi_hba->hw->get_phys_state(hisi_hba);
1249 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1250 if (!(state & BIT(sas_phy->id)))
1253 ata_for_each_link(link, ap, EDGE) {
1254 int pmp = sata_srst_pmp(link);
1256 tmf_task.phy_id = sas_phy->id;
1257 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1258 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1260 if (rc != TMF_RESP_FUNC_COMPLETE) {
1261 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1269 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1271 struct device *dev = hisi_hba->dev;
1274 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1275 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1276 struct domain_device *device = sas_dev->sas_device;
1278 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1281 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1282 HISI_SAS_INT_ABT_DEV, 0);
1284 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1287 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1288 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1289 struct asd_sas_port *sas_port = &port->sas_port;
1290 struct domain_device *port_dev = sas_port->port_dev;
1291 struct domain_device *device;
1293 if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
1296 /* Try to find a SATA device */
1297 list_for_each_entry(device, &sas_port->dev_list,
1299 if (dev_is_sata(device)) {
1300 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1309 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1311 struct Scsi_Host *shost = hisi_hba->shost;
1313 down(&hisi_hba->sem);
1314 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1316 scsi_block_requests(shost);
1317 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1319 if (timer_pending(&hisi_hba->timer))
1320 del_timer_sync(&hisi_hba->timer);
1322 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1324 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1326 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1328 struct Scsi_Host *shost = hisi_hba->shost;
1331 /* Init and wait for PHYs to come up and all libsas event finished. */
1332 hisi_hba->hw->phys_init(hisi_hba);
1334 hisi_sas_refresh_port_id(hisi_hba);
1335 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1338 if (hisi_hba->reject_stp_links_msk)
1339 hisi_sas_terminate_stp_reject(hisi_hba);
1340 hisi_sas_reset_init_all_devices(hisi_hba);
1341 scsi_unblock_requests(shost);
1342 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1344 state = hisi_hba->hw->get_phys_state(hisi_hba);
1345 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state, state);
1347 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1349 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1351 struct device *dev = hisi_hba->dev;
1352 struct Scsi_Host *shost = hisi_hba->shost;
1355 if (!hisi_hba->hw->soft_reset)
1358 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1361 dev_info(dev, "controller resetting...\n");
1362 hisi_sas_controller_reset_prepare(hisi_hba);
1364 rc = hisi_hba->hw->soft_reset(hisi_hba);
1366 dev_warn(dev, "controller reset failed (%d)\n", rc);
1367 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1369 scsi_unblock_requests(shost);
1370 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1374 hisi_sas_controller_reset_done(hisi_hba);
1375 dev_info(dev, "controller reset complete\n");
1380 static int hisi_sas_abort_task(struct sas_task *task)
1382 struct scsi_lun lun;
1383 struct hisi_sas_tmf_task tmf_task;
1384 struct domain_device *device = task->dev;
1385 struct hisi_sas_device *sas_dev = device->lldd_dev;
1386 struct hisi_hba *hisi_hba;
1388 int rc = TMF_RESP_FUNC_FAILED;
1389 unsigned long flags;
1392 return TMF_RESP_FUNC_FAILED;
1394 hisi_hba = dev_to_hisi_hba(task->dev);
1395 dev = hisi_hba->dev;
1397 spin_lock_irqsave(&task->task_state_lock, flags);
1398 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1399 spin_unlock_irqrestore(&task->task_state_lock, flags);
1400 rc = TMF_RESP_FUNC_COMPLETE;
1403 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1404 spin_unlock_irqrestore(&task->task_state_lock, flags);
1406 sas_dev->dev_status = HISI_SAS_DEV_EH;
1407 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1408 struct scsi_cmnd *cmnd = task->uldd_task;
1409 struct hisi_sas_slot *slot = task->lldd_task;
1410 u32 tag = slot->idx;
1413 int_to_scsilun(cmnd->device->lun, &lun);
1414 tmf_task.tmf = TMF_ABORT_TASK;
1415 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1417 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1420 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1421 HISI_SAS_INT_ABT_CMD, tag);
1423 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1424 return TMF_RESP_FUNC_FAILED;
1428 * If the TMF finds that the IO is not in the device and also
1429 * the internal abort does not succeed, then it is safe to
1431 * Note: if the internal abort succeeds then the slot
1432 * will have already been completed
1434 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1435 if (task->lldd_task)
1436 hisi_sas_do_release_task(hisi_hba, task, slot);
1438 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1439 task->task_proto & SAS_PROTOCOL_STP) {
1440 if (task->dev->dev_type == SAS_SATA_DEV) {
1441 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1442 HISI_SAS_INT_ABT_DEV, 0);
1444 dev_err(dev, "abort task: internal abort failed\n");
1447 hisi_sas_dereg_device(hisi_hba, device);
1448 rc = hisi_sas_softreset_ata_disk(device);
1450 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1452 struct hisi_sas_slot *slot = task->lldd_task;
1453 u32 tag = slot->idx;
1455 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1456 HISI_SAS_INT_ABT_CMD, tag);
1457 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1459 hisi_sas_do_release_task(hisi_hba, task, slot);
1463 if (rc != TMF_RESP_FUNC_COMPLETE)
1464 dev_notice(dev, "abort task: rc=%d\n", rc);
1468 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1470 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1471 struct device *dev = hisi_hba->dev;
1472 struct hisi_sas_tmf_task tmf_task;
1473 int rc = TMF_RESP_FUNC_FAILED;
1475 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1476 HISI_SAS_INT_ABT_DEV, 0);
1478 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1479 return TMF_RESP_FUNC_FAILED;
1481 hisi_sas_dereg_device(hisi_hba, device);
1483 tmf_task.tmf = TMF_ABORT_TASK_SET;
1484 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1486 if (rc == TMF_RESP_FUNC_COMPLETE)
1487 hisi_sas_release_task(hisi_hba, device);
1492 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1494 int rc = TMF_RESP_FUNC_FAILED;
1495 struct hisi_sas_tmf_task tmf_task;
1497 tmf_task.tmf = TMF_CLEAR_ACA;
1498 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1503 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1505 struct sas_phy *local_phy = sas_get_local_phy(device);
1506 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1507 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1508 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1509 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1510 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
1511 struct hisi_sas_phy *phy = container_of(sas_phy,
1512 struct hisi_sas_phy, sas_phy);
1513 DECLARE_COMPLETION_ONSTACK(phyreset);
1515 if (scsi_is_sas_phy_local(local_phy)) {
1517 phy->reset_completion = &phyreset;
1520 rc = sas_phy_reset(local_phy, reset_type);
1521 sas_put_local_phy(local_phy);
1523 if (scsi_is_sas_phy_local(local_phy)) {
1524 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1525 unsigned long flags;
1527 spin_lock_irqsave(&phy->lock, flags);
1528 phy->reset_completion = NULL;
1530 spin_unlock_irqrestore(&phy->lock, flags);
1532 /* report PHY down if timed out */
1534 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1541 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1543 struct hisi_sas_device *sas_dev = device->lldd_dev;
1544 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1545 struct device *dev = hisi_hba->dev;
1546 int rc = TMF_RESP_FUNC_FAILED;
1548 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1549 return TMF_RESP_FUNC_FAILED;
1550 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1552 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1553 HISI_SAS_INT_ABT_DEV, 0);
1555 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1556 return TMF_RESP_FUNC_FAILED;
1558 hisi_sas_dereg_device(hisi_hba, device);
1560 rc = hisi_sas_debug_I_T_nexus_reset(device);
1562 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1563 hisi_sas_release_task(hisi_hba, device);
1568 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1570 struct hisi_sas_device *sas_dev = device->lldd_dev;
1571 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1572 struct device *dev = hisi_hba->dev;
1573 int rc = TMF_RESP_FUNC_FAILED;
1575 sas_dev->dev_status = HISI_SAS_DEV_EH;
1576 if (dev_is_sata(device)) {
1577 struct sas_phy *phy;
1579 /* Clear internal IO and then hardreset */
1580 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1581 HISI_SAS_INT_ABT_DEV, 0);
1583 dev_err(dev, "lu_reset: internal abort failed\n");
1586 hisi_sas_dereg_device(hisi_hba, device);
1588 phy = sas_get_local_phy(device);
1590 rc = sas_phy_reset(phy, 1);
1593 hisi_sas_release_task(hisi_hba, device);
1594 sas_put_local_phy(phy);
1596 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1598 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1599 HISI_SAS_INT_ABT_DEV, 0);
1601 dev_err(dev, "lu_reset: internal abort failed\n");
1604 hisi_sas_dereg_device(hisi_hba, device);
1606 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1607 if (rc == TMF_RESP_FUNC_COMPLETE)
1608 hisi_sas_release_task(hisi_hba, device);
1611 if (rc != TMF_RESP_FUNC_COMPLETE)
1612 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1613 sas_dev->device_id, rc);
1617 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1619 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1620 struct device *dev = hisi_hba->dev;
1621 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1624 queue_work(hisi_hba->wq, &r.work);
1625 wait_for_completion(r.completion);
1627 return TMF_RESP_FUNC_FAILED;
1629 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1630 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1631 struct domain_device *device = sas_dev->sas_device;
1633 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1634 DEV_IS_EXPANDER(device->dev_type))
1637 rc = hisi_sas_debug_I_T_nexus_reset(device);
1638 if (rc != TMF_RESP_FUNC_COMPLETE)
1639 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1640 sas_dev->device_id, rc);
1643 hisi_sas_release_tasks(hisi_hba);
1645 return TMF_RESP_FUNC_COMPLETE;
1648 static int hisi_sas_query_task(struct sas_task *task)
1650 struct scsi_lun lun;
1651 struct hisi_sas_tmf_task tmf_task;
1652 int rc = TMF_RESP_FUNC_FAILED;
1654 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1655 struct scsi_cmnd *cmnd = task->uldd_task;
1656 struct domain_device *device = task->dev;
1657 struct hisi_sas_slot *slot = task->lldd_task;
1658 u32 tag = slot->idx;
1660 int_to_scsilun(cmnd->device->lun, &lun);
1661 tmf_task.tmf = TMF_QUERY_TASK;
1662 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1664 rc = hisi_sas_debug_issue_ssp_tmf(device,
1668 /* The task is still in Lun, release it then */
1669 case TMF_RESP_FUNC_SUCC:
1670 /* The task is not in Lun or failed, reset the phy */
1671 case TMF_RESP_FUNC_FAILED:
1672 case TMF_RESP_FUNC_COMPLETE:
1675 rc = TMF_RESP_FUNC_FAILED;
1683 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1684 struct sas_task *task, int abort_flag,
1687 struct domain_device *device = task->dev;
1688 struct hisi_sas_device *sas_dev = device->lldd_dev;
1689 struct device *dev = hisi_hba->dev;
1690 struct hisi_sas_port *port;
1691 struct hisi_sas_slot *slot;
1692 struct asd_sas_port *sas_port = device->port;
1693 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1694 struct hisi_sas_dq *dq = sas_dev->dq;
1695 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1696 unsigned long flags, flags_dq = 0;
1699 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1705 port = to_hisi_sas_port(sas_port);
1707 /* simply get a slot and send abort command */
1708 spin_lock_irqsave(&hisi_hba->lock, flags);
1709 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1711 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1714 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1716 slot = &hisi_hba->slot_info[slot_idx];
1718 spin_lock_irqsave(&dq->lock, flags_dq);
1719 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1720 if (wr_q_index < 0) {
1721 spin_unlock_irqrestore(&dq->lock, flags_dq);
1725 list_add_tail(&slot->delivery, &dq->list);
1726 spin_unlock_irqrestore(&dq->lock, flags_dq);
1728 dlvry_queue = dq->id;
1729 dlvry_queue_slot = wr_q_index;
1731 slot->n_elem = n_elem;
1732 slot->dlvry_queue = dlvry_queue;
1733 slot->dlvry_queue_slot = dlvry_queue_slot;
1734 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1735 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1738 slot->is_internal = true;
1739 task->lldd_task = slot;
1741 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1742 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1743 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1745 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1746 abort_flag, task_tag);
1748 spin_lock_irqsave(&task->task_state_lock, flags);
1749 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1750 spin_unlock_irqrestore(&task->task_state_lock, flags);
1752 WRITE_ONCE(slot->ready, 1);
1753 /* send abort command to the chip */
1754 spin_lock_irqsave(&dq->lock, flags);
1755 list_add_tail(&slot->entry, &sas_dev->list);
1756 hisi_hba->hw->start_delivery(dq);
1757 spin_unlock_irqrestore(&dq->lock, flags);
1762 spin_lock_irqsave(&hisi_hba->lock, flags);
1763 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1764 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1766 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1772 * hisi_sas_internal_task_abort -- execute an internal
1773 * abort command for single IO command or a device
1774 * @hisi_hba: host controller struct
1775 * @device: domain device
1776 * @abort_flag: mode of operation, device or single IO
1777 * @tag: tag of IO to be aborted (only relevant to single
1781 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1782 struct domain_device *device,
1783 int abort_flag, int tag)
1785 struct sas_task *task;
1786 struct hisi_sas_device *sas_dev = device->lldd_dev;
1787 struct device *dev = hisi_hba->dev;
1791 * The interface is not realized means this HW don't support internal
1792 * abort, or don't need to do internal abort. Then here, we return
1793 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1794 * the internal abort has been executed and returned CQ.
1796 if (!hisi_hba->hw->prep_abort)
1797 return TMF_RESP_FUNC_FAILED;
1799 task = sas_alloc_slow_task(GFP_KERNEL);
1804 task->task_proto = device->tproto;
1805 task->task_done = hisi_sas_task_done;
1806 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1807 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1808 add_timer(&task->slow_task->timer);
1810 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1811 task, abort_flag, tag);
1813 del_timer(&task->slow_task->timer);
1814 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1818 wait_for_completion(&task->slow_task->completion);
1819 res = TMF_RESP_FUNC_FAILED;
1821 /* Internal abort timed out */
1822 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1823 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1824 struct hisi_sas_slot *slot = task->lldd_task;
1828 dev_err(dev, "internal task abort: timeout and not done.\n");
1832 dev_err(dev, "internal task abort: timeout.\n");
1835 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1836 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1837 res = TMF_RESP_FUNC_COMPLETE;
1841 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1842 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1843 res = TMF_RESP_FUNC_SUCC;
1848 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1849 "resp: 0x%x sts 0x%x\n",
1850 SAS_ADDR(device->sas_addr),
1852 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1853 task->task_status.stat);
1854 sas_free_task(task);
1859 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1861 hisi_sas_port_notify_formed(sas_phy);
1864 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1865 u8 reg_index, u8 reg_count, u8 *write_data)
1867 struct hisi_hba *hisi_hba = sha->lldd_ha;
1869 if (!hisi_hba->hw->write_gpio)
1872 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1873 reg_index, reg_count, write_data);
1876 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1878 phy->phy_attached = 0;
1883 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1885 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1886 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1887 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1888 struct device *dev = hisi_hba->dev;
1891 /* Phy down but ready */
1892 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1893 hisi_sas_port_notify_formed(sas_phy);
1895 struct hisi_sas_port *port = phy->port;
1897 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
1899 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
1902 /* Phy down and not ready */
1903 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1904 sas_phy_disconnected(sas_phy);
1907 if (phy->phy_type & PORT_TYPE_SAS) {
1908 int port_id = port->id;
1910 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1912 port->port_attached = 0;
1913 } else if (phy->phy_type & PORT_TYPE_SATA)
1914 port->port_attached = 0;
1916 hisi_sas_phy_disconnected(phy);
1919 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1921 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1925 for (i = 0; i < hisi_hba->queue_count; i++) {
1926 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1928 tasklet_kill(&cq->tasklet);
1931 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1933 struct scsi_transport_template *hisi_sas_stt;
1934 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1936 struct device_attribute *host_attrs[] = {
1937 &dev_attr_phy_event_threshold,
1940 EXPORT_SYMBOL_GPL(host_attrs);
1942 static struct sas_domain_function_template hisi_sas_transport_ops = {
1943 .lldd_dev_found = hisi_sas_dev_found,
1944 .lldd_dev_gone = hisi_sas_dev_gone,
1945 .lldd_execute_task = hisi_sas_queue_command,
1946 .lldd_control_phy = hisi_sas_control_phy,
1947 .lldd_abort_task = hisi_sas_abort_task,
1948 .lldd_abort_task_set = hisi_sas_abort_task_set,
1949 .lldd_clear_aca = hisi_sas_clear_aca,
1950 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1951 .lldd_lu_reset = hisi_sas_lu_reset,
1952 .lldd_query_task = hisi_sas_query_task,
1953 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1954 .lldd_port_formed = hisi_sas_port_formed,
1955 .lldd_write_gpio = hisi_sas_write_gpio,
1958 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1960 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1962 for (i = 0; i < hisi_hba->queue_count; i++) {
1963 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1964 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1966 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1967 memset(hisi_hba->cmd_hdr[i], 0, s);
1970 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1971 memset(hisi_hba->complete_hdr[i], 0, s);
1975 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1976 memset(hisi_hba->initial_fis, 0, s);
1978 s = max_command_entries * sizeof(struct hisi_sas_iost);
1979 memset(hisi_hba->iost, 0, s);
1981 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1982 memset(hisi_hba->breakpoint, 0, s);
1984 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1985 memset(hisi_hba->sata_breakpoint, 0, s);
1987 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1989 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1991 struct device *dev = hisi_hba->dev;
1992 int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
1993 int max_command_entries_ru, sz_slot_buf_ru;
1994 int blk_cnt, slots_per_blk;
1996 sema_init(&hisi_hba->sem, 1);
1997 spin_lock_init(&hisi_hba->lock);
1998 for (i = 0; i < hisi_hba->n_phy; i++) {
1999 hisi_sas_phy_init(hisi_hba, i);
2000 hisi_hba->port[i].port_attached = 0;
2001 hisi_hba->port[i].id = -1;
2004 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2005 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2006 hisi_hba->devices[i].device_id = i;
2007 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
2010 for (i = 0; i < hisi_hba->queue_count; i++) {
2011 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2012 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2014 /* Completion queue structure */
2016 cq->hisi_hba = hisi_hba;
2018 /* Delivery queue structure */
2019 spin_lock_init(&dq->lock);
2020 INIT_LIST_HEAD(&dq->list);
2022 dq->hisi_hba = hisi_hba;
2024 /* Delivery queue */
2025 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2026 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2027 &hisi_hba->cmd_hdr_dma[i],
2029 if (!hisi_hba->cmd_hdr[i])
2032 /* Completion queue */
2033 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2034 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2035 &hisi_hba->complete_hdr_dma[i],
2037 if (!hisi_hba->complete_hdr[i])
2041 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2042 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2044 if (!hisi_hba->itct)
2046 memset(hisi_hba->itct, 0, s);
2048 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2049 sizeof(struct hisi_sas_slot),
2051 if (!hisi_hba->slot_info)
2054 /* roundup to avoid overly large block size */
2055 max_command_entries_ru = roundup(max_command_entries, 64);
2056 sz_slot_buf_ru = roundup(sizeof(struct hisi_sas_slot_buf_table), 64);
2057 s = lcm(max_command_entries_ru, sz_slot_buf_ru);
2058 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2059 slots_per_blk = s / sz_slot_buf_ru;
2060 for (i = 0; i < blk_cnt; i++) {
2061 struct hisi_sas_slot_buf_table *buf;
2063 int slot_index = i * slots_per_blk;
2065 buf = dmam_alloc_coherent(dev, s, &buf_dma, GFP_KERNEL);
2070 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2071 struct hisi_sas_slot *slot;
2073 slot = &hisi_hba->slot_info[slot_index];
2075 slot->buf_dma = buf_dma;
2076 slot->idx = slot_index;
2079 buf_dma += sizeof(*buf);
2083 s = max_command_entries * sizeof(struct hisi_sas_iost);
2084 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2086 if (!hisi_hba->iost)
2089 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2090 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2091 &hisi_hba->breakpoint_dma,
2093 if (!hisi_hba->breakpoint)
2096 hisi_hba->slot_index_count = max_command_entries;
2097 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2098 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2099 if (!hisi_hba->slot_index_tags)
2102 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2103 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2104 &hisi_hba->initial_fis_dma,
2106 if (!hisi_hba->initial_fis)
2109 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2110 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2111 &hisi_hba->sata_breakpoint_dma,
2113 if (!hisi_hba->sata_breakpoint)
2115 hisi_sas_init_mem(hisi_hba);
2117 hisi_sas_slot_index_init(hisi_hba);
2119 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2120 if (!hisi_hba->wq) {
2121 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2129 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2131 void hisi_sas_free(struct hisi_hba *hisi_hba)
2134 destroy_workqueue(hisi_hba->wq);
2136 EXPORT_SYMBOL_GPL(hisi_sas_free);
2138 void hisi_sas_rst_work_handler(struct work_struct *work)
2140 struct hisi_hba *hisi_hba =
2141 container_of(work, struct hisi_hba, rst_work);
2143 hisi_sas_controller_reset(hisi_hba);
2145 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2147 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2149 struct hisi_sas_rst *rst =
2150 container_of(work, struct hisi_sas_rst, work);
2152 if (!hisi_sas_controller_reset(rst->hisi_hba))
2154 complete(rst->completion);
2156 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2158 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2160 struct device *dev = hisi_hba->dev;
2161 struct platform_device *pdev = hisi_hba->platform_dev;
2162 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2165 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2167 dev_err(dev, "could not get property sas-addr\n");
2173 * These properties are only required for platform device-based
2174 * controller with DT firmware.
2176 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2177 "hisilicon,sas-syscon");
2178 if (IS_ERR(hisi_hba->ctrl)) {
2179 dev_err(dev, "could not get syscon\n");
2183 if (device_property_read_u32(dev, "ctrl-reset-reg",
2184 &hisi_hba->ctrl_reset_reg)) {
2186 "could not get property ctrl-reset-reg\n");
2190 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2191 &hisi_hba->ctrl_reset_sts_reg)) {
2193 "could not get property ctrl-reset-sts-reg\n");
2197 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2198 &hisi_hba->ctrl_clock_ena_reg)) {
2200 "could not get property ctrl-clock-ena-reg\n");
2205 refclk = devm_clk_get(dev, NULL);
2207 dev_dbg(dev, "no ref clk property\n");
2209 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2211 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2212 dev_err(dev, "could not get property phy-count\n");
2216 if (device_property_read_u32(dev, "queue-count",
2217 &hisi_hba->queue_count)) {
2218 dev_err(dev, "could not get property queue-count\n");
2224 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2226 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2227 const struct hisi_sas_hw *hw)
2229 struct resource *res;
2230 struct Scsi_Host *shost;
2231 struct hisi_hba *hisi_hba;
2232 struct device *dev = &pdev->dev;
2234 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2236 dev_err(dev, "scsi host alloc failed\n");
2239 hisi_hba = shost_priv(shost);
2241 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2243 hisi_hba->dev = dev;
2244 hisi_hba->platform_dev = pdev;
2245 hisi_hba->shost = shost;
2246 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2248 timer_setup(&hisi_hba->timer, NULL, 0);
2250 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2253 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2254 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2255 dev_err(dev, "No usable DMA addressing method\n");
2259 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2260 hisi_hba->regs = devm_ioremap_resource(dev, res);
2261 if (IS_ERR(hisi_hba->regs))
2264 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2266 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2267 if (IS_ERR(hisi_hba->sgpio_regs))
2271 if (hisi_sas_alloc(hisi_hba, shost)) {
2272 hisi_sas_free(hisi_hba);
2278 scsi_host_put(shost);
2279 dev_err(dev, "shost alloc failed\n");
2283 int hisi_sas_probe(struct platform_device *pdev,
2284 const struct hisi_sas_hw *hw)
2286 struct Scsi_Host *shost;
2287 struct hisi_hba *hisi_hba;
2288 struct device *dev = &pdev->dev;
2289 struct asd_sas_phy **arr_phy;
2290 struct asd_sas_port **arr_port;
2291 struct sas_ha_struct *sha;
2292 int rc, phy_nr, port_nr, i;
2294 shost = hisi_sas_shost_alloc(pdev, hw);
2298 sha = SHOST_TO_SAS_HA(shost);
2299 hisi_hba = shost_priv(shost);
2300 platform_set_drvdata(pdev, sha);
2302 phy_nr = port_nr = hisi_hba->n_phy;
2304 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2305 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2306 if (!arr_phy || !arr_port) {
2311 sha->sas_phy = arr_phy;
2312 sha->sas_port = arr_port;
2313 sha->lldd_ha = hisi_hba;
2315 shost->transportt = hisi_sas_stt;
2316 shost->max_id = HISI_SAS_MAX_DEVICES;
2317 shost->max_lun = ~0;
2318 shost->max_channel = 1;
2319 shost->max_cmd_len = 16;
2320 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2321 shost->can_queue = hisi_hba->hw->max_command_entries;
2322 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2324 sha->sas_ha_name = DRV_NAME;
2325 sha->dev = hisi_hba->dev;
2326 sha->lldd_module = THIS_MODULE;
2327 sha->sas_addr = &hisi_hba->sas_addr[0];
2328 sha->num_phys = hisi_hba->n_phy;
2329 sha->core.shost = hisi_hba->shost;
2331 for (i = 0; i < hisi_hba->n_phy; i++) {
2332 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2333 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2336 rc = scsi_add_host(shost, &pdev->dev);
2340 rc = sas_register_ha(sha);
2342 goto err_out_register_ha;
2344 rc = hisi_hba->hw->hw_init(hisi_hba);
2346 goto err_out_register_ha;
2348 scsi_scan_host(shost);
2352 err_out_register_ha:
2353 scsi_remove_host(shost);
2355 hisi_sas_free(hisi_hba);
2356 scsi_host_put(shost);
2359 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2361 int hisi_sas_remove(struct platform_device *pdev)
2363 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2364 struct hisi_hba *hisi_hba = sha->lldd_ha;
2365 struct Scsi_Host *shost = sha->core.shost;
2367 if (timer_pending(&hisi_hba->timer))
2368 del_timer(&hisi_hba->timer);
2370 sas_unregister_ha(sha);
2371 sas_remove_host(sha->core.shost);
2373 hisi_sas_free(hisi_hba);
2374 scsi_host_put(shost);
2377 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2379 static __init int hisi_sas_init(void)
2381 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2388 static __exit void hisi_sas_exit(void)
2390 sas_release_transport(hisi_sas_stt);
2393 module_init(hisi_sas_init);
2394 module_exit(hisi_sas_exit);
2396 MODULE_LICENSE("GPL");
2397 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2398 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2399 MODULE_ALIAS("platform:" DRV_NAME);