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);
26 u8 hisi_sas_get_ata_protocol(u8 cmd, int direction)
29 case ATA_CMD_FPDMA_WRITE:
30 case ATA_CMD_FPDMA_READ:
31 case ATA_CMD_FPDMA_RECV:
32 case ATA_CMD_FPDMA_SEND:
33 case ATA_CMD_NCQ_NON_DATA:
34 return HISI_SAS_SATA_PROTOCOL_FPDMA;
36 case ATA_CMD_DOWNLOAD_MICRO:
38 case ATA_CMD_PMP_READ:
39 case ATA_CMD_READ_LOG_EXT:
40 case ATA_CMD_PIO_READ:
41 case ATA_CMD_PIO_READ_EXT:
42 case ATA_CMD_PMP_WRITE:
43 case ATA_CMD_WRITE_LOG_EXT:
44 case ATA_CMD_PIO_WRITE:
45 case ATA_CMD_PIO_WRITE_EXT:
46 return HISI_SAS_SATA_PROTOCOL_PIO;
49 case ATA_CMD_DOWNLOAD_MICRO_DMA:
50 case ATA_CMD_PMP_READ_DMA:
51 case ATA_CMD_PMP_WRITE_DMA:
53 case ATA_CMD_READ_EXT:
54 case ATA_CMD_READ_LOG_DMA_EXT:
55 case ATA_CMD_READ_STREAM_DMA_EXT:
56 case ATA_CMD_TRUSTED_RCV_DMA:
57 case ATA_CMD_TRUSTED_SND_DMA:
59 case ATA_CMD_WRITE_EXT:
60 case ATA_CMD_WRITE_FUA_EXT:
61 case ATA_CMD_WRITE_QUEUED:
62 case ATA_CMD_WRITE_LOG_DMA_EXT:
63 case ATA_CMD_WRITE_STREAM_DMA_EXT:
64 case ATA_CMD_ZAC_MGMT_IN:
65 return HISI_SAS_SATA_PROTOCOL_DMA;
67 case ATA_CMD_CHK_POWER:
68 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_FLUSH_EXT:
73 case ATA_CMD_VERIFY_EXT:
74 case ATA_CMD_SET_FEATURES:
76 case ATA_CMD_STANDBYNOW1:
77 case ATA_CMD_ZAC_MGMT_OUT:
78 return HISI_SAS_SATA_PROTOCOL_NONDATA;
80 if (direction == DMA_NONE)
81 return HISI_SAS_SATA_PROTOCOL_NONDATA;
82 return HISI_SAS_SATA_PROTOCOL_PIO;
85 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
87 void hisi_sas_sata_done(struct sas_task *task,
88 struct hisi_sas_slot *slot)
90 struct task_status_struct *ts = &task->task_status;
91 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
92 struct hisi_sas_status_buffer *status_buf =
93 hisi_sas_status_buf_addr_mem(slot);
94 u8 *iu = &status_buf->iu[0];
95 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
97 resp->frame_len = sizeof(struct dev_to_host_fis);
98 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
100 ts->buf_valid_size = sizeof(*resp);
102 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
104 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
106 struct ata_queued_cmd *qc = task->uldd_task;
109 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
110 qc->tf.command == ATA_CMD_FPDMA_READ) {
117 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
119 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
121 return device->port->ha->lldd_ha;
124 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
126 return container_of(sas_port, struct hisi_sas_port, sas_port);
128 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
130 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
134 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
135 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
137 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
139 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
141 void *bitmap = hisi_hba->slot_index_tags;
143 clear_bit(slot_idx, bitmap);
146 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
148 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
151 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
153 void *bitmap = hisi_hba->slot_index_tags;
155 set_bit(slot_idx, bitmap);
158 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
161 void *bitmap = hisi_hba->slot_index_tags;
163 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
164 if (index >= hisi_hba->slot_index_count)
165 return -SAS_QUEUE_FULL;
166 hisi_sas_slot_index_set(hisi_hba, index);
171 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
175 for (i = 0; i < hisi_hba->slot_index_count; ++i)
176 hisi_sas_slot_index_clear(hisi_hba, i);
179 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
180 struct hisi_sas_slot *slot)
184 struct device *dev = hisi_hba->dev;
185 struct domain_device *device = task->dev;
186 struct hisi_sas_device *sas_dev = device->lldd_dev;
188 if (!task->lldd_task)
191 task->lldd_task = NULL;
193 if (!sas_protocol_ata(task->task_proto))
195 dma_unmap_sg(dev, task->scatter,
200 atomic64_dec(&sas_dev->running_req);
204 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
206 list_del_init(&slot->entry);
210 hisi_sas_slot_index_free(hisi_hba, slot->idx);
212 /* slot memory is fully zeroed when it is reused */
214 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
216 static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
217 struct hisi_sas_slot *slot)
219 return hisi_hba->hw->prep_smp(hisi_hba, slot);
222 static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
223 struct hisi_sas_slot *slot, int is_tmf,
224 struct hisi_sas_tmf_task *tmf)
226 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
229 static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
230 struct hisi_sas_slot *slot)
232 return hisi_hba->hw->prep_stp(hisi_hba, slot);
235 static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
236 struct hisi_sas_slot *slot,
237 int device_id, int abort_flag, int tag_to_abort)
239 return hisi_hba->hw->prep_abort(hisi_hba, slot,
240 device_id, abort_flag, tag_to_abort);
244 * This function will issue an abort TMF regardless of whether the
245 * task is in the sdev or not. Then it will do the task complete
246 * cleanup and callbacks.
248 static void hisi_sas_slot_abort(struct work_struct *work)
250 struct hisi_sas_slot *abort_slot =
251 container_of(work, struct hisi_sas_slot, abort_slot);
252 struct sas_task *task = abort_slot->task;
253 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
254 struct scsi_cmnd *cmnd = task->uldd_task;
255 struct hisi_sas_tmf_task tmf_task;
257 struct device *dev = hisi_hba->dev;
258 int tag = abort_slot->idx;
261 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
262 dev_err(dev, "cannot abort slot for non-ssp task\n");
266 int_to_scsilun(cmnd->device->lun, &lun);
267 tmf_task.tmf = TMF_ABORT_TASK;
268 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
270 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
272 /* Do cleanup for this task */
273 spin_lock_irqsave(&hisi_hba->lock, flags);
274 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
275 spin_unlock_irqrestore(&hisi_hba->lock, flags);
277 task->task_done(task);
280 static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
281 *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
284 struct hisi_hba *hisi_hba = dq->hisi_hba;
285 struct domain_device *device = task->dev;
286 struct hisi_sas_device *sas_dev = device->lldd_dev;
287 struct hisi_sas_port *port;
288 struct hisi_sas_slot *slot;
289 struct hisi_sas_cmd_hdr *cmd_hdr_base;
290 struct asd_sas_port *sas_port = device->port;
291 struct device *dev = hisi_hba->dev;
292 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
296 struct task_status_struct *ts = &task->task_status;
298 ts->resp = SAS_TASK_UNDELIVERED;
299 ts->stat = SAS_PHY_DOWN;
301 * libsas will use dev->port, should
302 * not call task_done for sata
304 if (device->dev_type != SAS_SATA_DEV)
305 task->task_done(task);
309 if (DEV_IS_GONE(sas_dev)) {
311 dev_info(dev, "task prep: device %d not ready\n",
314 dev_info(dev, "task prep: device %016llx not ready\n",
315 SAS_ADDR(device->sas_addr));
320 port = to_hisi_sas_port(sas_port);
321 if (port && !port->port_attached) {
322 dev_info(dev, "task prep: %s port%d not attach device\n",
323 (dev_is_sata(device)) ?
330 if (!sas_protocol_ata(task->task_proto)) {
331 if (task->num_scatter) {
332 n_elem = dma_map_sg(dev, task->scatter,
333 task->num_scatter, task->data_dir);
340 n_elem = task->num_scatter;
342 spin_lock_irqsave(&hisi_hba->lock, flags);
343 if (hisi_hba->hw->slot_index_alloc)
344 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
347 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
349 spin_unlock_irqrestore(&hisi_hba->lock, flags);
352 spin_unlock_irqrestore(&hisi_hba->lock, flags);
354 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
358 dlvry_queue = dq->id;
359 dlvry_queue_slot = dq->wr_point;
360 slot = &hisi_hba->slot_info[slot_idx];
361 memset(slot, 0, sizeof(struct hisi_sas_slot));
363 slot->idx = slot_idx;
364 slot->n_elem = n_elem;
365 slot->dlvry_queue = dlvry_queue;
366 slot->dlvry_queue_slot = dlvry_queue_slot;
367 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
368 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
371 task->lldd_task = slot;
372 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
374 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
375 GFP_ATOMIC, &slot->buf_dma);
378 goto err_out_slot_buf;
380 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
381 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
382 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
384 switch (task->task_proto) {
385 case SAS_PROTOCOL_SMP:
386 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
388 case SAS_PROTOCOL_SSP:
389 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
391 case SAS_PROTOCOL_SATA:
392 case SAS_PROTOCOL_STP:
393 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
394 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
397 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
404 dev_err(dev, "task prep: rc = 0x%x\n", rc);
408 spin_lock_irqsave(&hisi_hba->lock, flags);
409 list_add_tail(&slot->entry, &sas_dev->list);
410 spin_unlock_irqrestore(&hisi_hba->lock, flags);
411 spin_lock_irqsave(&task->task_state_lock, flags);
412 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
413 spin_unlock_irqrestore(&task->task_state_lock, flags);
415 dq->slot_prep = slot;
417 atomic64_inc(&sas_dev->running_req);
423 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
426 /* Nothing to be done */
428 spin_lock_irqsave(&hisi_hba->lock, flags);
429 hisi_sas_slot_index_free(hisi_hba, slot_idx);
430 spin_unlock_irqrestore(&hisi_hba->lock, flags);
432 dev_err(dev, "task prep: failed[%d]!\n", rc);
433 if (!sas_protocol_ata(task->task_proto))
435 dma_unmap_sg(dev, task->scatter,
442 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
443 int is_tmf, struct hisi_sas_tmf_task *tmf)
448 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
449 struct device *dev = hisi_hba->dev;
450 struct domain_device *device = task->dev;
451 struct hisi_sas_device *sas_dev = device->lldd_dev;
452 struct hisi_sas_dq *dq = sas_dev->dq;
454 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
457 /* protect task_prep and start_delivery sequence */
458 spin_lock_irqsave(&dq->lock, flags);
459 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
461 dev_err(dev, "task exec: failed[%d]!\n", rc);
464 hisi_hba->hw->start_delivery(dq);
465 spin_unlock_irqrestore(&dq->lock, flags);
470 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
472 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
473 struct asd_sas_phy *sas_phy = &phy->sas_phy;
474 struct sas_ha_struct *sas_ha;
476 if (!phy->phy_attached)
479 sas_ha = &hisi_hba->sha;
480 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
483 struct sas_phy *sphy = sas_phy->phy;
485 sphy->negotiated_linkrate = sas_phy->linkrate;
486 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
487 sphy->maximum_linkrate_hw =
488 hisi_hba->hw->phy_get_max_linkrate();
489 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
490 sphy->minimum_linkrate = phy->minimum_linkrate;
492 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
493 sphy->maximum_linkrate = phy->maximum_linkrate;
496 if (phy->phy_type & PORT_TYPE_SAS) {
497 struct sas_identify_frame *id;
499 id = (struct sas_identify_frame *)phy->frame_rcvd;
500 id->dev_type = phy->identify.device_type;
501 id->initiator_bits = SAS_PROTOCOL_ALL;
502 id->target_bits = phy->identify.target_port_protocols;
503 } else if (phy->phy_type & PORT_TYPE_SATA) {
507 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
508 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
511 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
513 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
514 struct hisi_sas_device *sas_dev = NULL;
518 spin_lock_irqsave(&hisi_hba->lock, flags);
519 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
520 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
521 int queue = i % hisi_hba->queue_count;
522 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
524 hisi_hba->devices[i].device_id = i;
525 sas_dev = &hisi_hba->devices[i];
526 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
527 sas_dev->dev_type = device->dev_type;
528 sas_dev->hisi_hba = hisi_hba;
529 sas_dev->sas_device = device;
531 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
535 spin_unlock_irqrestore(&hisi_hba->lock, flags);
540 static int hisi_sas_dev_found(struct domain_device *device)
542 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
543 struct domain_device *parent_dev = device->parent;
544 struct hisi_sas_device *sas_dev;
545 struct device *dev = hisi_hba->dev;
547 if (hisi_hba->hw->alloc_dev)
548 sas_dev = hisi_hba->hw->alloc_dev(device);
550 sas_dev = hisi_sas_alloc_dev(device);
552 dev_err(dev, "fail alloc dev: max support %d devices\n",
553 HISI_SAS_MAX_DEVICES);
557 device->lldd_dev = sas_dev;
558 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
560 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
562 u8 phy_num = parent_dev->ex_dev.num_phys;
565 for (phy_no = 0; phy_no < phy_num; phy_no++) {
566 phy = &parent_dev->ex_dev.ex_phy[phy_no];
567 if (SAS_ADDR(phy->attached_sas_addr) ==
568 SAS_ADDR(device->sas_addr)) {
569 sas_dev->attached_phy = phy_no;
574 if (phy_no == phy_num) {
575 dev_info(dev, "dev found: no attached "
576 "dev:%016llx at ex:%016llx\n",
577 SAS_ADDR(device->sas_addr),
578 SAS_ADDR(parent_dev->sas_addr));
586 static int hisi_sas_slave_configure(struct scsi_device *sdev)
588 struct domain_device *dev = sdev_to_domain_dev(sdev);
589 int ret = sas_slave_configure(sdev);
593 if (!dev_is_sata(dev))
594 sas_change_queue_depth(sdev, 64);
599 static void hisi_sas_scan_start(struct Scsi_Host *shost)
601 struct hisi_hba *hisi_hba = shost_priv(shost);
603 hisi_hba->hw->phys_init(hisi_hba);
606 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
608 struct hisi_hba *hisi_hba = shost_priv(shost);
609 struct sas_ha_struct *sha = &hisi_hba->sha;
611 /* Wait for PHY up interrupt to occur */
619 static void hisi_sas_phyup_work(struct work_struct *work)
621 struct hisi_sas_phy *phy =
622 container_of(work, struct hisi_sas_phy, phyup_ws);
623 struct hisi_hba *hisi_hba = phy->hisi_hba;
624 struct asd_sas_phy *sas_phy = &phy->sas_phy;
625 int phy_no = sas_phy->id;
627 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
628 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
631 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
633 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
634 struct asd_sas_phy *sas_phy = &phy->sas_phy;
636 phy->hisi_hba = hisi_hba;
638 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
639 sas_phy->class = SAS;
640 sas_phy->iproto = SAS_PROTOCOL_ALL;
642 sas_phy->type = PHY_TYPE_PHYSICAL;
643 sas_phy->role = PHY_ROLE_INITIATOR;
644 sas_phy->oob_mode = OOB_NOT_CONNECTED;
645 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
646 sas_phy->id = phy_no;
647 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
648 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
649 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
650 sas_phy->lldd_phy = phy;
652 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
655 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
657 struct sas_ha_struct *sas_ha = sas_phy->ha;
658 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
659 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
660 struct asd_sas_port *sas_port = sas_phy->port;
661 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
667 spin_lock_irqsave(&hisi_hba->lock, flags);
668 port->port_attached = 1;
669 port->id = phy->port_id;
671 sas_port->lldd_port = port;
672 spin_unlock_irqrestore(&hisi_hba->lock, flags);
675 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
676 struct hisi_sas_slot *slot)
680 struct task_status_struct *ts;
682 ts = &task->task_status;
684 ts->resp = SAS_TASK_COMPLETE;
685 ts->stat = SAS_ABORTED_TASK;
686 spin_lock_irqsave(&task->task_state_lock, flags);
687 task->task_state_flags &=
688 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
689 task->task_state_flags |= SAS_TASK_STATE_DONE;
690 spin_unlock_irqrestore(&task->task_state_lock, flags);
693 hisi_sas_slot_task_free(hisi_hba, task, slot);
696 /* hisi_hba.lock should be locked */
697 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
698 struct domain_device *device)
700 struct hisi_sas_slot *slot, *slot2;
701 struct hisi_sas_device *sas_dev = device->lldd_dev;
703 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
704 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
707 static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
709 struct hisi_sas_device *sas_dev;
710 struct domain_device *device;
713 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
714 sas_dev = &hisi_hba->devices[i];
715 device = sas_dev->sas_device;
717 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
721 hisi_sas_release_task(hisi_hba, device);
725 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
726 struct domain_device *device)
728 if (hisi_hba->hw->dereg_device)
729 hisi_hba->hw->dereg_device(hisi_hba, device);
732 static void hisi_sas_dev_gone(struct domain_device *device)
734 struct hisi_sas_device *sas_dev = device->lldd_dev;
735 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
736 struct device *dev = hisi_hba->dev;
738 dev_info(dev, "found dev[%d:%x] is gone\n",
739 sas_dev->device_id, sas_dev->dev_type);
741 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
742 hisi_sas_internal_task_abort(hisi_hba, device,
743 HISI_SAS_INT_ABT_DEV, 0);
745 hisi_sas_dereg_device(hisi_hba, device);
747 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
748 device->lldd_dev = NULL;
749 memset(sas_dev, 0, sizeof(*sas_dev));
752 if (hisi_hba->hw->free_device)
753 hisi_hba->hw->free_device(sas_dev);
754 sas_dev->dev_type = SAS_PHY_UNUSED;
757 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
759 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
762 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
765 struct sas_ha_struct *sas_ha = sas_phy->ha;
766 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
767 int phy_no = sas_phy->id;
770 case PHY_FUNC_HARD_RESET:
771 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
774 case PHY_FUNC_LINK_RESET:
775 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
777 hisi_hba->hw->phy_start(hisi_hba, phy_no);
780 case PHY_FUNC_DISABLE:
781 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
784 case PHY_FUNC_SET_LINK_RATE:
785 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
787 case PHY_FUNC_GET_EVENTS:
788 if (hisi_hba->hw->get_events) {
789 hisi_hba->hw->get_events(hisi_hba, phy_no);
793 case PHY_FUNC_RELEASE_SPINUP_HOLD:
800 static void hisi_sas_task_done(struct sas_task *task)
802 if (!del_timer(&task->slow_task->timer))
804 complete(&task->slow_task->completion);
807 static void hisi_sas_tmf_timedout(struct timer_list *t)
809 struct sas_task_slow *slow = from_timer(slow, t, timer);
810 struct sas_task *task = slow->task;
813 spin_lock_irqsave(&task->task_state_lock, flags);
814 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
815 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
816 spin_unlock_irqrestore(&task->task_state_lock, flags);
818 complete(&task->slow_task->completion);
821 #define TASK_TIMEOUT 20
823 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
824 void *parameter, u32 para_len,
825 struct hisi_sas_tmf_task *tmf)
827 struct hisi_sas_device *sas_dev = device->lldd_dev;
828 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
829 struct device *dev = hisi_hba->dev;
830 struct sas_task *task;
833 for (retry = 0; retry < TASK_RETRY; retry++) {
834 task = sas_alloc_slow_task(GFP_KERNEL);
839 task->task_proto = device->tproto;
841 if (dev_is_sata(device)) {
842 task->ata_task.device_control_reg_update = 1;
843 memcpy(&task->ata_task.fis, parameter, para_len);
845 memcpy(&task->ssp_task, parameter, para_len);
847 task->task_done = hisi_sas_task_done;
849 task->slow_task->timer.function = hisi_sas_tmf_timedout;
850 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
851 add_timer(&task->slow_task->timer);
853 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
856 del_timer(&task->slow_task->timer);
857 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
862 wait_for_completion(&task->slow_task->completion);
863 res = TMF_RESP_FUNC_FAILED;
864 /* Even TMF timed out, return direct. */
865 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
866 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
867 struct hisi_sas_slot *slot = task->lldd_task;
869 dev_err(dev, "abort tmf: TMF task timeout\n");
877 if (task->task_status.resp == SAS_TASK_COMPLETE &&
878 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
879 res = TMF_RESP_FUNC_COMPLETE;
883 if (task->task_status.resp == SAS_TASK_COMPLETE &&
884 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
885 res = TMF_RESP_FUNC_SUCC;
889 if (task->task_status.resp == SAS_TASK_COMPLETE &&
890 task->task_status.stat == SAS_DATA_UNDERRUN) {
891 /* no error, but return the number of bytes of
894 dev_warn(dev, "abort tmf: task to dev %016llx "
895 "resp: 0x%x sts 0x%x underrun\n",
896 SAS_ADDR(device->sas_addr),
897 task->task_status.resp,
898 task->task_status.stat);
899 res = task->task_status.residual;
903 if (task->task_status.resp == SAS_TASK_COMPLETE &&
904 task->task_status.stat == SAS_DATA_OVERRUN) {
905 dev_warn(dev, "abort tmf: blocked task error\n");
910 dev_warn(dev, "abort tmf: task to dev "
911 "%016llx resp: 0x%x status 0x%x\n",
912 SAS_ADDR(device->sas_addr), task->task_status.resp,
913 task->task_status.stat);
918 if (retry == TASK_RETRY)
919 dev_warn(dev, "abort tmf: executing internal task failed!\n");
924 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
925 bool reset, int pmp, u8 *fis)
927 struct ata_taskfile tf;
929 ata_tf_init(dev, &tf);
934 tf.command = ATA_CMD_DEV_RESET;
935 ata_tf_to_fis(&tf, pmp, 0, fis);
938 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
941 struct ata_port *ap = device->sata_dev.ap;
942 struct ata_link *link;
943 int rc = TMF_RESP_FUNC_FAILED;
944 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
945 struct device *dev = hisi_hba->dev;
946 int s = sizeof(struct host_to_dev_fis);
949 ata_for_each_link(link, ap, EDGE) {
950 int pmp = sata_srst_pmp(link);
952 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
953 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
954 if (rc != TMF_RESP_FUNC_COMPLETE)
958 if (rc == TMF_RESP_FUNC_COMPLETE) {
959 ata_for_each_link(link, ap, EDGE) {
960 int pmp = sata_srst_pmp(link);
962 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
963 rc = hisi_sas_exec_internal_tmf_task(device, fis,
965 if (rc != TMF_RESP_FUNC_COMPLETE)
966 dev_err(dev, "ata disk de-reset failed\n");
969 dev_err(dev, "ata disk reset failed\n");
972 if (rc == TMF_RESP_FUNC_COMPLETE) {
973 spin_lock_irqsave(&hisi_hba->lock, flags);
974 hisi_sas_release_task(hisi_hba, device);
975 spin_unlock_irqrestore(&hisi_hba->lock, flags);
981 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
982 u8 *lun, struct hisi_sas_tmf_task *tmf)
984 struct sas_ssp_task ssp_task;
986 if (!(device->tproto & SAS_PROTOCOL_SSP))
987 return TMF_RESP_FUNC_ESUPP;
989 memcpy(ssp_task.LUN, lun, 8);
991 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
992 sizeof(ssp_task), tmf);
995 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
997 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1000 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1001 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1002 struct domain_device *device = sas_dev->sas_device;
1003 struct asd_sas_port *sas_port;
1004 struct hisi_sas_port *port;
1005 struct hisi_sas_phy *phy = NULL;
1006 struct asd_sas_phy *sas_phy;
1008 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1009 || !device || !device->port)
1012 sas_port = device->port;
1013 port = to_hisi_sas_port(sas_port);
1015 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1016 if (state & BIT(sas_phy->id)) {
1017 phy = sas_phy->lldd_phy;
1022 port->id = phy->port_id;
1024 /* Update linkrate of directly attached device. */
1025 if (!device->parent)
1026 device->linkrate = phy->sas_phy.linkrate;
1028 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1034 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1037 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1038 struct asd_sas_port *_sas_port = NULL;
1041 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1042 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1043 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1044 struct asd_sas_port *sas_port = sas_phy->port;
1045 bool do_port_check = !!(_sas_port != sas_port);
1047 if (!sas_phy->phy->enabled)
1050 /* Report PHY state change to libsas */
1051 if (state & BIT(phy_no)) {
1052 if (do_port_check && sas_port && sas_port->port_dev) {
1053 struct domain_device *dev = sas_port->port_dev;
1055 _sas_port = sas_port;
1057 if (DEV_IS_EXPANDER(dev->dev_type))
1058 sas_ha->notify_port_event(sas_phy,
1059 PORTE_BROADCAST_RCVD);
1061 } else if (old_state & (1 << phy_no))
1062 /* PHY down but was up before */
1063 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1068 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1070 struct device *dev = hisi_hba->dev;
1071 struct Scsi_Host *shost = hisi_hba->shost;
1072 u32 old_state, state;
1073 unsigned long flags;
1076 if (!hisi_hba->hw->soft_reset)
1079 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1082 dev_info(dev, "controller resetting...\n");
1083 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1085 scsi_block_requests(shost);
1086 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1087 rc = hisi_hba->hw->soft_reset(hisi_hba);
1089 dev_warn(dev, "controller reset failed (%d)\n", rc);
1090 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1091 scsi_unblock_requests(shost);
1094 spin_lock_irqsave(&hisi_hba->lock, flags);
1095 hisi_sas_release_tasks(hisi_hba);
1096 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1098 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1100 /* Init and wait for PHYs to come up and all libsas event finished. */
1101 hisi_hba->hw->phys_init(hisi_hba);
1103 hisi_sas_refresh_port_id(hisi_hba);
1104 scsi_unblock_requests(shost);
1106 state = hisi_hba->hw->get_phys_state(hisi_hba);
1107 hisi_sas_rescan_topology(hisi_hba, old_state, state);
1108 dev_info(dev, "controller reset complete\n");
1111 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1116 static int hisi_sas_abort_task(struct sas_task *task)
1118 struct scsi_lun lun;
1119 struct hisi_sas_tmf_task tmf_task;
1120 struct domain_device *device = task->dev;
1121 struct hisi_sas_device *sas_dev = device->lldd_dev;
1122 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
1123 struct device *dev = hisi_hba->dev;
1124 int rc = TMF_RESP_FUNC_FAILED;
1125 unsigned long flags;
1128 dev_warn(dev, "Device has been removed\n");
1129 return TMF_RESP_FUNC_FAILED;
1132 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1133 rc = TMF_RESP_FUNC_COMPLETE;
1137 sas_dev->dev_status = HISI_SAS_DEV_EH;
1138 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1139 struct scsi_cmnd *cmnd = task->uldd_task;
1140 struct hisi_sas_slot *slot = task->lldd_task;
1141 u32 tag = slot->idx;
1144 int_to_scsilun(cmnd->device->lun, &lun);
1145 tmf_task.tmf = TMF_ABORT_TASK;
1146 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1148 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1151 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1152 HISI_SAS_INT_ABT_CMD, tag);
1154 * If the TMF finds that the IO is not in the device and also
1155 * the internal abort does not succeed, then it is safe to
1157 * Note: if the internal abort succeeds then the slot
1158 * will have already been completed
1160 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1161 if (task->lldd_task) {
1162 spin_lock_irqsave(&hisi_hba->lock, flags);
1163 hisi_sas_do_release_task(hisi_hba, task, slot);
1164 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1167 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1168 task->task_proto & SAS_PROTOCOL_STP) {
1169 if (task->dev->dev_type == SAS_SATA_DEV) {
1170 hisi_sas_internal_task_abort(hisi_hba, device,
1171 HISI_SAS_INT_ABT_DEV, 0);
1172 hisi_sas_dereg_device(hisi_hba, device);
1173 rc = hisi_sas_softreset_ata_disk(device);
1175 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1177 struct hisi_sas_slot *slot = task->lldd_task;
1178 u32 tag = slot->idx;
1180 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1181 HISI_SAS_INT_ABT_CMD, tag);
1182 if (rc == TMF_RESP_FUNC_FAILED && task->lldd_task) {
1183 spin_lock_irqsave(&hisi_hba->lock, flags);
1184 hisi_sas_do_release_task(hisi_hba, task, slot);
1185 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1190 if (rc != TMF_RESP_FUNC_COMPLETE)
1191 dev_notice(dev, "abort task: rc=%d\n", rc);
1195 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1197 struct hisi_sas_tmf_task tmf_task;
1198 int rc = TMF_RESP_FUNC_FAILED;
1200 tmf_task.tmf = TMF_ABORT_TASK_SET;
1201 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1206 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1208 int rc = TMF_RESP_FUNC_FAILED;
1209 struct hisi_sas_tmf_task tmf_task;
1211 tmf_task.tmf = TMF_CLEAR_ACA;
1212 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1217 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1219 struct sas_phy *phy = sas_get_local_phy(device);
1220 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1221 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1222 rc = sas_phy_reset(phy, reset_type);
1223 sas_put_local_phy(phy);
1228 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1230 struct hisi_sas_device *sas_dev = device->lldd_dev;
1231 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1232 unsigned long flags;
1233 int rc = TMF_RESP_FUNC_FAILED;
1235 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1236 return TMF_RESP_FUNC_FAILED;
1237 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1239 hisi_sas_internal_task_abort(hisi_hba, device,
1240 HISI_SAS_INT_ABT_DEV, 0);
1241 hisi_sas_dereg_device(hisi_hba, device);
1243 rc = hisi_sas_debug_I_T_nexus_reset(device);
1245 if (rc == TMF_RESP_FUNC_COMPLETE) {
1246 spin_lock_irqsave(&hisi_hba->lock, flags);
1247 hisi_sas_release_task(hisi_hba, device);
1248 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1253 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1255 struct hisi_sas_device *sas_dev = device->lldd_dev;
1256 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1257 struct device *dev = hisi_hba->dev;
1258 unsigned long flags;
1259 int rc = TMF_RESP_FUNC_FAILED;
1261 sas_dev->dev_status = HISI_SAS_DEV_EH;
1262 if (dev_is_sata(device)) {
1263 struct sas_phy *phy;
1265 /* Clear internal IO and then hardreset */
1266 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1267 HISI_SAS_INT_ABT_DEV, 0);
1268 if (rc == TMF_RESP_FUNC_FAILED)
1270 hisi_sas_dereg_device(hisi_hba, device);
1272 phy = sas_get_local_phy(device);
1274 rc = sas_phy_reset(phy, 1);
1277 spin_lock_irqsave(&hisi_hba->lock, flags);
1278 hisi_sas_release_task(hisi_hba, device);
1279 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1281 sas_put_local_phy(phy);
1283 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1285 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1286 if (rc == TMF_RESP_FUNC_COMPLETE) {
1287 spin_lock_irqsave(&hisi_hba->lock, flags);
1288 hisi_sas_release_task(hisi_hba, device);
1289 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1293 if (rc != TMF_RESP_FUNC_COMPLETE)
1294 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1295 sas_dev->device_id, rc);
1299 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1301 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1303 return hisi_sas_controller_reset(hisi_hba);
1306 static int hisi_sas_query_task(struct sas_task *task)
1308 struct scsi_lun lun;
1309 struct hisi_sas_tmf_task tmf_task;
1310 int rc = TMF_RESP_FUNC_FAILED;
1312 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1313 struct scsi_cmnd *cmnd = task->uldd_task;
1314 struct domain_device *device = task->dev;
1315 struct hisi_sas_slot *slot = task->lldd_task;
1316 u32 tag = slot->idx;
1318 int_to_scsilun(cmnd->device->lun, &lun);
1319 tmf_task.tmf = TMF_QUERY_TASK;
1320 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1322 rc = hisi_sas_debug_issue_ssp_tmf(device,
1326 /* The task is still in Lun, release it then */
1327 case TMF_RESP_FUNC_SUCC:
1328 /* The task is not in Lun or failed, reset the phy */
1329 case TMF_RESP_FUNC_FAILED:
1330 case TMF_RESP_FUNC_COMPLETE:
1333 rc = TMF_RESP_FUNC_FAILED;
1341 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1342 struct sas_task *task, int abort_flag,
1345 struct domain_device *device = task->dev;
1346 struct hisi_sas_device *sas_dev = device->lldd_dev;
1347 struct device *dev = hisi_hba->dev;
1348 struct hisi_sas_port *port;
1349 struct hisi_sas_slot *slot;
1350 struct asd_sas_port *sas_port = device->port;
1351 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1352 struct hisi_sas_dq *dq = sas_dev->dq;
1353 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1354 unsigned long flags, flags_dq;
1356 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1362 port = to_hisi_sas_port(sas_port);
1364 /* simply get a slot and send abort command */
1365 spin_lock_irqsave(&hisi_hba->lock, flags);
1366 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1368 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1371 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1373 spin_lock_irqsave(&dq->lock, flags_dq);
1374 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1378 dlvry_queue = dq->id;
1379 dlvry_queue_slot = dq->wr_point;
1381 slot = &hisi_hba->slot_info[slot_idx];
1382 memset(slot, 0, sizeof(struct hisi_sas_slot));
1384 slot->idx = slot_idx;
1385 slot->n_elem = n_elem;
1386 slot->dlvry_queue = dlvry_queue;
1387 slot->dlvry_queue_slot = dlvry_queue_slot;
1388 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1389 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1392 task->lldd_task = slot;
1394 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1395 GFP_ATOMIC, &slot->buf_dma);
1401 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1402 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1403 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1405 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1406 abort_flag, task_tag);
1410 spin_lock_irqsave(&hisi_hba->lock, flags);
1411 list_add_tail(&slot->entry, &sas_dev->list);
1412 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1413 spin_lock_irqsave(&task->task_state_lock, flags);
1414 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1415 spin_unlock_irqrestore(&task->task_state_lock, flags);
1417 dq->slot_prep = slot;
1419 atomic64_inc(&sas_dev->running_req);
1421 /* send abort command to the chip */
1422 hisi_hba->hw->start_delivery(dq);
1423 spin_unlock_irqrestore(&dq->lock, flags_dq);
1428 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1431 spin_lock_irqsave(&hisi_hba->lock, flags);
1432 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1433 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1434 spin_unlock_irqrestore(&dq->lock, flags_dq);
1436 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1442 * hisi_sas_internal_task_abort -- execute an internal
1443 * abort command for single IO command or a device
1444 * @hisi_hba: host controller struct
1445 * @device: domain device
1446 * @abort_flag: mode of operation, device or single IO
1447 * @tag: tag of IO to be aborted (only relevant to single
1451 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1452 struct domain_device *device,
1453 int abort_flag, int tag)
1455 struct sas_task *task;
1456 struct hisi_sas_device *sas_dev = device->lldd_dev;
1457 struct device *dev = hisi_hba->dev;
1460 if (!hisi_hba->hw->prep_abort)
1463 task = sas_alloc_slow_task(GFP_KERNEL);
1468 task->task_proto = device->tproto;
1469 task->task_done = hisi_sas_task_done;
1470 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1471 task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
1472 add_timer(&task->slow_task->timer);
1474 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1475 task, abort_flag, tag);
1477 del_timer(&task->slow_task->timer);
1478 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1482 wait_for_completion(&task->slow_task->completion);
1483 res = TMF_RESP_FUNC_FAILED;
1485 /* Internal abort timed out */
1486 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1487 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1488 struct hisi_sas_slot *slot = task->lldd_task;
1492 dev_err(dev, "internal task abort: timeout.\n");
1497 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1498 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1499 res = TMF_RESP_FUNC_COMPLETE;
1503 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1504 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1505 res = TMF_RESP_FUNC_SUCC;
1510 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1511 "resp: 0x%x sts 0x%x\n",
1512 SAS_ADDR(device->sas_addr),
1514 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1515 task->task_status.stat);
1516 sas_free_task(task);
1521 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1523 hisi_sas_port_notify_formed(sas_phy);
1526 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1528 phy->phy_attached = 0;
1533 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1535 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1536 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1537 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1540 /* Phy down but ready */
1541 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1542 hisi_sas_port_notify_formed(sas_phy);
1544 struct hisi_sas_port *port = phy->port;
1546 /* Phy down and not ready */
1547 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1548 sas_phy_disconnected(sas_phy);
1551 if (phy->phy_type & PORT_TYPE_SAS) {
1552 int port_id = port->id;
1554 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1556 port->port_attached = 0;
1557 } else if (phy->phy_type & PORT_TYPE_SATA)
1558 port->port_attached = 0;
1560 hisi_sas_phy_disconnected(phy);
1563 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1565 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1569 for (i = 0; i < hisi_hba->queue_count; i++) {
1570 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1572 tasklet_kill(&cq->tasklet);
1575 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1577 struct scsi_transport_template *hisi_sas_stt;
1578 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1580 static struct scsi_host_template _hisi_sas_sht = {
1581 .module = THIS_MODULE,
1583 .queuecommand = sas_queuecommand,
1584 .target_alloc = sas_target_alloc,
1585 .slave_configure = hisi_sas_slave_configure,
1586 .scan_finished = hisi_sas_scan_finished,
1587 .scan_start = hisi_sas_scan_start,
1588 .change_queue_depth = sas_change_queue_depth,
1589 .bios_param = sas_bios_param,
1592 .sg_tablesize = SG_ALL,
1593 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1594 .use_clustering = ENABLE_CLUSTERING,
1595 .eh_device_reset_handler = sas_eh_device_reset_handler,
1596 .eh_target_reset_handler = sas_eh_target_reset_handler,
1597 .target_destroy = sas_target_destroy,
1600 struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1601 EXPORT_SYMBOL_GPL(hisi_sas_sht);
1603 static struct sas_domain_function_template hisi_sas_transport_ops = {
1604 .lldd_dev_found = hisi_sas_dev_found,
1605 .lldd_dev_gone = hisi_sas_dev_gone,
1606 .lldd_execute_task = hisi_sas_queue_command,
1607 .lldd_control_phy = hisi_sas_control_phy,
1608 .lldd_abort_task = hisi_sas_abort_task,
1609 .lldd_abort_task_set = hisi_sas_abort_task_set,
1610 .lldd_clear_aca = hisi_sas_clear_aca,
1611 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1612 .lldd_lu_reset = hisi_sas_lu_reset,
1613 .lldd_query_task = hisi_sas_query_task,
1614 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1615 .lldd_port_formed = hisi_sas_port_formed,
1618 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1620 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1622 for (i = 0; i < hisi_hba->queue_count; i++) {
1623 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1624 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1626 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1627 memset(hisi_hba->cmd_hdr[i], 0, s);
1630 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1631 memset(hisi_hba->complete_hdr[i], 0, s);
1635 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1636 memset(hisi_hba->initial_fis, 0, s);
1638 s = max_command_entries * sizeof(struct hisi_sas_iost);
1639 memset(hisi_hba->iost, 0, s);
1641 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1642 memset(hisi_hba->breakpoint, 0, s);
1644 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1645 memset(hisi_hba->sata_breakpoint, 0, s);
1647 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1649 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1651 struct device *dev = hisi_hba->dev;
1652 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1654 spin_lock_init(&hisi_hba->lock);
1655 for (i = 0; i < hisi_hba->n_phy; i++) {
1656 hisi_sas_phy_init(hisi_hba, i);
1657 hisi_hba->port[i].port_attached = 0;
1658 hisi_hba->port[i].id = -1;
1661 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1662 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1663 hisi_hba->devices[i].device_id = i;
1664 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1667 for (i = 0; i < hisi_hba->queue_count; i++) {
1668 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1669 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1671 /* Completion queue structure */
1673 cq->hisi_hba = hisi_hba;
1675 /* Delivery queue structure */
1676 spin_lock_init(&dq->lock);
1678 dq->hisi_hba = hisi_hba;
1680 /* Delivery queue */
1681 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1682 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1683 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1684 if (!hisi_hba->cmd_hdr[i])
1687 /* Completion queue */
1688 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1689 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1690 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1691 if (!hisi_hba->complete_hdr[i])
1695 s = sizeof(struct hisi_sas_slot_buf_table);
1696 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1697 if (!hisi_hba->buffer_pool)
1700 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1701 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1703 if (!hisi_hba->itct)
1706 memset(hisi_hba->itct, 0, s);
1708 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1709 sizeof(struct hisi_sas_slot),
1711 if (!hisi_hba->slot_info)
1714 s = max_command_entries * sizeof(struct hisi_sas_iost);
1715 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1717 if (!hisi_hba->iost)
1720 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1721 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1722 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1723 if (!hisi_hba->breakpoint)
1726 hisi_hba->slot_index_count = max_command_entries;
1727 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
1728 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1729 if (!hisi_hba->slot_index_tags)
1732 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1733 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1734 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1735 if (!hisi_hba->initial_fis)
1738 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1739 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1740 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1741 if (!hisi_hba->sata_breakpoint)
1743 hisi_sas_init_mem(hisi_hba);
1745 hisi_sas_slot_index_init(hisi_hba);
1747 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1748 if (!hisi_hba->wq) {
1749 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1757 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
1759 void hisi_sas_free(struct hisi_hba *hisi_hba)
1761 struct device *dev = hisi_hba->dev;
1762 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1764 for (i = 0; i < hisi_hba->queue_count; i++) {
1765 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1766 if (hisi_hba->cmd_hdr[i])
1767 dma_free_coherent(dev, s,
1768 hisi_hba->cmd_hdr[i],
1769 hisi_hba->cmd_hdr_dma[i]);
1771 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1772 if (hisi_hba->complete_hdr[i])
1773 dma_free_coherent(dev, s,
1774 hisi_hba->complete_hdr[i],
1775 hisi_hba->complete_hdr_dma[i]);
1778 dma_pool_destroy(hisi_hba->buffer_pool);
1780 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1782 dma_free_coherent(dev, s,
1783 hisi_hba->itct, hisi_hba->itct_dma);
1785 s = max_command_entries * sizeof(struct hisi_sas_iost);
1787 dma_free_coherent(dev, s,
1788 hisi_hba->iost, hisi_hba->iost_dma);
1790 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1791 if (hisi_hba->breakpoint)
1792 dma_free_coherent(dev, s,
1793 hisi_hba->breakpoint,
1794 hisi_hba->breakpoint_dma);
1797 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1798 if (hisi_hba->initial_fis)
1799 dma_free_coherent(dev, s,
1800 hisi_hba->initial_fis,
1801 hisi_hba->initial_fis_dma);
1803 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1804 if (hisi_hba->sata_breakpoint)
1805 dma_free_coherent(dev, s,
1806 hisi_hba->sata_breakpoint,
1807 hisi_hba->sata_breakpoint_dma);
1810 destroy_workqueue(hisi_hba->wq);
1812 EXPORT_SYMBOL_GPL(hisi_sas_free);
1814 void hisi_sas_rst_work_handler(struct work_struct *work)
1816 struct hisi_hba *hisi_hba =
1817 container_of(work, struct hisi_hba, rst_work);
1819 hisi_sas_controller_reset(hisi_hba);
1821 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
1823 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
1825 struct device *dev = hisi_hba->dev;
1826 struct platform_device *pdev = hisi_hba->platform_dev;
1827 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
1830 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1832 dev_err(dev, "could not get property sas-addr\n");
1838 * These properties are only required for platform device-based
1839 * controller with DT firmware.
1841 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1842 "hisilicon,sas-syscon");
1843 if (IS_ERR(hisi_hba->ctrl)) {
1844 dev_err(dev, "could not get syscon\n");
1848 if (device_property_read_u32(dev, "ctrl-reset-reg",
1849 &hisi_hba->ctrl_reset_reg)) {
1851 "could not get property ctrl-reset-reg\n");
1855 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1856 &hisi_hba->ctrl_reset_sts_reg)) {
1858 "could not get property ctrl-reset-sts-reg\n");
1862 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1863 &hisi_hba->ctrl_clock_ena_reg)) {
1865 "could not get property ctrl-clock-ena-reg\n");
1870 refclk = devm_clk_get(dev, NULL);
1872 dev_dbg(dev, "no ref clk property\n");
1874 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1876 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
1877 dev_err(dev, "could not get property phy-count\n");
1881 if (device_property_read_u32(dev, "queue-count",
1882 &hisi_hba->queue_count)) {
1883 dev_err(dev, "could not get property queue-count\n");
1889 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
1891 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1892 const struct hisi_sas_hw *hw)
1894 struct resource *res;
1895 struct Scsi_Host *shost;
1896 struct hisi_hba *hisi_hba;
1897 struct device *dev = &pdev->dev;
1899 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
1901 dev_err(dev, "scsi host alloc failed\n");
1904 hisi_hba = shost_priv(shost);
1906 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
1908 hisi_hba->dev = dev;
1909 hisi_hba->platform_dev = pdev;
1910 hisi_hba->shost = shost;
1911 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1913 timer_setup(&hisi_hba->timer, NULL, 0);
1915 if (hisi_sas_get_fw_info(hisi_hba) < 0)
1918 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1919 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1920 dev_err(dev, "No usable DMA addressing method\n");
1924 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1925 hisi_hba->regs = devm_ioremap_resource(dev, res);
1926 if (IS_ERR(hisi_hba->regs))
1929 if (hisi_sas_alloc(hisi_hba, shost)) {
1930 hisi_sas_free(hisi_hba);
1936 scsi_host_put(shost);
1937 dev_err(dev, "shost alloc failed\n");
1941 void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1945 for (i = 0; i < hisi_hba->n_phy; i++)
1946 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1950 EXPORT_SYMBOL_GPL(hisi_sas_init_add);
1952 int hisi_sas_probe(struct platform_device *pdev,
1953 const struct hisi_sas_hw *hw)
1955 struct Scsi_Host *shost;
1956 struct hisi_hba *hisi_hba;
1957 struct device *dev = &pdev->dev;
1958 struct asd_sas_phy **arr_phy;
1959 struct asd_sas_port **arr_port;
1960 struct sas_ha_struct *sha;
1961 int rc, phy_nr, port_nr, i;
1963 shost = hisi_sas_shost_alloc(pdev, hw);
1967 sha = SHOST_TO_SAS_HA(shost);
1968 hisi_hba = shost_priv(shost);
1969 platform_set_drvdata(pdev, sha);
1971 phy_nr = port_nr = hisi_hba->n_phy;
1973 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1974 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
1975 if (!arr_phy || !arr_port) {
1980 sha->sas_phy = arr_phy;
1981 sha->sas_port = arr_port;
1982 sha->lldd_ha = hisi_hba;
1984 shost->transportt = hisi_sas_stt;
1985 shost->max_id = HISI_SAS_MAX_DEVICES;
1986 shost->max_lun = ~0;
1987 shost->max_channel = 1;
1988 shost->max_cmd_len = 16;
1989 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
1990 shost->can_queue = hisi_hba->hw->max_command_entries;
1991 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
1993 sha->sas_ha_name = DRV_NAME;
1994 sha->dev = hisi_hba->dev;
1995 sha->lldd_module = THIS_MODULE;
1996 sha->sas_addr = &hisi_hba->sas_addr[0];
1997 sha->num_phys = hisi_hba->n_phy;
1998 sha->core.shost = hisi_hba->shost;
2000 for (i = 0; i < hisi_hba->n_phy; i++) {
2001 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2002 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2005 hisi_sas_init_add(hisi_hba);
2007 rc = scsi_add_host(shost, &pdev->dev);
2011 rc = sas_register_ha(sha);
2013 goto err_out_register_ha;
2015 rc = hisi_hba->hw->hw_init(hisi_hba);
2017 goto err_out_register_ha;
2019 scsi_scan_host(shost);
2023 err_out_register_ha:
2024 scsi_remove_host(shost);
2026 hisi_sas_free(hisi_hba);
2027 scsi_host_put(shost);
2030 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2032 int hisi_sas_remove(struct platform_device *pdev)
2034 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2035 struct hisi_hba *hisi_hba = sha->lldd_ha;
2036 struct Scsi_Host *shost = sha->core.shost;
2038 sas_unregister_ha(sha);
2039 sas_remove_host(sha->core.shost);
2041 hisi_sas_free(hisi_hba);
2042 scsi_host_put(shost);
2045 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2047 static __init int hisi_sas_init(void)
2049 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2056 static __exit void hisi_sas_exit(void)
2058 sas_release_transport(hisi_sas_stt);
2061 module_init(hisi_sas_init);
2062 module_exit(hisi_sas_exit);
2064 MODULE_LICENSE("GPL");
2065 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2066 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2067 MODULE_ALIAS("platform:" DRV_NAME);