2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
13 #include "../libsas/sas_internal.h"
14 #define DRV_NAME "hisi_sas"
16 #define DEV_IS_GONE(dev) \
17 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
19 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
20 u8 *lun, struct hisi_sas_tmf_task *tmf);
22 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
23 struct domain_device *device,
24 int abort_flag, int tag);
25 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
26 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
28 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
29 struct domain_device *device);
30 static void hisi_sas_dev_gone(struct domain_device *device);
32 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
34 switch (fis->command) {
35 case ATA_CMD_FPDMA_WRITE:
36 case ATA_CMD_FPDMA_READ:
37 case ATA_CMD_FPDMA_RECV:
38 case ATA_CMD_FPDMA_SEND:
39 case ATA_CMD_NCQ_NON_DATA:
40 return HISI_SAS_SATA_PROTOCOL_FPDMA;
42 case ATA_CMD_DOWNLOAD_MICRO:
44 case ATA_CMD_PMP_READ:
45 case ATA_CMD_READ_LOG_EXT:
46 case ATA_CMD_PIO_READ:
47 case ATA_CMD_PIO_READ_EXT:
48 case ATA_CMD_PMP_WRITE:
49 case ATA_CMD_WRITE_LOG_EXT:
50 case ATA_CMD_PIO_WRITE:
51 case ATA_CMD_PIO_WRITE_EXT:
52 return HISI_SAS_SATA_PROTOCOL_PIO;
55 case ATA_CMD_DOWNLOAD_MICRO_DMA:
56 case ATA_CMD_PMP_READ_DMA:
57 case ATA_CMD_PMP_WRITE_DMA:
59 case ATA_CMD_READ_EXT:
60 case ATA_CMD_READ_LOG_DMA_EXT:
61 case ATA_CMD_READ_STREAM_DMA_EXT:
62 case ATA_CMD_TRUSTED_RCV_DMA:
63 case ATA_CMD_TRUSTED_SND_DMA:
65 case ATA_CMD_WRITE_EXT:
66 case ATA_CMD_WRITE_FUA_EXT:
67 case ATA_CMD_WRITE_QUEUED:
68 case ATA_CMD_WRITE_LOG_DMA_EXT:
69 case ATA_CMD_WRITE_STREAM_DMA_EXT:
70 case ATA_CMD_ZAC_MGMT_IN:
71 return HISI_SAS_SATA_PROTOCOL_DMA;
73 case ATA_CMD_CHK_POWER:
74 case ATA_CMD_DEV_RESET:
77 case ATA_CMD_FLUSH_EXT:
79 case ATA_CMD_VERIFY_EXT:
80 case ATA_CMD_SET_FEATURES:
82 case ATA_CMD_STANDBYNOW1:
83 case ATA_CMD_ZAC_MGMT_OUT:
84 return HISI_SAS_SATA_PROTOCOL_NONDATA;
87 switch (fis->features) {
88 case ATA_SET_MAX_PASSWD:
89 case ATA_SET_MAX_LOCK:
90 return HISI_SAS_SATA_PROTOCOL_PIO;
92 case ATA_SET_MAX_PASSWD_DMA:
93 case ATA_SET_MAX_UNLOCK_DMA:
94 return HISI_SAS_SATA_PROTOCOL_DMA;
97 return HISI_SAS_SATA_PROTOCOL_NONDATA;
102 if (direction == DMA_NONE)
103 return HISI_SAS_SATA_PROTOCOL_NONDATA;
104 return HISI_SAS_SATA_PROTOCOL_PIO;
108 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
110 void hisi_sas_sata_done(struct sas_task *task,
111 struct hisi_sas_slot *slot)
113 struct task_status_struct *ts = &task->task_status;
114 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
115 struct hisi_sas_status_buffer *status_buf =
116 hisi_sas_status_buf_addr_mem(slot);
117 u8 *iu = &status_buf->iu[0];
118 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
120 resp->frame_len = sizeof(struct dev_to_host_fis);
121 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
123 ts->buf_valid_size = sizeof(*resp);
125 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
127 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
129 struct ata_queued_cmd *qc = task->uldd_task;
132 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
133 qc->tf.command == ATA_CMD_FPDMA_READ) {
140 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
143 * This function assumes linkrate mask fits in 8 bits, which it
144 * does for all HW versions supported.
146 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
151 max -= SAS_LINK_RATE_1_5_GBPS;
152 for (i = 0; i <= max; i++)
153 rate |= 1 << (i * 2);
156 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
158 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
160 return device->port->ha->lldd_ha;
163 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
165 return container_of(sas_port, struct hisi_sas_port, sas_port);
167 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
169 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
173 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
174 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
176 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
178 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
180 void *bitmap = hisi_hba->slot_index_tags;
182 clear_bit(slot_idx, bitmap);
185 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
187 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
190 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
192 void *bitmap = hisi_hba->slot_index_tags;
194 set_bit(slot_idx, bitmap);
197 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
200 void *bitmap = hisi_hba->slot_index_tags;
202 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
203 hisi_hba->last_slot_index + 1);
204 if (index >= hisi_hba->slot_index_count) {
205 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
207 if (index >= hisi_hba->slot_index_count)
208 return -SAS_QUEUE_FULL;
210 hisi_sas_slot_index_set(hisi_hba, index);
212 hisi_hba->last_slot_index = index;
217 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
221 for (i = 0; i < hisi_hba->slot_index_count; ++i)
222 hisi_sas_slot_index_clear(hisi_hba, i);
225 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
226 struct hisi_sas_slot *slot)
228 struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
232 struct device *dev = hisi_hba->dev;
234 if (!task->lldd_task)
237 task->lldd_task = NULL;
239 if (!sas_protocol_ata(task->task_proto))
241 dma_unmap_sg(dev, task->scatter,
247 spin_lock_irqsave(&dq->lock, flags);
248 list_del_init(&slot->entry);
249 spin_unlock_irqrestore(&dq->lock, flags);
251 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
253 spin_lock_irqsave(&hisi_hba->lock, flags);
254 hisi_sas_slot_index_free(hisi_hba, slot->idx);
255 spin_unlock_irqrestore(&hisi_hba->lock, flags);
257 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
259 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
260 struct hisi_sas_slot *slot)
262 hisi_hba->hw->prep_smp(hisi_hba, slot);
265 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
266 struct hisi_sas_slot *slot)
268 hisi_hba->hw->prep_ssp(hisi_hba, slot);
271 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
272 struct hisi_sas_slot *slot)
274 hisi_hba->hw->prep_stp(hisi_hba, slot);
277 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
278 struct hisi_sas_slot *slot,
279 int device_id, int abort_flag, int tag_to_abort)
281 hisi_hba->hw->prep_abort(hisi_hba, slot,
282 device_id, abort_flag, tag_to_abort);
285 static int hisi_sas_task_prep(struct sas_task *task,
286 struct hisi_sas_dq **dq_pointer,
287 bool is_tmf, struct hisi_sas_tmf_task *tmf,
290 struct domain_device *device = task->dev;
291 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
292 struct hisi_sas_device *sas_dev = device->lldd_dev;
293 struct hisi_sas_port *port;
294 struct hisi_sas_slot *slot;
295 struct hisi_sas_cmd_hdr *cmd_hdr_base;
296 struct asd_sas_port *sas_port = device->port;
297 struct device *dev = hisi_hba->dev;
298 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
299 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
300 struct hisi_sas_dq *dq;
305 struct task_status_struct *ts = &task->task_status;
307 ts->resp = SAS_TASK_UNDELIVERED;
308 ts->stat = SAS_PHY_DOWN;
310 * libsas will use dev->port, should
311 * not call task_done for sata
313 if (device->dev_type != SAS_SATA_DEV)
314 task->task_done(task);
318 if (DEV_IS_GONE(sas_dev)) {
320 dev_info(dev, "task prep: device %d not ready\n",
323 dev_info(dev, "task prep: device %016llx not ready\n",
324 SAS_ADDR(device->sas_addr));
329 *dq_pointer = dq = sas_dev->dq;
331 port = to_hisi_sas_port(sas_port);
332 if (port && !port->port_attached) {
333 dev_info(dev, "task prep: %s port%d not attach device\n",
334 (dev_is_sata(device)) ?
341 if (!sas_protocol_ata(task->task_proto)) {
342 unsigned int req_len, resp_len;
344 if (task->num_scatter) {
345 n_elem = dma_map_sg(dev, task->scatter,
346 task->num_scatter, task->data_dir);
351 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
352 n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
358 req_len = sg_dma_len(&task->smp_task.smp_req);
361 goto err_out_dma_unmap;
363 n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
367 goto err_out_dma_unmap;
369 resp_len = sg_dma_len(&task->smp_task.smp_resp);
370 if (resp_len & 0x3) {
372 goto err_out_dma_unmap;
376 n_elem = task->num_scatter;
378 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
379 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
382 goto err_out_dma_unmap;
385 spin_lock_irqsave(&hisi_hba->lock, flags);
386 if (hisi_hba->hw->slot_index_alloc)
387 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
390 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
391 spin_unlock_irqrestore(&hisi_hba->lock, flags);
393 goto err_out_dma_unmap;
395 slot = &hisi_hba->slot_info[slot_idx];
397 spin_lock_irqsave(&dq->lock, flags);
398 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
399 if (wr_q_index < 0) {
400 spin_unlock_irqrestore(&dq->lock, flags);
405 list_add_tail(&slot->delivery, &dq->list);
406 list_add_tail(&slot->entry, &sas_dev->list);
407 spin_unlock_irqrestore(&dq->lock, flags);
409 dlvry_queue = dq->id;
410 dlvry_queue_slot = wr_q_index;
412 slot->n_elem = n_elem;
413 slot->dlvry_queue = dlvry_queue;
414 slot->dlvry_queue_slot = dlvry_queue_slot;
415 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
416 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
420 slot->is_internal = is_tmf;
421 task->lldd_task = slot;
423 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
424 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
425 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
427 switch (task->task_proto) {
428 case SAS_PROTOCOL_SMP:
429 hisi_sas_task_prep_smp(hisi_hba, slot);
431 case SAS_PROTOCOL_SSP:
432 hisi_sas_task_prep_ssp(hisi_hba, slot);
434 case SAS_PROTOCOL_SATA:
435 case SAS_PROTOCOL_STP:
436 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
437 hisi_sas_task_prep_ata(hisi_hba, slot);
440 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
445 spin_lock_irqsave(&task->task_state_lock, flags);
446 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
447 spin_unlock_irqrestore(&task->task_state_lock, flags);
450 WRITE_ONCE(slot->ready, 1);
455 spin_lock_irqsave(&hisi_hba->lock, flags);
456 hisi_sas_slot_index_free(hisi_hba, slot_idx);
457 spin_unlock_irqrestore(&hisi_hba->lock, flags);
459 if (!sas_protocol_ata(task->task_proto)) {
460 if (task->num_scatter) {
461 dma_unmap_sg(dev, task->scatter, task->num_scatter,
463 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
465 dma_unmap_sg(dev, &task->smp_task.smp_req,
468 dma_unmap_sg(dev, &task->smp_task.smp_resp,
473 dev_err(dev, "task prep: failed[%d]!\n", rc);
477 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
478 bool is_tmf, struct hisi_sas_tmf_task *tmf)
483 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
484 struct device *dev = hisi_hba->dev;
485 struct hisi_sas_dq *dq = NULL;
487 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
489 * For IOs from upper layer, it may already disable preempt
490 * in the IO path, if disable preempt again in down(),
491 * function schedule() will report schedule_bug(), so check
492 * preemptible() before goto down().
497 down(&hisi_hba->sem);
501 /* protect task_prep and start_delivery sequence */
502 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
504 dev_err(dev, "task exec: failed[%d]!\n", rc);
507 spin_lock_irqsave(&dq->lock, flags);
508 hisi_hba->hw->start_delivery(dq);
509 spin_unlock_irqrestore(&dq->lock, flags);
515 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
517 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
518 struct asd_sas_phy *sas_phy = &phy->sas_phy;
519 struct sas_ha_struct *sas_ha;
521 if (!phy->phy_attached)
524 sas_ha = &hisi_hba->sha;
525 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
528 struct sas_phy *sphy = sas_phy->phy;
530 sphy->negotiated_linkrate = sas_phy->linkrate;
531 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
532 sphy->maximum_linkrate_hw =
533 hisi_hba->hw->phy_get_max_linkrate();
534 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
535 sphy->minimum_linkrate = phy->minimum_linkrate;
537 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
538 sphy->maximum_linkrate = phy->maximum_linkrate;
541 if (phy->phy_type & PORT_TYPE_SAS) {
542 struct sas_identify_frame *id;
544 id = (struct sas_identify_frame *)phy->frame_rcvd;
545 id->dev_type = phy->identify.device_type;
546 id->initiator_bits = SAS_PROTOCOL_ALL;
547 id->target_bits = phy->identify.target_port_protocols;
548 } else if (phy->phy_type & PORT_TYPE_SATA) {
552 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
553 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
556 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
558 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
559 struct hisi_sas_device *sas_dev = NULL;
561 int last = hisi_hba->last_dev_id;
562 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
565 spin_lock_irqsave(&hisi_hba->lock, flags);
566 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
567 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
568 int queue = i % hisi_hba->queue_count;
569 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
571 hisi_hba->devices[i].device_id = i;
572 sas_dev = &hisi_hba->devices[i];
573 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
574 sas_dev->dev_type = device->dev_type;
575 sas_dev->hisi_hba = hisi_hba;
576 sas_dev->sas_device = device;
578 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
583 hisi_hba->last_dev_id = i;
584 spin_unlock_irqrestore(&hisi_hba->lock, flags);
589 #define HISI_SAS_SRST_ATA_DISK_CNT 3
590 static int hisi_sas_init_device(struct domain_device *device)
592 int rc = TMF_RESP_FUNC_COMPLETE;
594 struct hisi_sas_tmf_task tmf_task;
595 int retry = HISI_SAS_SRST_ATA_DISK_CNT;
596 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
598 switch (device->dev_type) {
600 int_to_scsilun(0, &lun);
602 tmf_task.tmf = TMF_CLEAR_TASK_SET;
603 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
605 if (rc == TMF_RESP_FUNC_COMPLETE)
606 hisi_sas_release_task(hisi_hba, device);
610 case SAS_SATA_PM_PORT:
611 case SAS_SATA_PENDING:
612 while (retry-- > 0) {
613 rc = hisi_sas_softreset_ata_disk(device);
625 static int hisi_sas_dev_found(struct domain_device *device)
627 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
628 struct domain_device *parent_dev = device->parent;
629 struct hisi_sas_device *sas_dev;
630 struct device *dev = hisi_hba->dev;
633 if (hisi_hba->hw->alloc_dev)
634 sas_dev = hisi_hba->hw->alloc_dev(device);
636 sas_dev = hisi_sas_alloc_dev(device);
638 dev_err(dev, "fail alloc dev: max support %d devices\n",
639 HISI_SAS_MAX_DEVICES);
643 device->lldd_dev = sas_dev;
644 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
646 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
648 u8 phy_num = parent_dev->ex_dev.num_phys;
651 for (phy_no = 0; phy_no < phy_num; phy_no++) {
652 phy = &parent_dev->ex_dev.ex_phy[phy_no];
653 if (SAS_ADDR(phy->attached_sas_addr) ==
654 SAS_ADDR(device->sas_addr))
658 if (phy_no == phy_num) {
659 dev_info(dev, "dev found: no attached "
660 "dev:%016llx at ex:%016llx\n",
661 SAS_ADDR(device->sas_addr),
662 SAS_ADDR(parent_dev->sas_addr));
668 dev_info(dev, "dev[%d:%x] found\n",
669 sas_dev->device_id, sas_dev->dev_type);
671 rc = hisi_sas_init_device(device);
677 hisi_sas_dev_gone(device);
681 int hisi_sas_slave_configure(struct scsi_device *sdev)
683 struct domain_device *dev = sdev_to_domain_dev(sdev);
684 int ret = sas_slave_configure(sdev);
688 if (!dev_is_sata(dev))
689 sas_change_queue_depth(sdev, 64);
693 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
695 void hisi_sas_scan_start(struct Scsi_Host *shost)
697 struct hisi_hba *hisi_hba = shost_priv(shost);
699 hisi_hba->hw->phys_init(hisi_hba);
701 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
703 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
705 struct hisi_hba *hisi_hba = shost_priv(shost);
706 struct sas_ha_struct *sha = &hisi_hba->sha;
708 /* Wait for PHY up interrupt to occur */
715 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
717 static void hisi_sas_phyup_work(struct work_struct *work)
719 struct hisi_sas_phy *phy =
720 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
721 struct hisi_hba *hisi_hba = phy->hisi_hba;
722 struct asd_sas_phy *sas_phy = &phy->sas_phy;
723 int phy_no = sas_phy->id;
725 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
726 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
727 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
730 static void hisi_sas_linkreset_work(struct work_struct *work)
732 struct hisi_sas_phy *phy =
733 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
734 struct asd_sas_phy *sas_phy = &phy->sas_phy;
736 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
739 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
740 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
741 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
744 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
745 enum hisi_sas_phy_event event)
747 struct hisi_hba *hisi_hba = phy->hisi_hba;
749 if (WARN_ON(event >= HISI_PHYES_NUM))
752 return queue_work(hisi_hba->wq, &phy->works[event]);
754 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
756 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
758 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
759 struct asd_sas_phy *sas_phy = &phy->sas_phy;
762 phy->hisi_hba = hisi_hba;
764 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
765 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
766 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
767 sas_phy->class = SAS;
768 sas_phy->iproto = SAS_PROTOCOL_ALL;
770 sas_phy->type = PHY_TYPE_PHYSICAL;
771 sas_phy->role = PHY_ROLE_INITIATOR;
772 sas_phy->oob_mode = OOB_NOT_CONNECTED;
773 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
774 sas_phy->id = phy_no;
775 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
776 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
777 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
778 sas_phy->lldd_phy = phy;
780 for (i = 0; i < HISI_PHYES_NUM; i++)
781 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
783 spin_lock_init(&phy->lock);
786 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
788 struct sas_ha_struct *sas_ha = sas_phy->ha;
789 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
790 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
791 struct asd_sas_port *sas_port = sas_phy->port;
792 struct hisi_sas_port *port;
798 port = to_hisi_sas_port(sas_port);
799 spin_lock_irqsave(&hisi_hba->lock, flags);
800 port->port_attached = 1;
801 port->id = phy->port_id;
803 sas_port->lldd_port = port;
804 spin_unlock_irqrestore(&hisi_hba->lock, flags);
807 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
808 struct hisi_sas_slot *slot)
812 struct task_status_struct *ts;
814 ts = &task->task_status;
816 ts->resp = SAS_TASK_COMPLETE;
817 ts->stat = SAS_ABORTED_TASK;
818 spin_lock_irqsave(&task->task_state_lock, flags);
819 task->task_state_flags &=
820 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
821 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
822 task->task_state_flags |= SAS_TASK_STATE_DONE;
823 spin_unlock_irqrestore(&task->task_state_lock, flags);
826 hisi_sas_slot_task_free(hisi_hba, task, slot);
829 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
830 struct domain_device *device)
832 struct hisi_sas_slot *slot, *slot2;
833 struct hisi_sas_device *sas_dev = device->lldd_dev;
835 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
836 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
839 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
841 struct hisi_sas_device *sas_dev;
842 struct domain_device *device;
845 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
846 sas_dev = &hisi_hba->devices[i];
847 device = sas_dev->sas_device;
849 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
853 hisi_sas_release_task(hisi_hba, device);
856 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
858 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
859 struct domain_device *device)
861 if (hisi_hba->hw->dereg_device)
862 hisi_hba->hw->dereg_device(hisi_hba, device);
865 static void hisi_sas_dev_gone(struct domain_device *device)
867 struct hisi_sas_device *sas_dev = device->lldd_dev;
868 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
869 struct device *dev = hisi_hba->dev;
871 dev_info(dev, "dev[%d:%x] is gone\n",
872 sas_dev->device_id, sas_dev->dev_type);
874 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
875 hisi_sas_internal_task_abort(hisi_hba, device,
876 HISI_SAS_INT_ABT_DEV, 0);
878 hisi_sas_dereg_device(hisi_hba, device);
880 down(&hisi_hba->sem);
881 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
883 device->lldd_dev = NULL;
886 if (hisi_hba->hw->free_device)
887 hisi_hba->hw->free_device(sas_dev);
888 sas_dev->dev_type = SAS_PHY_UNUSED;
891 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
893 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
896 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
897 struct sas_phy_linkrates *r)
899 struct sas_phy_linkrates _r;
901 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
902 struct asd_sas_phy *sas_phy = &phy->sas_phy;
903 enum sas_linkrate min, max;
905 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
908 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
909 max = sas_phy->phy->maximum_linkrate;
910 min = r->minimum_linkrate;
911 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
912 max = r->maximum_linkrate;
913 min = sas_phy->phy->minimum_linkrate;
917 _r.maximum_linkrate = max;
918 _r.minimum_linkrate = min;
920 sas_phy->phy->maximum_linkrate = max;
921 sas_phy->phy->minimum_linkrate = min;
923 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
925 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
926 hisi_hba->hw->phy_start(hisi_hba, phy_no);
931 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
934 struct sas_ha_struct *sas_ha = sas_phy->ha;
935 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
936 int phy_no = sas_phy->id;
939 case PHY_FUNC_HARD_RESET:
940 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
943 case PHY_FUNC_LINK_RESET:
944 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
946 hisi_hba->hw->phy_start(hisi_hba, phy_no);
949 case PHY_FUNC_DISABLE:
950 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
953 case PHY_FUNC_SET_LINK_RATE:
954 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
955 case PHY_FUNC_GET_EVENTS:
956 if (hisi_hba->hw->get_events) {
957 hisi_hba->hw->get_events(hisi_hba, phy_no);
961 case PHY_FUNC_RELEASE_SPINUP_HOLD:
968 static void hisi_sas_task_done(struct sas_task *task)
970 del_timer(&task->slow_task->timer);
971 complete(&task->slow_task->completion);
974 static void hisi_sas_tmf_timedout(struct timer_list *t)
976 struct sas_task_slow *slow = from_timer(slow, t, timer);
977 struct sas_task *task = slow->task;
979 bool is_completed = true;
981 spin_lock_irqsave(&task->task_state_lock, flags);
982 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
983 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
984 is_completed = false;
986 spin_unlock_irqrestore(&task->task_state_lock, flags);
989 complete(&task->slow_task->completion);
992 #define TASK_TIMEOUT 20
994 #define INTERNAL_ABORT_TIMEOUT 6
995 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
996 void *parameter, u32 para_len,
997 struct hisi_sas_tmf_task *tmf)
999 struct hisi_sas_device *sas_dev = device->lldd_dev;
1000 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1001 struct device *dev = hisi_hba->dev;
1002 struct sas_task *task;
1005 for (retry = 0; retry < TASK_RETRY; retry++) {
1006 task = sas_alloc_slow_task(GFP_KERNEL);
1011 task->task_proto = device->tproto;
1013 if (dev_is_sata(device)) {
1014 task->ata_task.device_control_reg_update = 1;
1015 memcpy(&task->ata_task.fis, parameter, para_len);
1017 memcpy(&task->ssp_task, parameter, para_len);
1019 task->task_done = hisi_sas_task_done;
1021 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1022 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1023 add_timer(&task->slow_task->timer);
1025 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1028 del_timer(&task->slow_task->timer);
1029 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1034 wait_for_completion(&task->slow_task->completion);
1035 res = TMF_RESP_FUNC_FAILED;
1036 /* Even TMF timed out, return direct. */
1037 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1038 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1039 struct hisi_sas_slot *slot = task->lldd_task;
1041 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1043 struct hisi_sas_cq *cq =
1044 &hisi_hba->cq[slot->dlvry_queue];
1046 * flush tasklet to avoid free'ing task
1047 * before using task in IO completion
1049 tasklet_kill(&cq->tasklet);
1055 dev_err(dev, "abort tmf: TMF task timeout\n");
1058 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1059 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1060 res = TMF_RESP_FUNC_COMPLETE;
1064 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1065 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1066 res = TMF_RESP_FUNC_SUCC;
1070 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1071 task->task_status.stat == SAS_DATA_UNDERRUN) {
1072 /* no error, but return the number of bytes of
1075 dev_warn(dev, "abort tmf: task to dev %016llx "
1076 "resp: 0x%x sts 0x%x underrun\n",
1077 SAS_ADDR(device->sas_addr),
1078 task->task_status.resp,
1079 task->task_status.stat);
1080 res = task->task_status.residual;
1084 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1085 task->task_status.stat == SAS_DATA_OVERRUN) {
1086 dev_warn(dev, "abort tmf: blocked task error\n");
1091 dev_warn(dev, "abort tmf: task to dev "
1092 "%016llx resp: 0x%x status 0x%x\n",
1093 SAS_ADDR(device->sas_addr), task->task_status.resp,
1094 task->task_status.stat);
1095 sas_free_task(task);
1099 if (retry == TASK_RETRY)
1100 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1101 sas_free_task(task);
1105 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1106 bool reset, int pmp, u8 *fis)
1108 struct ata_taskfile tf;
1110 ata_tf_init(dev, &tf);
1114 tf.ctl &= ~ATA_SRST;
1115 tf.command = ATA_CMD_DEV_RESET;
1116 ata_tf_to_fis(&tf, pmp, 0, fis);
1119 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1122 struct ata_port *ap = device->sata_dev.ap;
1123 struct ata_link *link;
1124 int rc = TMF_RESP_FUNC_FAILED;
1125 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1126 struct device *dev = hisi_hba->dev;
1127 int s = sizeof(struct host_to_dev_fis);
1129 ata_for_each_link(link, ap, EDGE) {
1130 int pmp = sata_srst_pmp(link);
1132 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1133 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1134 if (rc != TMF_RESP_FUNC_COMPLETE)
1138 if (rc == TMF_RESP_FUNC_COMPLETE) {
1139 ata_for_each_link(link, ap, EDGE) {
1140 int pmp = sata_srst_pmp(link);
1142 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1143 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1145 if (rc != TMF_RESP_FUNC_COMPLETE)
1146 dev_err(dev, "ata disk de-reset failed\n");
1149 dev_err(dev, "ata disk reset failed\n");
1152 if (rc == TMF_RESP_FUNC_COMPLETE)
1153 hisi_sas_release_task(hisi_hba, device);
1158 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1159 u8 *lun, struct hisi_sas_tmf_task *tmf)
1161 struct sas_ssp_task ssp_task;
1163 if (!(device->tproto & SAS_PROTOCOL_SSP))
1164 return TMF_RESP_FUNC_ESUPP;
1166 memcpy(ssp_task.LUN, lun, 8);
1168 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1169 sizeof(ssp_task), tmf);
1172 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1174 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1177 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1178 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1179 struct domain_device *device = sas_dev->sas_device;
1180 struct asd_sas_port *sas_port;
1181 struct hisi_sas_port *port;
1182 struct hisi_sas_phy *phy = NULL;
1183 struct asd_sas_phy *sas_phy;
1185 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1186 || !device || !device->port)
1189 sas_port = device->port;
1190 port = to_hisi_sas_port(sas_port);
1192 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1193 if (state & BIT(sas_phy->id)) {
1194 phy = sas_phy->lldd_phy;
1199 port->id = phy->port_id;
1201 /* Update linkrate of directly attached device. */
1202 if (!device->parent)
1203 device->linkrate = phy->sas_phy.linkrate;
1205 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1211 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1214 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1215 struct asd_sas_port *_sas_port = NULL;
1218 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1219 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1220 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1221 struct asd_sas_port *sas_port = sas_phy->port;
1222 bool do_port_check = !!(_sas_port != sas_port);
1224 if (!sas_phy->phy->enabled)
1227 /* Report PHY state change to libsas */
1228 if (state & BIT(phy_no)) {
1229 if (do_port_check && sas_port && sas_port->port_dev) {
1230 struct domain_device *dev = sas_port->port_dev;
1232 _sas_port = sas_port;
1234 if (DEV_IS_EXPANDER(dev->dev_type))
1235 sas_ha->notify_port_event(sas_phy,
1236 PORTE_BROADCAST_RCVD);
1238 } else if (old_state & (1 << phy_no))
1239 /* PHY down but was up before */
1240 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1245 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1247 struct hisi_sas_device *sas_dev;
1248 struct domain_device *device;
1251 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1252 sas_dev = &hisi_hba->devices[i];
1253 device = sas_dev->sas_device;
1255 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1258 hisi_sas_init_device(device);
1262 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1263 struct asd_sas_port *sas_port,
1264 struct domain_device *device)
1266 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1267 struct ata_port *ap = device->sata_dev.ap;
1268 struct device *dev = hisi_hba->dev;
1269 int s = sizeof(struct host_to_dev_fis);
1270 int rc = TMF_RESP_FUNC_FAILED;
1271 struct asd_sas_phy *sas_phy;
1272 struct ata_link *link;
1276 state = hisi_hba->hw->get_phys_state(hisi_hba);
1277 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1278 if (!(state & BIT(sas_phy->id)))
1281 ata_for_each_link(link, ap, EDGE) {
1282 int pmp = sata_srst_pmp(link);
1284 tmf_task.phy_id = sas_phy->id;
1285 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1286 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1288 if (rc != TMF_RESP_FUNC_COMPLETE) {
1289 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1297 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1299 struct device *dev = hisi_hba->dev;
1302 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1303 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1304 struct domain_device *device = sas_dev->sas_device;
1306 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1309 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1310 HISI_SAS_INT_ABT_DEV, 0);
1312 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1315 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1316 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1317 struct asd_sas_port *sas_port = &port->sas_port;
1318 struct domain_device *port_dev = sas_port->port_dev;
1319 struct domain_device *device;
1321 if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
1324 /* Try to find a SATA device */
1325 list_for_each_entry(device, &sas_port->dev_list,
1327 if (dev_is_sata(device)) {
1328 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1337 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1339 struct Scsi_Host *shost = hisi_hba->shost;
1341 down(&hisi_hba->sem);
1342 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1344 scsi_block_requests(shost);
1345 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1347 if (timer_pending(&hisi_hba->timer))
1348 del_timer_sync(&hisi_hba->timer);
1350 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1352 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1354 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1356 struct Scsi_Host *shost = hisi_hba->shost;
1359 /* Init and wait for PHYs to come up and all libsas event finished. */
1360 hisi_hba->hw->phys_init(hisi_hba);
1362 hisi_sas_refresh_port_id(hisi_hba);
1363 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1366 if (hisi_hba->reject_stp_links_msk)
1367 hisi_sas_terminate_stp_reject(hisi_hba);
1368 hisi_sas_reset_init_all_devices(hisi_hba);
1369 scsi_unblock_requests(shost);
1370 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1372 state = hisi_hba->hw->get_phys_state(hisi_hba);
1373 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state, state);
1375 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1377 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1379 struct device *dev = hisi_hba->dev;
1380 struct Scsi_Host *shost = hisi_hba->shost;
1383 if (!hisi_hba->hw->soft_reset)
1386 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1389 dev_info(dev, "controller resetting...\n");
1390 hisi_sas_controller_reset_prepare(hisi_hba);
1392 rc = hisi_hba->hw->soft_reset(hisi_hba);
1394 dev_warn(dev, "controller reset failed (%d)\n", rc);
1395 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1397 scsi_unblock_requests(shost);
1398 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1402 hisi_sas_controller_reset_done(hisi_hba);
1403 dev_info(dev, "controller reset complete\n");
1408 static int hisi_sas_abort_task(struct sas_task *task)
1410 struct scsi_lun lun;
1411 struct hisi_sas_tmf_task tmf_task;
1412 struct domain_device *device = task->dev;
1413 struct hisi_sas_device *sas_dev = device->lldd_dev;
1414 struct hisi_hba *hisi_hba;
1416 int rc = TMF_RESP_FUNC_FAILED;
1417 unsigned long flags;
1420 return TMF_RESP_FUNC_FAILED;
1422 hisi_hba = dev_to_hisi_hba(task->dev);
1423 dev = hisi_hba->dev;
1425 spin_lock_irqsave(&task->task_state_lock, flags);
1426 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1427 struct hisi_sas_slot *slot = task->lldd_task;
1428 struct hisi_sas_cq *cq;
1432 * flush tasklet to avoid free'ing task
1433 * before using task in IO completion
1435 cq = &hisi_hba->cq[slot->dlvry_queue];
1436 tasklet_kill(&cq->tasklet);
1438 spin_unlock_irqrestore(&task->task_state_lock, flags);
1439 rc = TMF_RESP_FUNC_COMPLETE;
1442 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1443 spin_unlock_irqrestore(&task->task_state_lock, flags);
1445 sas_dev->dev_status = HISI_SAS_DEV_EH;
1446 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1447 struct scsi_cmnd *cmnd = task->uldd_task;
1448 struct hisi_sas_slot *slot = task->lldd_task;
1449 u32 tag = slot->idx;
1452 int_to_scsilun(cmnd->device->lun, &lun);
1453 tmf_task.tmf = TMF_ABORT_TASK;
1454 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1456 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1459 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1460 HISI_SAS_INT_ABT_CMD, tag);
1462 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1463 return TMF_RESP_FUNC_FAILED;
1467 * If the TMF finds that the IO is not in the device and also
1468 * the internal abort does not succeed, then it is safe to
1470 * Note: if the internal abort succeeds then the slot
1471 * will have already been completed
1473 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1474 if (task->lldd_task)
1475 hisi_sas_do_release_task(hisi_hba, task, slot);
1477 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1478 task->task_proto & SAS_PROTOCOL_STP) {
1479 if (task->dev->dev_type == SAS_SATA_DEV) {
1480 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1481 HISI_SAS_INT_ABT_DEV, 0);
1483 dev_err(dev, "abort task: internal abort failed\n");
1486 hisi_sas_dereg_device(hisi_hba, device);
1487 rc = hisi_sas_softreset_ata_disk(device);
1489 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1491 struct hisi_sas_slot *slot = task->lldd_task;
1492 u32 tag = slot->idx;
1493 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1495 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1496 HISI_SAS_INT_ABT_CMD, tag);
1497 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1500 * flush tasklet to avoid free'ing task
1501 * before using task in IO completion
1503 tasklet_kill(&cq->tasklet);
1509 if (rc != TMF_RESP_FUNC_COMPLETE)
1510 dev_notice(dev, "abort task: rc=%d\n", rc);
1514 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1516 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1517 struct device *dev = hisi_hba->dev;
1518 struct hisi_sas_tmf_task tmf_task;
1519 int rc = TMF_RESP_FUNC_FAILED;
1521 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1522 HISI_SAS_INT_ABT_DEV, 0);
1524 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1525 return TMF_RESP_FUNC_FAILED;
1527 hisi_sas_dereg_device(hisi_hba, device);
1529 tmf_task.tmf = TMF_ABORT_TASK_SET;
1530 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1532 if (rc == TMF_RESP_FUNC_COMPLETE)
1533 hisi_sas_release_task(hisi_hba, device);
1538 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1540 int rc = TMF_RESP_FUNC_FAILED;
1541 struct hisi_sas_tmf_task tmf_task;
1543 tmf_task.tmf = TMF_CLEAR_ACA;
1544 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1549 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1551 struct sas_phy *local_phy = sas_get_local_phy(device);
1552 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1553 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1554 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1555 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1556 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
1557 struct hisi_sas_phy *phy = container_of(sas_phy,
1558 struct hisi_sas_phy, sas_phy);
1559 DECLARE_COMPLETION_ONSTACK(phyreset);
1561 if (scsi_is_sas_phy_local(local_phy)) {
1563 phy->reset_completion = &phyreset;
1566 rc = sas_phy_reset(local_phy, reset_type);
1567 sas_put_local_phy(local_phy);
1569 if (scsi_is_sas_phy_local(local_phy)) {
1570 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1571 unsigned long flags;
1573 spin_lock_irqsave(&phy->lock, flags);
1574 phy->reset_completion = NULL;
1576 spin_unlock_irqrestore(&phy->lock, flags);
1578 /* report PHY down if timed out */
1580 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1587 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1589 struct hisi_sas_device *sas_dev = device->lldd_dev;
1590 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1591 struct device *dev = hisi_hba->dev;
1592 int rc = TMF_RESP_FUNC_FAILED;
1594 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1595 return TMF_RESP_FUNC_FAILED;
1596 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1598 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1599 HISI_SAS_INT_ABT_DEV, 0);
1601 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1602 return TMF_RESP_FUNC_FAILED;
1604 hisi_sas_dereg_device(hisi_hba, device);
1606 rc = hisi_sas_debug_I_T_nexus_reset(device);
1608 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1609 hisi_sas_release_task(hisi_hba, device);
1614 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1616 struct hisi_sas_device *sas_dev = device->lldd_dev;
1617 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1618 struct device *dev = hisi_hba->dev;
1619 int rc = TMF_RESP_FUNC_FAILED;
1621 sas_dev->dev_status = HISI_SAS_DEV_EH;
1622 if (dev_is_sata(device)) {
1623 struct sas_phy *phy;
1625 /* Clear internal IO and then hardreset */
1626 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1627 HISI_SAS_INT_ABT_DEV, 0);
1629 dev_err(dev, "lu_reset: internal abort failed\n");
1632 hisi_sas_dereg_device(hisi_hba, device);
1634 phy = sas_get_local_phy(device);
1636 rc = sas_phy_reset(phy, 1);
1639 hisi_sas_release_task(hisi_hba, device);
1640 sas_put_local_phy(phy);
1642 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1644 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1645 HISI_SAS_INT_ABT_DEV, 0);
1647 dev_err(dev, "lu_reset: internal abort failed\n");
1650 hisi_sas_dereg_device(hisi_hba, device);
1652 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1653 if (rc == TMF_RESP_FUNC_COMPLETE)
1654 hisi_sas_release_task(hisi_hba, device);
1657 if (rc != TMF_RESP_FUNC_COMPLETE)
1658 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1659 sas_dev->device_id, rc);
1663 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1665 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1666 struct device *dev = hisi_hba->dev;
1667 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1670 queue_work(hisi_hba->wq, &r.work);
1671 wait_for_completion(r.completion);
1673 return TMF_RESP_FUNC_FAILED;
1675 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1676 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1677 struct domain_device *device = sas_dev->sas_device;
1679 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1680 DEV_IS_EXPANDER(device->dev_type))
1683 rc = hisi_sas_debug_I_T_nexus_reset(device);
1684 if (rc != TMF_RESP_FUNC_COMPLETE)
1685 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1686 sas_dev->device_id, rc);
1689 hisi_sas_release_tasks(hisi_hba);
1691 return TMF_RESP_FUNC_COMPLETE;
1694 static int hisi_sas_query_task(struct sas_task *task)
1696 struct scsi_lun lun;
1697 struct hisi_sas_tmf_task tmf_task;
1698 int rc = TMF_RESP_FUNC_FAILED;
1700 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1701 struct scsi_cmnd *cmnd = task->uldd_task;
1702 struct domain_device *device = task->dev;
1703 struct hisi_sas_slot *slot = task->lldd_task;
1704 u32 tag = slot->idx;
1706 int_to_scsilun(cmnd->device->lun, &lun);
1707 tmf_task.tmf = TMF_QUERY_TASK;
1708 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1710 rc = hisi_sas_debug_issue_ssp_tmf(device,
1714 /* The task is still in Lun, release it then */
1715 case TMF_RESP_FUNC_SUCC:
1716 /* The task is not in Lun or failed, reset the phy */
1717 case TMF_RESP_FUNC_FAILED:
1718 case TMF_RESP_FUNC_COMPLETE:
1721 rc = TMF_RESP_FUNC_FAILED;
1729 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1730 struct sas_task *task, int abort_flag,
1733 struct domain_device *device = task->dev;
1734 struct hisi_sas_device *sas_dev = device->lldd_dev;
1735 struct device *dev = hisi_hba->dev;
1736 struct hisi_sas_port *port;
1737 struct hisi_sas_slot *slot;
1738 struct asd_sas_port *sas_port = device->port;
1739 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1740 struct hisi_sas_dq *dq = sas_dev->dq;
1741 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1742 unsigned long flags, flags_dq = 0;
1745 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1751 port = to_hisi_sas_port(sas_port);
1753 /* simply get a slot and send abort command */
1754 spin_lock_irqsave(&hisi_hba->lock, flags);
1755 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1757 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1760 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1762 slot = &hisi_hba->slot_info[slot_idx];
1764 spin_lock_irqsave(&dq->lock, flags_dq);
1765 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1766 if (wr_q_index < 0) {
1767 spin_unlock_irqrestore(&dq->lock, flags_dq);
1771 list_add_tail(&slot->delivery, &dq->list);
1772 spin_unlock_irqrestore(&dq->lock, flags_dq);
1774 dlvry_queue = dq->id;
1775 dlvry_queue_slot = wr_q_index;
1777 slot->n_elem = n_elem;
1778 slot->dlvry_queue = dlvry_queue;
1779 slot->dlvry_queue_slot = dlvry_queue_slot;
1780 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1781 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1784 slot->is_internal = true;
1785 task->lldd_task = slot;
1787 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1788 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1789 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1791 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1792 abort_flag, task_tag);
1794 spin_lock_irqsave(&task->task_state_lock, flags);
1795 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1796 spin_unlock_irqrestore(&task->task_state_lock, flags);
1798 WRITE_ONCE(slot->ready, 1);
1799 /* send abort command to the chip */
1800 spin_lock_irqsave(&dq->lock, flags);
1801 list_add_tail(&slot->entry, &sas_dev->list);
1802 hisi_hba->hw->start_delivery(dq);
1803 spin_unlock_irqrestore(&dq->lock, flags);
1808 spin_lock_irqsave(&hisi_hba->lock, flags);
1809 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1810 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1812 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1818 * hisi_sas_internal_task_abort -- execute an internal
1819 * abort command for single IO command or a device
1820 * @hisi_hba: host controller struct
1821 * @device: domain device
1822 * @abort_flag: mode of operation, device or single IO
1823 * @tag: tag of IO to be aborted (only relevant to single
1827 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1828 struct domain_device *device,
1829 int abort_flag, int tag)
1831 struct sas_task *task;
1832 struct hisi_sas_device *sas_dev = device->lldd_dev;
1833 struct device *dev = hisi_hba->dev;
1837 * The interface is not realized means this HW don't support internal
1838 * abort, or don't need to do internal abort. Then here, we return
1839 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1840 * the internal abort has been executed and returned CQ.
1842 if (!hisi_hba->hw->prep_abort)
1843 return TMF_RESP_FUNC_FAILED;
1845 task = sas_alloc_slow_task(GFP_KERNEL);
1850 task->task_proto = device->tproto;
1851 task->task_done = hisi_sas_task_done;
1852 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1853 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1854 add_timer(&task->slow_task->timer);
1856 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1857 task, abort_flag, tag);
1859 del_timer(&task->slow_task->timer);
1860 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1864 wait_for_completion(&task->slow_task->completion);
1865 res = TMF_RESP_FUNC_FAILED;
1867 /* Internal abort timed out */
1868 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1869 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1870 struct hisi_sas_slot *slot = task->lldd_task;
1873 struct hisi_sas_cq *cq =
1874 &hisi_hba->cq[slot->dlvry_queue];
1876 * flush tasklet to avoid free'ing task
1877 * before using task in IO completion
1879 tasklet_kill(&cq->tasklet);
1882 dev_err(dev, "internal task abort: timeout and not done.\n");
1886 dev_err(dev, "internal task abort: timeout.\n");
1889 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1890 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1891 res = TMF_RESP_FUNC_COMPLETE;
1895 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1896 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1897 res = TMF_RESP_FUNC_SUCC;
1902 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1903 "resp: 0x%x sts 0x%x\n",
1904 SAS_ADDR(device->sas_addr),
1906 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1907 task->task_status.stat);
1908 sas_free_task(task);
1913 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1915 hisi_sas_port_notify_formed(sas_phy);
1918 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1922 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1923 u8 reg_index, u8 reg_count, u8 *write_data)
1925 struct hisi_hba *hisi_hba = sha->lldd_ha;
1927 if (!hisi_hba->hw->write_gpio)
1930 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1931 reg_index, reg_count, write_data);
1934 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1936 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1937 struct sas_phy *sphy = sas_phy->phy;
1938 struct sas_phy_data *d = sphy->hostdata;
1940 phy->phy_attached = 0;
1945 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1947 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
1950 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1952 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1953 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1954 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1955 struct device *dev = hisi_hba->dev;
1958 /* Phy down but ready */
1959 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1960 hisi_sas_port_notify_formed(sas_phy);
1962 struct hisi_sas_port *port = phy->port;
1964 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
1966 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
1969 /* Phy down and not ready */
1970 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1971 sas_phy_disconnected(sas_phy);
1974 if (phy->phy_type & PORT_TYPE_SAS) {
1975 int port_id = port->id;
1977 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1979 port->port_attached = 0;
1980 } else if (phy->phy_type & PORT_TYPE_SATA)
1981 port->port_attached = 0;
1983 hisi_sas_phy_disconnected(phy);
1986 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1988 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1992 for (i = 0; i < hisi_hba->queue_count; i++) {
1993 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1995 tasklet_kill(&cq->tasklet);
1998 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
2000 struct scsi_transport_template *hisi_sas_stt;
2001 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2003 struct device_attribute *host_attrs[] = {
2004 &dev_attr_phy_event_threshold,
2007 EXPORT_SYMBOL_GPL(host_attrs);
2009 static struct sas_domain_function_template hisi_sas_transport_ops = {
2010 .lldd_dev_found = hisi_sas_dev_found,
2011 .lldd_dev_gone = hisi_sas_dev_gone,
2012 .lldd_execute_task = hisi_sas_queue_command,
2013 .lldd_control_phy = hisi_sas_control_phy,
2014 .lldd_abort_task = hisi_sas_abort_task,
2015 .lldd_abort_task_set = hisi_sas_abort_task_set,
2016 .lldd_clear_aca = hisi_sas_clear_aca,
2017 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2018 .lldd_lu_reset = hisi_sas_lu_reset,
2019 .lldd_query_task = hisi_sas_query_task,
2020 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2021 .lldd_port_formed = hisi_sas_port_formed,
2022 .lldd_port_deformed = hisi_sas_port_deformed,
2023 .lldd_write_gpio = hisi_sas_write_gpio,
2026 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2028 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
2030 for (i = 0; i < hisi_hba->queue_count; i++) {
2031 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2032 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2034 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2035 memset(hisi_hba->cmd_hdr[i], 0, s);
2038 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2039 memset(hisi_hba->complete_hdr[i], 0, s);
2043 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2044 memset(hisi_hba->initial_fis, 0, s);
2046 s = max_command_entries * sizeof(struct hisi_sas_iost);
2047 memset(hisi_hba->iost, 0, s);
2049 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2050 memset(hisi_hba->breakpoint, 0, s);
2052 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2053 memset(hisi_hba->sata_breakpoint, 0, s);
2055 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2057 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
2059 struct device *dev = hisi_hba->dev;
2060 int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
2061 int max_command_entries_ru, sz_slot_buf_ru;
2062 int blk_cnt, slots_per_blk;
2064 sema_init(&hisi_hba->sem, 1);
2065 spin_lock_init(&hisi_hba->lock);
2066 for (i = 0; i < hisi_hba->n_phy; i++) {
2067 hisi_sas_phy_init(hisi_hba, i);
2068 hisi_hba->port[i].port_attached = 0;
2069 hisi_hba->port[i].id = -1;
2072 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2073 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2074 hisi_hba->devices[i].device_id = i;
2075 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
2078 for (i = 0; i < hisi_hba->queue_count; i++) {
2079 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2080 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2082 /* Completion queue structure */
2084 cq->hisi_hba = hisi_hba;
2086 /* Delivery queue structure */
2087 spin_lock_init(&dq->lock);
2088 INIT_LIST_HEAD(&dq->list);
2090 dq->hisi_hba = hisi_hba;
2092 /* Delivery queue */
2093 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2094 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2095 &hisi_hba->cmd_hdr_dma[i],
2097 if (!hisi_hba->cmd_hdr[i])
2100 /* Completion queue */
2101 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2102 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2103 &hisi_hba->complete_hdr_dma[i],
2105 if (!hisi_hba->complete_hdr[i])
2109 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2110 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2112 if (!hisi_hba->itct)
2114 memset(hisi_hba->itct, 0, s);
2116 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2117 sizeof(struct hisi_sas_slot),
2119 if (!hisi_hba->slot_info)
2122 /* roundup to avoid overly large block size */
2123 max_command_entries_ru = roundup(max_command_entries, 64);
2124 sz_slot_buf_ru = roundup(sizeof(struct hisi_sas_slot_buf_table), 64);
2125 s = lcm(max_command_entries_ru, sz_slot_buf_ru);
2126 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2127 slots_per_blk = s / sz_slot_buf_ru;
2128 for (i = 0; i < blk_cnt; i++) {
2129 struct hisi_sas_slot_buf_table *buf;
2131 int slot_index = i * slots_per_blk;
2133 buf = dmam_alloc_coherent(dev, s, &buf_dma, GFP_KERNEL);
2138 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2139 struct hisi_sas_slot *slot;
2141 slot = &hisi_hba->slot_info[slot_index];
2143 slot->buf_dma = buf_dma;
2144 slot->idx = slot_index;
2147 buf_dma += sizeof(*buf);
2151 s = max_command_entries * sizeof(struct hisi_sas_iost);
2152 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2154 if (!hisi_hba->iost)
2157 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2158 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2159 &hisi_hba->breakpoint_dma,
2161 if (!hisi_hba->breakpoint)
2164 hisi_hba->slot_index_count = max_command_entries;
2165 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2166 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2167 if (!hisi_hba->slot_index_tags)
2170 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2171 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2172 &hisi_hba->initial_fis_dma,
2174 if (!hisi_hba->initial_fis)
2177 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2178 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2179 &hisi_hba->sata_breakpoint_dma,
2181 if (!hisi_hba->sata_breakpoint)
2183 hisi_sas_init_mem(hisi_hba);
2185 hisi_sas_slot_index_init(hisi_hba);
2187 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2188 if (!hisi_hba->wq) {
2189 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2197 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2199 void hisi_sas_free(struct hisi_hba *hisi_hba)
2202 destroy_workqueue(hisi_hba->wq);
2204 EXPORT_SYMBOL_GPL(hisi_sas_free);
2206 void hisi_sas_rst_work_handler(struct work_struct *work)
2208 struct hisi_hba *hisi_hba =
2209 container_of(work, struct hisi_hba, rst_work);
2211 hisi_sas_controller_reset(hisi_hba);
2213 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2215 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2217 struct hisi_sas_rst *rst =
2218 container_of(work, struct hisi_sas_rst, work);
2220 if (!hisi_sas_controller_reset(rst->hisi_hba))
2222 complete(rst->completion);
2224 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2226 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2228 struct device *dev = hisi_hba->dev;
2229 struct platform_device *pdev = hisi_hba->platform_dev;
2230 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2233 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2235 dev_err(dev, "could not get property sas-addr\n");
2241 * These properties are only required for platform device-based
2242 * controller with DT firmware.
2244 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2245 "hisilicon,sas-syscon");
2246 if (IS_ERR(hisi_hba->ctrl)) {
2247 dev_err(dev, "could not get syscon\n");
2251 if (device_property_read_u32(dev, "ctrl-reset-reg",
2252 &hisi_hba->ctrl_reset_reg)) {
2254 "could not get property ctrl-reset-reg\n");
2258 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2259 &hisi_hba->ctrl_reset_sts_reg)) {
2261 "could not get property ctrl-reset-sts-reg\n");
2265 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2266 &hisi_hba->ctrl_clock_ena_reg)) {
2268 "could not get property ctrl-clock-ena-reg\n");
2273 refclk = devm_clk_get(dev, NULL);
2275 dev_dbg(dev, "no ref clk property\n");
2277 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2279 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2280 dev_err(dev, "could not get property phy-count\n");
2284 if (device_property_read_u32(dev, "queue-count",
2285 &hisi_hba->queue_count)) {
2286 dev_err(dev, "could not get property queue-count\n");
2292 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2294 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2295 const struct hisi_sas_hw *hw)
2297 struct resource *res;
2298 struct Scsi_Host *shost;
2299 struct hisi_hba *hisi_hba;
2300 struct device *dev = &pdev->dev;
2302 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2304 dev_err(dev, "scsi host alloc failed\n");
2307 hisi_hba = shost_priv(shost);
2309 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2311 hisi_hba->dev = dev;
2312 hisi_hba->platform_dev = pdev;
2313 hisi_hba->shost = shost;
2314 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2316 timer_setup(&hisi_hba->timer, NULL, 0);
2318 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2321 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2322 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2323 dev_err(dev, "No usable DMA addressing method\n");
2327 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2328 hisi_hba->regs = devm_ioremap_resource(dev, res);
2329 if (IS_ERR(hisi_hba->regs))
2332 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2334 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2335 if (IS_ERR(hisi_hba->sgpio_regs))
2339 if (hisi_sas_alloc(hisi_hba, shost)) {
2340 hisi_sas_free(hisi_hba);
2346 scsi_host_put(shost);
2347 dev_err(dev, "shost alloc failed\n");
2351 int hisi_sas_probe(struct platform_device *pdev,
2352 const struct hisi_sas_hw *hw)
2354 struct Scsi_Host *shost;
2355 struct hisi_hba *hisi_hba;
2356 struct device *dev = &pdev->dev;
2357 struct asd_sas_phy **arr_phy;
2358 struct asd_sas_port **arr_port;
2359 struct sas_ha_struct *sha;
2360 int rc, phy_nr, port_nr, i;
2362 shost = hisi_sas_shost_alloc(pdev, hw);
2366 sha = SHOST_TO_SAS_HA(shost);
2367 hisi_hba = shost_priv(shost);
2368 platform_set_drvdata(pdev, sha);
2370 phy_nr = port_nr = hisi_hba->n_phy;
2372 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2373 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2374 if (!arr_phy || !arr_port) {
2379 sha->sas_phy = arr_phy;
2380 sha->sas_port = arr_port;
2381 sha->lldd_ha = hisi_hba;
2383 shost->transportt = hisi_sas_stt;
2384 shost->max_id = HISI_SAS_MAX_DEVICES;
2385 shost->max_lun = ~0;
2386 shost->max_channel = 1;
2387 shost->max_cmd_len = 16;
2388 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2389 shost->can_queue = hisi_hba->hw->max_command_entries;
2390 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2392 sha->sas_ha_name = DRV_NAME;
2393 sha->dev = hisi_hba->dev;
2394 sha->lldd_module = THIS_MODULE;
2395 sha->sas_addr = &hisi_hba->sas_addr[0];
2396 sha->num_phys = hisi_hba->n_phy;
2397 sha->core.shost = hisi_hba->shost;
2399 for (i = 0; i < hisi_hba->n_phy; i++) {
2400 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2401 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2404 rc = scsi_add_host(shost, &pdev->dev);
2408 rc = sas_register_ha(sha);
2410 goto err_out_register_ha;
2412 rc = hisi_hba->hw->hw_init(hisi_hba);
2414 goto err_out_register_ha;
2416 scsi_scan_host(shost);
2420 err_out_register_ha:
2421 scsi_remove_host(shost);
2423 hisi_sas_free(hisi_hba);
2424 scsi_host_put(shost);
2427 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2429 int hisi_sas_remove(struct platform_device *pdev)
2431 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2432 struct hisi_hba *hisi_hba = sha->lldd_ha;
2433 struct Scsi_Host *shost = sha->core.shost;
2435 if (timer_pending(&hisi_hba->timer))
2436 del_timer(&hisi_hba->timer);
2438 sas_unregister_ha(sha);
2439 sas_remove_host(sha->core.shost);
2441 hisi_sas_free(hisi_hba);
2442 scsi_host_put(shost);
2445 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2447 static __init int hisi_sas_init(void)
2449 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2456 static __exit void hisi_sas_exit(void)
2458 sas_release_transport(hisi_sas_stt);
2461 module_init(hisi_sas_init);
2462 module_exit(hisi_sas_exit);
2464 MODULE_LICENSE("GPL");
2465 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2466 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2467 MODULE_ALIAS("platform:" DRV_NAME);