2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
13 #define DRV_NAME "hisi_sas"
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
24 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
25 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
27 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
28 struct domain_device *device);
29 static void hisi_sas_dev_gone(struct domain_device *device);
31 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
33 switch (fis->command) {
34 case ATA_CMD_FPDMA_WRITE:
35 case ATA_CMD_FPDMA_READ:
36 case ATA_CMD_FPDMA_RECV:
37 case ATA_CMD_FPDMA_SEND:
38 case ATA_CMD_NCQ_NON_DATA:
39 return HISI_SAS_SATA_PROTOCOL_FPDMA;
41 case ATA_CMD_DOWNLOAD_MICRO:
43 case ATA_CMD_PMP_READ:
44 case ATA_CMD_READ_LOG_EXT:
45 case ATA_CMD_PIO_READ:
46 case ATA_CMD_PIO_READ_EXT:
47 case ATA_CMD_PMP_WRITE:
48 case ATA_CMD_WRITE_LOG_EXT:
49 case ATA_CMD_PIO_WRITE:
50 case ATA_CMD_PIO_WRITE_EXT:
51 return HISI_SAS_SATA_PROTOCOL_PIO;
54 case ATA_CMD_DOWNLOAD_MICRO_DMA:
55 case ATA_CMD_PMP_READ_DMA:
56 case ATA_CMD_PMP_WRITE_DMA:
58 case ATA_CMD_READ_EXT:
59 case ATA_CMD_READ_LOG_DMA_EXT:
60 case ATA_CMD_READ_STREAM_DMA_EXT:
61 case ATA_CMD_TRUSTED_RCV_DMA:
62 case ATA_CMD_TRUSTED_SND_DMA:
64 case ATA_CMD_WRITE_EXT:
65 case ATA_CMD_WRITE_FUA_EXT:
66 case ATA_CMD_WRITE_QUEUED:
67 case ATA_CMD_WRITE_LOG_DMA_EXT:
68 case ATA_CMD_WRITE_STREAM_DMA_EXT:
69 case ATA_CMD_ZAC_MGMT_IN:
70 return HISI_SAS_SATA_PROTOCOL_DMA;
72 case ATA_CMD_CHK_POWER:
73 case ATA_CMD_DEV_RESET:
76 case ATA_CMD_FLUSH_EXT:
78 case ATA_CMD_VERIFY_EXT:
79 case ATA_CMD_SET_FEATURES:
81 case ATA_CMD_STANDBYNOW1:
82 case ATA_CMD_ZAC_MGMT_OUT:
83 return HISI_SAS_SATA_PROTOCOL_NONDATA;
86 switch (fis->features) {
87 case ATA_SET_MAX_PASSWD:
88 case ATA_SET_MAX_LOCK:
89 return HISI_SAS_SATA_PROTOCOL_PIO;
91 case ATA_SET_MAX_PASSWD_DMA:
92 case ATA_SET_MAX_UNLOCK_DMA:
93 return HISI_SAS_SATA_PROTOCOL_DMA;
96 return HISI_SAS_SATA_PROTOCOL_NONDATA;
101 if (direction == DMA_NONE)
102 return HISI_SAS_SATA_PROTOCOL_NONDATA;
103 return HISI_SAS_SATA_PROTOCOL_PIO;
107 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
109 void hisi_sas_sata_done(struct sas_task *task,
110 struct hisi_sas_slot *slot)
112 struct task_status_struct *ts = &task->task_status;
113 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
114 struct hisi_sas_status_buffer *status_buf =
115 hisi_sas_status_buf_addr_mem(slot);
116 u8 *iu = &status_buf->iu[0];
117 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
119 resp->frame_len = sizeof(struct dev_to_host_fis);
120 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
122 ts->buf_valid_size = sizeof(*resp);
124 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
126 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
128 struct ata_queued_cmd *qc = task->uldd_task;
131 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
132 qc->tf.command == ATA_CMD_FPDMA_READ) {
139 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
142 * This function assumes linkrate mask fits in 8 bits, which it
143 * does for all HW versions supported.
145 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
150 max -= SAS_LINK_RATE_1_5_GBPS;
151 for (i = 0; i <= max; i++)
152 rate |= 1 << (i * 2);
155 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
157 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
159 return device->port->ha->lldd_ha;
162 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
164 return container_of(sas_port, struct hisi_sas_port, sas_port);
166 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
168 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
172 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
173 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
175 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
177 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
179 void *bitmap = hisi_hba->slot_index_tags;
181 clear_bit(slot_idx, bitmap);
184 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
186 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
189 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
191 void *bitmap = hisi_hba->slot_index_tags;
193 set_bit(slot_idx, bitmap);
196 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
199 void *bitmap = hisi_hba->slot_index_tags;
201 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
202 hisi_hba->last_slot_index + 1);
203 if (index >= hisi_hba->slot_index_count) {
204 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
206 if (index >= hisi_hba->slot_index_count)
207 return -SAS_QUEUE_FULL;
209 hisi_sas_slot_index_set(hisi_hba, index);
211 hisi_hba->last_slot_index = index;
216 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
220 for (i = 0; i < hisi_hba->slot_index_count; ++i)
221 hisi_sas_slot_index_clear(hisi_hba, i);
224 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
225 struct hisi_sas_slot *slot)
227 struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
231 struct device *dev = hisi_hba->dev;
233 if (!task->lldd_task)
236 task->lldd_task = NULL;
238 if (!sas_protocol_ata(task->task_proto))
240 dma_unmap_sg(dev, task->scatter,
246 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
248 spin_lock_irqsave(&dq->lock, flags);
249 list_del_init(&slot->entry);
250 spin_unlock_irqrestore(&dq->lock, flags);
254 spin_lock_irqsave(&hisi_hba->lock, flags);
255 hisi_sas_slot_index_free(hisi_hba, slot->idx);
256 spin_unlock_irqrestore(&hisi_hba->lock, flags);
258 /* slot memory is fully zeroed when it is reused */
260 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
262 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
263 struct hisi_sas_slot *slot)
265 hisi_hba->hw->prep_smp(hisi_hba, slot);
268 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
269 struct hisi_sas_slot *slot, int is_tmf,
270 struct hisi_sas_tmf_task *tmf)
272 hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
275 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
276 struct hisi_sas_slot *slot)
278 hisi_hba->hw->prep_stp(hisi_hba, slot);
281 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
282 struct hisi_sas_slot *slot,
283 int device_id, int abort_flag, int tag_to_abort)
285 hisi_hba->hw->prep_abort(hisi_hba, slot,
286 device_id, abort_flag, tag_to_abort);
290 * This function will issue an abort TMF regardless of whether the
291 * task is in the sdev or not. Then it will do the task complete
292 * cleanup and callbacks.
294 static void hisi_sas_slot_abort(struct work_struct *work)
296 struct hisi_sas_slot *abort_slot =
297 container_of(work, struct hisi_sas_slot, abort_slot);
298 struct sas_task *task = abort_slot->task;
299 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
300 struct scsi_cmnd *cmnd = task->uldd_task;
301 struct hisi_sas_tmf_task tmf_task;
303 struct device *dev = hisi_hba->dev;
304 int tag = abort_slot->idx;
306 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
307 dev_err(dev, "cannot abort slot for non-ssp task\n");
311 int_to_scsilun(cmnd->device->lun, &lun);
312 tmf_task.tmf = TMF_ABORT_TASK;
313 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
315 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
317 /* Do cleanup for this task */
318 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
320 task->task_done(task);
323 static int hisi_sas_task_prep(struct sas_task *task,
324 struct hisi_sas_dq **dq_pointer,
325 int is_tmf, struct hisi_sas_tmf_task *tmf,
328 struct domain_device *device = task->dev;
329 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
330 struct hisi_sas_device *sas_dev = device->lldd_dev;
331 struct hisi_sas_port *port;
332 struct hisi_sas_slot *slot;
333 struct hisi_sas_cmd_hdr *cmd_hdr_base;
334 struct asd_sas_port *sas_port = device->port;
335 struct device *dev = hisi_hba->dev;
336 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
337 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
338 unsigned long flags, flags_dq;
339 struct hisi_sas_dq *dq;
343 struct task_status_struct *ts = &task->task_status;
345 ts->resp = SAS_TASK_UNDELIVERED;
346 ts->stat = SAS_PHY_DOWN;
348 * libsas will use dev->port, should
349 * not call task_done for sata
351 if (device->dev_type != SAS_SATA_DEV)
352 task->task_done(task);
356 if (DEV_IS_GONE(sas_dev)) {
358 dev_info(dev, "task prep: device %d not ready\n",
361 dev_info(dev, "task prep: device %016llx not ready\n",
362 SAS_ADDR(device->sas_addr));
367 *dq_pointer = dq = sas_dev->dq;
369 port = to_hisi_sas_port(sas_port);
370 if (port && !port->port_attached) {
371 dev_info(dev, "task prep: %s port%d not attach device\n",
372 (dev_is_sata(device)) ?
379 if (!sas_protocol_ata(task->task_proto)) {
380 unsigned int req_len, resp_len;
382 if (task->num_scatter) {
383 n_elem = dma_map_sg(dev, task->scatter,
384 task->num_scatter, task->data_dir);
389 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
390 n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
396 req_len = sg_dma_len(&task->smp_task.smp_req);
399 goto err_out_dma_unmap;
401 n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
405 goto err_out_dma_unmap;
407 resp_len = sg_dma_len(&task->smp_task.smp_resp);
408 if (resp_len & 0x3) {
410 goto err_out_dma_unmap;
414 n_elem = task->num_scatter;
416 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
417 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
420 goto err_out_dma_unmap;
423 spin_lock_irqsave(&hisi_hba->lock, flags);
424 if (hisi_hba->hw->slot_index_alloc)
425 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
428 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
429 spin_unlock_irqrestore(&hisi_hba->lock, flags);
431 goto err_out_dma_unmap;
433 slot = &hisi_hba->slot_info[slot_idx];
434 memset(slot, 0, sizeof(struct hisi_sas_slot));
436 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
437 GFP_ATOMIC, &slot->buf_dma);
443 spin_lock_irqsave(&dq->lock, flags_dq);
444 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
445 if (wr_q_index < 0) {
446 spin_unlock_irqrestore(&dq->lock, flags_dq);
450 list_add_tail(&slot->delivery, &dq->list);
451 spin_unlock_irqrestore(&dq->lock, flags_dq);
453 dlvry_queue = dq->id;
454 dlvry_queue_slot = wr_q_index;
456 slot->idx = slot_idx;
457 slot->n_elem = n_elem;
458 slot->dlvry_queue = dlvry_queue;
459 slot->dlvry_queue_slot = dlvry_queue_slot;
460 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
461 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
465 slot->is_internal = true;
466 task->lldd_task = slot;
467 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
469 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
470 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
471 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
473 switch (task->task_proto) {
474 case SAS_PROTOCOL_SMP:
475 hisi_sas_task_prep_smp(hisi_hba, slot);
477 case SAS_PROTOCOL_SSP:
478 hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
480 case SAS_PROTOCOL_SATA:
481 case SAS_PROTOCOL_STP:
482 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
483 hisi_sas_task_prep_ata(hisi_hba, slot);
486 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
491 spin_lock_irqsave(&dq->lock, flags);
492 list_add_tail(&slot->entry, &sas_dev->list);
493 spin_unlock_irqrestore(&dq->lock, flags);
494 spin_lock_irqsave(&task->task_state_lock, flags);
495 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
496 spin_unlock_irqrestore(&task->task_state_lock, flags);
504 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
507 spin_lock_irqsave(&hisi_hba->lock, flags);
508 hisi_sas_slot_index_free(hisi_hba, slot_idx);
509 spin_unlock_irqrestore(&hisi_hba->lock, flags);
511 if (!sas_protocol_ata(task->task_proto)) {
512 if (task->num_scatter) {
513 dma_unmap_sg(dev, task->scatter, task->num_scatter,
515 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
517 dma_unmap_sg(dev, &task->smp_task.smp_req,
520 dma_unmap_sg(dev, &task->smp_task.smp_resp,
525 dev_err(dev, "task prep: failed[%d]!\n", rc);
529 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
530 int is_tmf, struct hisi_sas_tmf_task *tmf)
535 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
536 struct device *dev = hisi_hba->dev;
537 struct hisi_sas_dq *dq = NULL;
539 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
542 /* protect task_prep and start_delivery sequence */
543 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
545 dev_err(dev, "task exec: failed[%d]!\n", rc);
548 spin_lock_irqsave(&dq->lock, flags);
549 hisi_hba->hw->start_delivery(dq);
550 spin_unlock_irqrestore(&dq->lock, flags);
556 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
558 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
559 struct asd_sas_phy *sas_phy = &phy->sas_phy;
560 struct sas_ha_struct *sas_ha;
562 if (!phy->phy_attached)
565 sas_ha = &hisi_hba->sha;
566 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
569 struct sas_phy *sphy = sas_phy->phy;
571 sphy->negotiated_linkrate = sas_phy->linkrate;
572 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
573 sphy->maximum_linkrate_hw =
574 hisi_hba->hw->phy_get_max_linkrate();
575 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
576 sphy->minimum_linkrate = phy->minimum_linkrate;
578 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
579 sphy->maximum_linkrate = phy->maximum_linkrate;
582 if (phy->phy_type & PORT_TYPE_SAS) {
583 struct sas_identify_frame *id;
585 id = (struct sas_identify_frame *)phy->frame_rcvd;
586 id->dev_type = phy->identify.device_type;
587 id->initiator_bits = SAS_PROTOCOL_ALL;
588 id->target_bits = phy->identify.target_port_protocols;
589 } else if (phy->phy_type & PORT_TYPE_SATA) {
593 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
594 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
597 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
599 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
600 struct hisi_sas_device *sas_dev = NULL;
602 int last = hisi_hba->last_dev_id;
603 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
606 spin_lock_irqsave(&hisi_hba->lock, flags);
607 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
608 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
609 int queue = i % hisi_hba->queue_count;
610 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
612 hisi_hba->devices[i].device_id = i;
613 sas_dev = &hisi_hba->devices[i];
614 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
615 sas_dev->dev_type = device->dev_type;
616 sas_dev->hisi_hba = hisi_hba;
617 sas_dev->sas_device = device;
619 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
624 hisi_hba->last_dev_id = i;
625 spin_unlock_irqrestore(&hisi_hba->lock, flags);
630 #define HISI_SAS_SRST_ATA_DISK_CNT 3
631 static int hisi_sas_init_device(struct domain_device *device)
633 int rc = TMF_RESP_FUNC_COMPLETE;
635 struct hisi_sas_tmf_task tmf_task;
636 int retry = HISI_SAS_SRST_ATA_DISK_CNT;
637 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
639 switch (device->dev_type) {
641 int_to_scsilun(0, &lun);
643 tmf_task.tmf = TMF_CLEAR_TASK_SET;
644 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
646 if (rc == TMF_RESP_FUNC_COMPLETE)
647 hisi_sas_release_task(hisi_hba, device);
651 case SAS_SATA_PM_PORT:
652 case SAS_SATA_PENDING:
653 while (retry-- > 0) {
654 rc = hisi_sas_softreset_ata_disk(device);
666 static int hisi_sas_dev_found(struct domain_device *device)
668 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
669 struct domain_device *parent_dev = device->parent;
670 struct hisi_sas_device *sas_dev;
671 struct device *dev = hisi_hba->dev;
674 if (hisi_hba->hw->alloc_dev)
675 sas_dev = hisi_hba->hw->alloc_dev(device);
677 sas_dev = hisi_sas_alloc_dev(device);
679 dev_err(dev, "fail alloc dev: max support %d devices\n",
680 HISI_SAS_MAX_DEVICES);
684 device->lldd_dev = sas_dev;
685 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
687 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
689 u8 phy_num = parent_dev->ex_dev.num_phys;
692 for (phy_no = 0; phy_no < phy_num; phy_no++) {
693 phy = &parent_dev->ex_dev.ex_phy[phy_no];
694 if (SAS_ADDR(phy->attached_sas_addr) ==
695 SAS_ADDR(device->sas_addr))
699 if (phy_no == phy_num) {
700 dev_info(dev, "dev found: no attached "
701 "dev:%016llx at ex:%016llx\n",
702 SAS_ADDR(device->sas_addr),
703 SAS_ADDR(parent_dev->sas_addr));
709 dev_info(dev, "dev[%d:%x] found\n",
710 sas_dev->device_id, sas_dev->dev_type);
712 rc = hisi_sas_init_device(device);
718 hisi_sas_dev_gone(device);
722 static int hisi_sas_slave_configure(struct scsi_device *sdev)
724 struct domain_device *dev = sdev_to_domain_dev(sdev);
725 int ret = sas_slave_configure(sdev);
729 if (!dev_is_sata(dev))
730 sas_change_queue_depth(sdev, 64);
735 static void hisi_sas_scan_start(struct Scsi_Host *shost)
737 struct hisi_hba *hisi_hba = shost_priv(shost);
739 hisi_hba->hw->phys_init(hisi_hba);
742 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
744 struct hisi_hba *hisi_hba = shost_priv(shost);
745 struct sas_ha_struct *sha = &hisi_hba->sha;
747 /* Wait for PHY up interrupt to occur */
755 static void hisi_sas_phyup_work(struct work_struct *work)
757 struct hisi_sas_phy *phy =
758 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
759 struct hisi_hba *hisi_hba = phy->hisi_hba;
760 struct asd_sas_phy *sas_phy = &phy->sas_phy;
761 int phy_no = sas_phy->id;
763 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
764 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
767 static void hisi_sas_linkreset_work(struct work_struct *work)
769 struct hisi_sas_phy *phy =
770 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
771 struct asd_sas_phy *sas_phy = &phy->sas_phy;
773 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
776 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
777 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
778 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
781 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
782 enum hisi_sas_phy_event event)
784 struct hisi_hba *hisi_hba = phy->hisi_hba;
786 if (WARN_ON(event >= HISI_PHYES_NUM))
789 return queue_work(hisi_hba->wq, &phy->works[event]);
791 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
793 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
795 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
796 struct asd_sas_phy *sas_phy = &phy->sas_phy;
799 phy->hisi_hba = hisi_hba;
801 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
802 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
803 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
804 sas_phy->class = SAS;
805 sas_phy->iproto = SAS_PROTOCOL_ALL;
807 sas_phy->type = PHY_TYPE_PHYSICAL;
808 sas_phy->role = PHY_ROLE_INITIATOR;
809 sas_phy->oob_mode = OOB_NOT_CONNECTED;
810 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
811 sas_phy->id = phy_no;
812 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
813 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
814 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
815 sas_phy->lldd_phy = phy;
817 for (i = 0; i < HISI_PHYES_NUM; i++)
818 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
821 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
823 struct sas_ha_struct *sas_ha = sas_phy->ha;
824 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
825 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
826 struct asd_sas_port *sas_port = sas_phy->port;
827 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
833 spin_lock_irqsave(&hisi_hba->lock, flags);
834 port->port_attached = 1;
835 port->id = phy->port_id;
837 sas_port->lldd_port = port;
838 spin_unlock_irqrestore(&hisi_hba->lock, flags);
841 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
842 struct hisi_sas_slot *slot)
846 struct task_status_struct *ts;
848 ts = &task->task_status;
850 ts->resp = SAS_TASK_COMPLETE;
851 ts->stat = SAS_ABORTED_TASK;
852 spin_lock_irqsave(&task->task_state_lock, flags);
853 task->task_state_flags &=
854 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
855 task->task_state_flags |= SAS_TASK_STATE_DONE;
856 spin_unlock_irqrestore(&task->task_state_lock, flags);
859 hisi_sas_slot_task_free(hisi_hba, task, slot);
862 /* hisi_hba.lock should be locked */
863 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
864 struct domain_device *device)
866 struct hisi_sas_slot *slot, *slot2;
867 struct hisi_sas_device *sas_dev = device->lldd_dev;
869 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
870 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
873 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
875 struct hisi_sas_device *sas_dev;
876 struct domain_device *device;
879 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
880 sas_dev = &hisi_hba->devices[i];
881 device = sas_dev->sas_device;
883 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
887 hisi_sas_release_task(hisi_hba, device);
890 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
892 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
893 struct domain_device *device)
895 if (hisi_hba->hw->dereg_device)
896 hisi_hba->hw->dereg_device(hisi_hba, device);
899 static void hisi_sas_dev_gone(struct domain_device *device)
901 struct hisi_sas_device *sas_dev = device->lldd_dev;
902 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
903 struct device *dev = hisi_hba->dev;
905 dev_info(dev, "dev[%d:%x] is gone\n",
906 sas_dev->device_id, sas_dev->dev_type);
908 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
909 hisi_sas_internal_task_abort(hisi_hba, device,
910 HISI_SAS_INT_ABT_DEV, 0);
912 hisi_sas_dereg_device(hisi_hba, device);
914 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
915 device->lldd_dev = NULL;
918 if (hisi_hba->hw->free_device)
919 hisi_hba->hw->free_device(sas_dev);
920 sas_dev->dev_type = SAS_PHY_UNUSED;
923 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
925 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
928 static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
929 struct sas_phy_linkrates *r)
931 struct sas_phy_linkrates _r;
933 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
934 struct asd_sas_phy *sas_phy = &phy->sas_phy;
935 enum sas_linkrate min, max;
937 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
938 max = sas_phy->phy->maximum_linkrate;
939 min = r->minimum_linkrate;
940 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
941 max = r->maximum_linkrate;
942 min = sas_phy->phy->minimum_linkrate;
946 _r.maximum_linkrate = max;
947 _r.minimum_linkrate = min;
949 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
951 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
952 hisi_hba->hw->phy_start(hisi_hba, phy_no);
955 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
958 struct sas_ha_struct *sas_ha = sas_phy->ha;
959 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
960 int phy_no = sas_phy->id;
963 case PHY_FUNC_HARD_RESET:
964 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
967 case PHY_FUNC_LINK_RESET:
968 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
970 hisi_hba->hw->phy_start(hisi_hba, phy_no);
973 case PHY_FUNC_DISABLE:
974 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
977 case PHY_FUNC_SET_LINK_RATE:
978 hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
980 case PHY_FUNC_GET_EVENTS:
981 if (hisi_hba->hw->get_events) {
982 hisi_hba->hw->get_events(hisi_hba, phy_no);
986 case PHY_FUNC_RELEASE_SPINUP_HOLD:
993 static void hisi_sas_task_done(struct sas_task *task)
995 if (!del_timer(&task->slow_task->timer))
997 complete(&task->slow_task->completion);
1000 static void hisi_sas_tmf_timedout(struct timer_list *t)
1002 struct sas_task_slow *slow = from_timer(slow, t, timer);
1003 struct sas_task *task = slow->task;
1004 unsigned long flags;
1006 spin_lock_irqsave(&task->task_state_lock, flags);
1007 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1008 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1009 spin_unlock_irqrestore(&task->task_state_lock, flags);
1011 complete(&task->slow_task->completion);
1014 #define TASK_TIMEOUT 20
1015 #define TASK_RETRY 3
1016 #define INTERNAL_ABORT_TIMEOUT 6
1017 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1018 void *parameter, u32 para_len,
1019 struct hisi_sas_tmf_task *tmf)
1021 struct hisi_sas_device *sas_dev = device->lldd_dev;
1022 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1023 struct device *dev = hisi_hba->dev;
1024 struct sas_task *task;
1027 for (retry = 0; retry < TASK_RETRY; retry++) {
1028 task = sas_alloc_slow_task(GFP_KERNEL);
1033 task->task_proto = device->tproto;
1035 if (dev_is_sata(device)) {
1036 task->ata_task.device_control_reg_update = 1;
1037 memcpy(&task->ata_task.fis, parameter, para_len);
1039 memcpy(&task->ssp_task, parameter, para_len);
1041 task->task_done = hisi_sas_task_done;
1043 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1044 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1045 add_timer(&task->slow_task->timer);
1047 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1050 del_timer(&task->slow_task->timer);
1051 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1056 wait_for_completion(&task->slow_task->completion);
1057 res = TMF_RESP_FUNC_FAILED;
1058 /* Even TMF timed out, return direct. */
1059 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1060 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1061 struct hisi_sas_slot *slot = task->lldd_task;
1063 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1069 dev_err(dev, "abort tmf: TMF task timeout\n");
1072 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1073 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1074 res = TMF_RESP_FUNC_COMPLETE;
1078 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1079 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1080 res = TMF_RESP_FUNC_SUCC;
1084 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1085 task->task_status.stat == SAS_DATA_UNDERRUN) {
1086 /* no error, but return the number of bytes of
1089 dev_warn(dev, "abort tmf: task to dev %016llx "
1090 "resp: 0x%x sts 0x%x underrun\n",
1091 SAS_ADDR(device->sas_addr),
1092 task->task_status.resp,
1093 task->task_status.stat);
1094 res = task->task_status.residual;
1098 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1099 task->task_status.stat == SAS_DATA_OVERRUN) {
1100 dev_warn(dev, "abort tmf: blocked task error\n");
1105 dev_warn(dev, "abort tmf: task to dev "
1106 "%016llx resp: 0x%x status 0x%x\n",
1107 SAS_ADDR(device->sas_addr), task->task_status.resp,
1108 task->task_status.stat);
1109 sas_free_task(task);
1113 if (retry == TASK_RETRY)
1114 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1115 sas_free_task(task);
1119 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1120 bool reset, int pmp, u8 *fis)
1122 struct ata_taskfile tf;
1124 ata_tf_init(dev, &tf);
1128 tf.ctl &= ~ATA_SRST;
1129 tf.command = ATA_CMD_DEV_RESET;
1130 ata_tf_to_fis(&tf, pmp, 0, fis);
1133 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1136 struct ata_port *ap = device->sata_dev.ap;
1137 struct ata_link *link;
1138 int rc = TMF_RESP_FUNC_FAILED;
1139 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1140 struct device *dev = hisi_hba->dev;
1141 int s = sizeof(struct host_to_dev_fis);
1143 ata_for_each_link(link, ap, EDGE) {
1144 int pmp = sata_srst_pmp(link);
1146 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1147 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1148 if (rc != TMF_RESP_FUNC_COMPLETE)
1152 if (rc == TMF_RESP_FUNC_COMPLETE) {
1153 ata_for_each_link(link, ap, EDGE) {
1154 int pmp = sata_srst_pmp(link);
1156 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1157 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1159 if (rc != TMF_RESP_FUNC_COMPLETE)
1160 dev_err(dev, "ata disk de-reset failed\n");
1163 dev_err(dev, "ata disk reset failed\n");
1166 if (rc == TMF_RESP_FUNC_COMPLETE)
1167 hisi_sas_release_task(hisi_hba, device);
1172 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1173 u8 *lun, struct hisi_sas_tmf_task *tmf)
1175 struct sas_ssp_task ssp_task;
1177 if (!(device->tproto & SAS_PROTOCOL_SSP))
1178 return TMF_RESP_FUNC_ESUPP;
1180 memcpy(ssp_task.LUN, lun, 8);
1182 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1183 sizeof(ssp_task), tmf);
1186 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1188 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1191 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1192 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1193 struct domain_device *device = sas_dev->sas_device;
1194 struct asd_sas_port *sas_port;
1195 struct hisi_sas_port *port;
1196 struct hisi_sas_phy *phy = NULL;
1197 struct asd_sas_phy *sas_phy;
1199 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1200 || !device || !device->port)
1203 sas_port = device->port;
1204 port = to_hisi_sas_port(sas_port);
1206 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1207 if (state & BIT(sas_phy->id)) {
1208 phy = sas_phy->lldd_phy;
1213 port->id = phy->port_id;
1215 /* Update linkrate of directly attached device. */
1216 if (!device->parent)
1217 device->linkrate = phy->sas_phy.linkrate;
1219 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1225 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1228 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1229 struct asd_sas_port *_sas_port = NULL;
1232 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1233 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1234 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1235 struct asd_sas_port *sas_port = sas_phy->port;
1236 bool do_port_check = !!(_sas_port != sas_port);
1238 if (!sas_phy->phy->enabled)
1241 /* Report PHY state change to libsas */
1242 if (state & BIT(phy_no)) {
1243 if (do_port_check && sas_port && sas_port->port_dev) {
1244 struct domain_device *dev = sas_port->port_dev;
1246 _sas_port = sas_port;
1248 if (DEV_IS_EXPANDER(dev->dev_type))
1249 sas_ha->notify_port_event(sas_phy,
1250 PORTE_BROADCAST_RCVD);
1252 } else if (old_state & (1 << phy_no))
1253 /* PHY down but was up before */
1254 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1259 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1261 struct device *dev = hisi_hba->dev;
1262 struct Scsi_Host *shost = hisi_hba->shost;
1263 u32 old_state, state;
1266 if (!hisi_hba->hw->soft_reset)
1269 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1272 dev_info(dev, "controller resetting...\n");
1273 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1275 scsi_block_requests(shost);
1276 if (timer_pending(&hisi_hba->timer))
1277 del_timer_sync(&hisi_hba->timer);
1279 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1280 rc = hisi_hba->hw->soft_reset(hisi_hba);
1282 dev_warn(dev, "controller reset failed (%d)\n", rc);
1283 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1284 scsi_unblock_requests(shost);
1287 hisi_sas_release_tasks(hisi_hba);
1289 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1291 /* Init and wait for PHYs to come up and all libsas event finished. */
1292 hisi_hba->hw->phys_init(hisi_hba);
1294 hisi_sas_refresh_port_id(hisi_hba);
1295 scsi_unblock_requests(shost);
1297 state = hisi_hba->hw->get_phys_state(hisi_hba);
1298 hisi_sas_rescan_topology(hisi_hba, old_state, state);
1299 dev_info(dev, "controller reset complete\n");
1302 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1307 static int hisi_sas_abort_task(struct sas_task *task)
1309 struct scsi_lun lun;
1310 struct hisi_sas_tmf_task tmf_task;
1311 struct domain_device *device = task->dev;
1312 struct hisi_sas_device *sas_dev = device->lldd_dev;
1313 struct hisi_hba *hisi_hba;
1315 int rc = TMF_RESP_FUNC_FAILED;
1316 unsigned long flags;
1319 return TMF_RESP_FUNC_FAILED;
1321 hisi_hba = dev_to_hisi_hba(task->dev);
1322 dev = hisi_hba->dev;
1324 spin_lock_irqsave(&task->task_state_lock, flags);
1325 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1326 spin_unlock_irqrestore(&task->task_state_lock, flags);
1327 rc = TMF_RESP_FUNC_COMPLETE;
1330 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1331 spin_unlock_irqrestore(&task->task_state_lock, flags);
1333 sas_dev->dev_status = HISI_SAS_DEV_EH;
1334 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1335 struct scsi_cmnd *cmnd = task->uldd_task;
1336 struct hisi_sas_slot *slot = task->lldd_task;
1337 u32 tag = slot->idx;
1340 int_to_scsilun(cmnd->device->lun, &lun);
1341 tmf_task.tmf = TMF_ABORT_TASK;
1342 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1344 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1347 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1348 HISI_SAS_INT_ABT_CMD, tag);
1350 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1351 return TMF_RESP_FUNC_FAILED;
1355 * If the TMF finds that the IO is not in the device and also
1356 * the internal abort does not succeed, then it is safe to
1358 * Note: if the internal abort succeeds then the slot
1359 * will have already been completed
1361 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1362 if (task->lldd_task)
1363 hisi_sas_do_release_task(hisi_hba, task, slot);
1365 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1366 task->task_proto & SAS_PROTOCOL_STP) {
1367 if (task->dev->dev_type == SAS_SATA_DEV) {
1368 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1369 HISI_SAS_INT_ABT_DEV, 0);
1371 dev_err(dev, "abort task: internal abort failed\n");
1374 hisi_sas_dereg_device(hisi_hba, device);
1375 rc = hisi_sas_softreset_ata_disk(device);
1377 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1379 struct hisi_sas_slot *slot = task->lldd_task;
1380 u32 tag = slot->idx;
1382 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1383 HISI_SAS_INT_ABT_CMD, tag);
1384 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1386 hisi_sas_do_release_task(hisi_hba, task, slot);
1390 if (rc != TMF_RESP_FUNC_COMPLETE)
1391 dev_notice(dev, "abort task: rc=%d\n", rc);
1395 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1397 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1398 struct device *dev = hisi_hba->dev;
1399 struct hisi_sas_tmf_task tmf_task;
1400 int rc = TMF_RESP_FUNC_FAILED;
1402 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1403 HISI_SAS_INT_ABT_DEV, 0);
1405 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1406 return TMF_RESP_FUNC_FAILED;
1408 hisi_sas_dereg_device(hisi_hba, device);
1410 tmf_task.tmf = TMF_ABORT_TASK_SET;
1411 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1413 if (rc == TMF_RESP_FUNC_COMPLETE)
1414 hisi_sas_release_task(hisi_hba, device);
1419 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1421 int rc = TMF_RESP_FUNC_FAILED;
1422 struct hisi_sas_tmf_task tmf_task;
1424 tmf_task.tmf = TMF_CLEAR_ACA;
1425 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1430 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1432 struct sas_phy *phy = sas_get_local_phy(device);
1433 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1434 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1435 rc = sas_phy_reset(phy, reset_type);
1436 sas_put_local_phy(phy);
1441 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1443 struct hisi_sas_device *sas_dev = device->lldd_dev;
1444 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1445 struct device *dev = hisi_hba->dev;
1446 int rc = TMF_RESP_FUNC_FAILED;
1448 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1449 return TMF_RESP_FUNC_FAILED;
1450 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1452 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1453 HISI_SAS_INT_ABT_DEV, 0);
1455 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1456 return TMF_RESP_FUNC_FAILED;
1458 hisi_sas_dereg_device(hisi_hba, device);
1460 rc = hisi_sas_debug_I_T_nexus_reset(device);
1462 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1463 hisi_sas_release_task(hisi_hba, device);
1468 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1470 struct hisi_sas_device *sas_dev = device->lldd_dev;
1471 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1472 struct device *dev = hisi_hba->dev;
1473 int rc = TMF_RESP_FUNC_FAILED;
1475 sas_dev->dev_status = HISI_SAS_DEV_EH;
1476 if (dev_is_sata(device)) {
1477 struct sas_phy *phy;
1479 /* Clear internal IO and then hardreset */
1480 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1481 HISI_SAS_INT_ABT_DEV, 0);
1483 dev_err(dev, "lu_reset: internal abort failed\n");
1486 hisi_sas_dereg_device(hisi_hba, device);
1488 phy = sas_get_local_phy(device);
1490 rc = sas_phy_reset(phy, 1);
1493 hisi_sas_release_task(hisi_hba, device);
1494 sas_put_local_phy(phy);
1496 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1498 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1499 HISI_SAS_INT_ABT_DEV, 0);
1501 dev_err(dev, "lu_reset: internal abort failed\n");
1504 hisi_sas_dereg_device(hisi_hba, device);
1506 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1507 if (rc == TMF_RESP_FUNC_COMPLETE)
1508 hisi_sas_release_task(hisi_hba, device);
1511 if (rc != TMF_RESP_FUNC_COMPLETE)
1512 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1513 sas_dev->device_id, rc);
1517 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1519 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1520 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1522 queue_work(hisi_hba->wq, &r.work);
1523 wait_for_completion(r.completion);
1525 return TMF_RESP_FUNC_COMPLETE;
1527 return TMF_RESP_FUNC_FAILED;
1530 static int hisi_sas_query_task(struct sas_task *task)
1532 struct scsi_lun lun;
1533 struct hisi_sas_tmf_task tmf_task;
1534 int rc = TMF_RESP_FUNC_FAILED;
1536 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1537 struct scsi_cmnd *cmnd = task->uldd_task;
1538 struct domain_device *device = task->dev;
1539 struct hisi_sas_slot *slot = task->lldd_task;
1540 u32 tag = slot->idx;
1542 int_to_scsilun(cmnd->device->lun, &lun);
1543 tmf_task.tmf = TMF_QUERY_TASK;
1544 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1546 rc = hisi_sas_debug_issue_ssp_tmf(device,
1550 /* The task is still in Lun, release it then */
1551 case TMF_RESP_FUNC_SUCC:
1552 /* The task is not in Lun or failed, reset the phy */
1553 case TMF_RESP_FUNC_FAILED:
1554 case TMF_RESP_FUNC_COMPLETE:
1557 rc = TMF_RESP_FUNC_FAILED;
1565 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1566 struct sas_task *task, int abort_flag,
1569 struct domain_device *device = task->dev;
1570 struct hisi_sas_device *sas_dev = device->lldd_dev;
1571 struct device *dev = hisi_hba->dev;
1572 struct hisi_sas_port *port;
1573 struct hisi_sas_slot *slot;
1574 struct asd_sas_port *sas_port = device->port;
1575 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1576 struct hisi_sas_dq *dq = sas_dev->dq;
1577 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1578 unsigned long flags, flags_dq = 0;
1581 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1587 port = to_hisi_sas_port(sas_port);
1589 /* simply get a slot and send abort command */
1590 spin_lock_irqsave(&hisi_hba->lock, flags);
1591 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1593 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1596 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1598 slot = &hisi_hba->slot_info[slot_idx];
1599 memset(slot, 0, sizeof(struct hisi_sas_slot));
1601 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1602 GFP_ATOMIC, &slot->buf_dma);
1608 spin_lock_irqsave(&dq->lock, flags_dq);
1609 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1610 if (wr_q_index < 0) {
1611 spin_unlock_irqrestore(&dq->lock, flags_dq);
1614 list_add_tail(&slot->delivery, &dq->list);
1615 spin_unlock_irqrestore(&dq->lock, flags_dq);
1617 dlvry_queue = dq->id;
1618 dlvry_queue_slot = wr_q_index;
1620 slot->idx = slot_idx;
1621 slot->n_elem = n_elem;
1622 slot->dlvry_queue = dlvry_queue;
1623 slot->dlvry_queue_slot = dlvry_queue_slot;
1624 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1625 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1628 slot->is_internal = true;
1629 task->lldd_task = slot;
1631 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1632 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1633 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1635 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1636 abort_flag, task_tag);
1638 spin_lock_irqsave(&task->task_state_lock, flags);
1639 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1640 spin_unlock_irqrestore(&task->task_state_lock, flags);
1643 /* send abort command to the chip */
1644 spin_lock_irqsave(&dq->lock, flags);
1645 list_add_tail(&slot->entry, &sas_dev->list);
1646 hisi_hba->hw->start_delivery(dq);
1647 spin_unlock_irqrestore(&dq->lock, flags);
1652 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1655 spin_lock_irqsave(&hisi_hba->lock, flags);
1656 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1657 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1659 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1665 * hisi_sas_internal_task_abort -- execute an internal
1666 * abort command for single IO command or a device
1667 * @hisi_hba: host controller struct
1668 * @device: domain device
1669 * @abort_flag: mode of operation, device or single IO
1670 * @tag: tag of IO to be aborted (only relevant to single
1674 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1675 struct domain_device *device,
1676 int abort_flag, int tag)
1678 struct sas_task *task;
1679 struct hisi_sas_device *sas_dev = device->lldd_dev;
1680 struct device *dev = hisi_hba->dev;
1684 * The interface is not realized means this HW don't support internal
1685 * abort, or don't need to do internal abort. Then here, we return
1686 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1687 * the internal abort has been executed and returned CQ.
1689 if (!hisi_hba->hw->prep_abort)
1690 return TMF_RESP_FUNC_FAILED;
1692 task = sas_alloc_slow_task(GFP_KERNEL);
1697 task->task_proto = device->tproto;
1698 task->task_done = hisi_sas_task_done;
1699 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1700 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1701 add_timer(&task->slow_task->timer);
1703 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1704 task, abort_flag, tag);
1706 del_timer(&task->slow_task->timer);
1707 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1711 wait_for_completion(&task->slow_task->completion);
1712 res = TMF_RESP_FUNC_FAILED;
1714 /* Internal abort timed out */
1715 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1716 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1717 struct hisi_sas_slot *slot = task->lldd_task;
1721 dev_err(dev, "internal task abort: timeout and not done.\n");
1725 dev_err(dev, "internal task abort: timeout.\n");
1728 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1729 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1730 res = TMF_RESP_FUNC_COMPLETE;
1734 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1735 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1736 res = TMF_RESP_FUNC_SUCC;
1741 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1742 "resp: 0x%x sts 0x%x\n",
1743 SAS_ADDR(device->sas_addr),
1745 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1746 task->task_status.stat);
1747 sas_free_task(task);
1752 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1754 hisi_sas_port_notify_formed(sas_phy);
1757 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1761 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1762 u8 reg_index, u8 reg_count, u8 *write_data)
1764 struct hisi_hba *hisi_hba = sha->lldd_ha;
1766 if (!hisi_hba->hw->write_gpio)
1769 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1770 reg_index, reg_count, write_data);
1773 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1775 phy->phy_attached = 0;
1780 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1782 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1783 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1784 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1787 /* Phy down but ready */
1788 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1789 hisi_sas_port_notify_formed(sas_phy);
1791 struct hisi_sas_port *port = phy->port;
1793 /* Phy down and not ready */
1794 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1795 sas_phy_disconnected(sas_phy);
1798 if (phy->phy_type & PORT_TYPE_SAS) {
1799 int port_id = port->id;
1801 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1803 port->port_attached = 0;
1804 } else if (phy->phy_type & PORT_TYPE_SATA)
1805 port->port_attached = 0;
1807 hisi_sas_phy_disconnected(phy);
1810 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1812 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1816 for (i = 0; i < hisi_hba->queue_count; i++) {
1817 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1819 tasklet_kill(&cq->tasklet);
1822 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1824 struct scsi_transport_template *hisi_sas_stt;
1825 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1827 static struct device_attribute *host_attrs[] = {
1828 &dev_attr_phy_event_threshold,
1832 static struct scsi_host_template _hisi_sas_sht = {
1833 .module = THIS_MODULE,
1835 .queuecommand = sas_queuecommand,
1836 .target_alloc = sas_target_alloc,
1837 .slave_configure = hisi_sas_slave_configure,
1838 .scan_finished = hisi_sas_scan_finished,
1839 .scan_start = hisi_sas_scan_start,
1840 .change_queue_depth = sas_change_queue_depth,
1841 .bios_param = sas_bios_param,
1844 .sg_tablesize = SG_ALL,
1845 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1846 .use_clustering = ENABLE_CLUSTERING,
1847 .eh_device_reset_handler = sas_eh_device_reset_handler,
1848 .eh_target_reset_handler = sas_eh_target_reset_handler,
1849 .target_destroy = sas_target_destroy,
1851 .shost_attrs = host_attrs,
1853 struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1854 EXPORT_SYMBOL_GPL(hisi_sas_sht);
1856 static struct sas_domain_function_template hisi_sas_transport_ops = {
1857 .lldd_dev_found = hisi_sas_dev_found,
1858 .lldd_dev_gone = hisi_sas_dev_gone,
1859 .lldd_execute_task = hisi_sas_queue_command,
1860 .lldd_control_phy = hisi_sas_control_phy,
1861 .lldd_abort_task = hisi_sas_abort_task,
1862 .lldd_abort_task_set = hisi_sas_abort_task_set,
1863 .lldd_clear_aca = hisi_sas_clear_aca,
1864 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1865 .lldd_lu_reset = hisi_sas_lu_reset,
1866 .lldd_query_task = hisi_sas_query_task,
1867 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1868 .lldd_port_formed = hisi_sas_port_formed,
1869 .lldd_port_deformed = hisi_sas_port_deformed,
1870 .lldd_write_gpio = hisi_sas_write_gpio,
1873 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1875 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1877 for (i = 0; i < hisi_hba->queue_count; i++) {
1878 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1879 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1881 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1882 memset(hisi_hba->cmd_hdr[i], 0, s);
1885 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1886 memset(hisi_hba->complete_hdr[i], 0, s);
1890 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1891 memset(hisi_hba->initial_fis, 0, s);
1893 s = max_command_entries * sizeof(struct hisi_sas_iost);
1894 memset(hisi_hba->iost, 0, s);
1896 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1897 memset(hisi_hba->breakpoint, 0, s);
1899 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1900 memset(hisi_hba->sata_breakpoint, 0, s);
1902 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1904 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1906 struct device *dev = hisi_hba->dev;
1907 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1909 spin_lock_init(&hisi_hba->lock);
1910 for (i = 0; i < hisi_hba->n_phy; i++) {
1911 hisi_sas_phy_init(hisi_hba, i);
1912 hisi_hba->port[i].port_attached = 0;
1913 hisi_hba->port[i].id = -1;
1916 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1917 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1918 hisi_hba->devices[i].device_id = i;
1919 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1922 for (i = 0; i < hisi_hba->queue_count; i++) {
1923 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1924 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1926 /* Completion queue structure */
1928 cq->hisi_hba = hisi_hba;
1930 /* Delivery queue structure */
1931 spin_lock_init(&dq->lock);
1932 INIT_LIST_HEAD(&dq->list);
1934 dq->hisi_hba = hisi_hba;
1936 /* Delivery queue */
1937 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1938 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1939 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1940 if (!hisi_hba->cmd_hdr[i])
1943 /* Completion queue */
1944 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1945 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1946 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1947 if (!hisi_hba->complete_hdr[i])
1951 s = sizeof(struct hisi_sas_slot_buf_table);
1952 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1953 if (!hisi_hba->buffer_pool)
1956 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1957 hisi_hba->itct = dma_zalloc_coherent(dev, s, &hisi_hba->itct_dma,
1959 if (!hisi_hba->itct)
1962 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1963 sizeof(struct hisi_sas_slot),
1965 if (!hisi_hba->slot_info)
1968 s = max_command_entries * sizeof(struct hisi_sas_iost);
1969 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1971 if (!hisi_hba->iost)
1974 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1975 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1976 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1977 if (!hisi_hba->breakpoint)
1980 hisi_hba->slot_index_count = max_command_entries;
1981 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
1982 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1983 if (!hisi_hba->slot_index_tags)
1986 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1987 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1988 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1989 if (!hisi_hba->initial_fis)
1992 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1993 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1994 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1995 if (!hisi_hba->sata_breakpoint)
1997 hisi_sas_init_mem(hisi_hba);
1999 hisi_sas_slot_index_init(hisi_hba);
2001 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2002 if (!hisi_hba->wq) {
2003 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2011 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2013 void hisi_sas_free(struct hisi_hba *hisi_hba)
2015 struct device *dev = hisi_hba->dev;
2016 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
2018 for (i = 0; i < hisi_hba->queue_count; i++) {
2019 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2020 if (hisi_hba->cmd_hdr[i])
2021 dma_free_coherent(dev, s,
2022 hisi_hba->cmd_hdr[i],
2023 hisi_hba->cmd_hdr_dma[i]);
2025 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2026 if (hisi_hba->complete_hdr[i])
2027 dma_free_coherent(dev, s,
2028 hisi_hba->complete_hdr[i],
2029 hisi_hba->complete_hdr_dma[i]);
2032 dma_pool_destroy(hisi_hba->buffer_pool);
2034 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2036 dma_free_coherent(dev, s,
2037 hisi_hba->itct, hisi_hba->itct_dma);
2039 s = max_command_entries * sizeof(struct hisi_sas_iost);
2041 dma_free_coherent(dev, s,
2042 hisi_hba->iost, hisi_hba->iost_dma);
2044 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2045 if (hisi_hba->breakpoint)
2046 dma_free_coherent(dev, s,
2047 hisi_hba->breakpoint,
2048 hisi_hba->breakpoint_dma);
2051 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2052 if (hisi_hba->initial_fis)
2053 dma_free_coherent(dev, s,
2054 hisi_hba->initial_fis,
2055 hisi_hba->initial_fis_dma);
2057 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2058 if (hisi_hba->sata_breakpoint)
2059 dma_free_coherent(dev, s,
2060 hisi_hba->sata_breakpoint,
2061 hisi_hba->sata_breakpoint_dma);
2064 destroy_workqueue(hisi_hba->wq);
2066 EXPORT_SYMBOL_GPL(hisi_sas_free);
2068 void hisi_sas_rst_work_handler(struct work_struct *work)
2070 struct hisi_hba *hisi_hba =
2071 container_of(work, struct hisi_hba, rst_work);
2073 hisi_sas_controller_reset(hisi_hba);
2075 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2077 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2079 struct hisi_sas_rst *rst =
2080 container_of(work, struct hisi_sas_rst, work);
2082 if (!hisi_sas_controller_reset(rst->hisi_hba))
2084 complete(rst->completion);
2086 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2088 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2090 struct device *dev = hisi_hba->dev;
2091 struct platform_device *pdev = hisi_hba->platform_dev;
2092 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2095 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2097 dev_err(dev, "could not get property sas-addr\n");
2103 * These properties are only required for platform device-based
2104 * controller with DT firmware.
2106 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2107 "hisilicon,sas-syscon");
2108 if (IS_ERR(hisi_hba->ctrl)) {
2109 dev_err(dev, "could not get syscon\n");
2113 if (device_property_read_u32(dev, "ctrl-reset-reg",
2114 &hisi_hba->ctrl_reset_reg)) {
2116 "could not get property ctrl-reset-reg\n");
2120 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2121 &hisi_hba->ctrl_reset_sts_reg)) {
2123 "could not get property ctrl-reset-sts-reg\n");
2127 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2128 &hisi_hba->ctrl_clock_ena_reg)) {
2130 "could not get property ctrl-clock-ena-reg\n");
2135 refclk = devm_clk_get(dev, NULL);
2137 dev_dbg(dev, "no ref clk property\n");
2139 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2141 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2142 dev_err(dev, "could not get property phy-count\n");
2146 if (device_property_read_u32(dev, "queue-count",
2147 &hisi_hba->queue_count)) {
2148 dev_err(dev, "could not get property queue-count\n");
2154 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2156 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2157 const struct hisi_sas_hw *hw)
2159 struct resource *res;
2160 struct Scsi_Host *shost;
2161 struct hisi_hba *hisi_hba;
2162 struct device *dev = &pdev->dev;
2164 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
2166 dev_err(dev, "scsi host alloc failed\n");
2169 hisi_hba = shost_priv(shost);
2171 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2173 hisi_hba->dev = dev;
2174 hisi_hba->platform_dev = pdev;
2175 hisi_hba->shost = shost;
2176 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2178 timer_setup(&hisi_hba->timer, NULL, 0);
2180 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2183 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2184 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2185 dev_err(dev, "No usable DMA addressing method\n");
2189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2190 hisi_hba->regs = devm_ioremap_resource(dev, res);
2191 if (IS_ERR(hisi_hba->regs))
2194 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2196 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2197 if (IS_ERR(hisi_hba->sgpio_regs))
2201 if (hisi_sas_alloc(hisi_hba, shost)) {
2202 hisi_sas_free(hisi_hba);
2208 scsi_host_put(shost);
2209 dev_err(dev, "shost alloc failed\n");
2213 int hisi_sas_probe(struct platform_device *pdev,
2214 const struct hisi_sas_hw *hw)
2216 struct Scsi_Host *shost;
2217 struct hisi_hba *hisi_hba;
2218 struct device *dev = &pdev->dev;
2219 struct asd_sas_phy **arr_phy;
2220 struct asd_sas_port **arr_port;
2221 struct sas_ha_struct *sha;
2222 int rc, phy_nr, port_nr, i;
2224 shost = hisi_sas_shost_alloc(pdev, hw);
2228 sha = SHOST_TO_SAS_HA(shost);
2229 hisi_hba = shost_priv(shost);
2230 platform_set_drvdata(pdev, sha);
2232 phy_nr = port_nr = hisi_hba->n_phy;
2234 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2235 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2236 if (!arr_phy || !arr_port) {
2241 sha->sas_phy = arr_phy;
2242 sha->sas_port = arr_port;
2243 sha->lldd_ha = hisi_hba;
2245 shost->transportt = hisi_sas_stt;
2246 shost->max_id = HISI_SAS_MAX_DEVICES;
2247 shost->max_lun = ~0;
2248 shost->max_channel = 1;
2249 shost->max_cmd_len = 16;
2250 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2251 shost->can_queue = hisi_hba->hw->max_command_entries;
2252 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2254 sha->sas_ha_name = DRV_NAME;
2255 sha->dev = hisi_hba->dev;
2256 sha->lldd_module = THIS_MODULE;
2257 sha->sas_addr = &hisi_hba->sas_addr[0];
2258 sha->num_phys = hisi_hba->n_phy;
2259 sha->core.shost = hisi_hba->shost;
2261 for (i = 0; i < hisi_hba->n_phy; i++) {
2262 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2263 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2266 rc = scsi_add_host(shost, &pdev->dev);
2270 rc = sas_register_ha(sha);
2272 goto err_out_register_ha;
2274 rc = hisi_hba->hw->hw_init(hisi_hba);
2276 goto err_out_register_ha;
2278 scsi_scan_host(shost);
2282 err_out_register_ha:
2283 scsi_remove_host(shost);
2285 hisi_sas_free(hisi_hba);
2286 scsi_host_put(shost);
2289 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2291 int hisi_sas_remove(struct platform_device *pdev)
2293 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2294 struct hisi_hba *hisi_hba = sha->lldd_ha;
2295 struct Scsi_Host *shost = sha->core.shost;
2297 if (timer_pending(&hisi_hba->timer))
2298 del_timer(&hisi_hba->timer);
2300 sas_unregister_ha(sha);
2301 sas_remove_host(sha->core.shost);
2303 hisi_sas_free(hisi_hba);
2304 scsi_host_put(shost);
2307 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2309 static __init int hisi_sas_init(void)
2311 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2318 static __exit void hisi_sas_exit(void)
2320 sas_release_transport(hisi_sas_stt);
2323 module_init(hisi_sas_init);
2324 module_exit(hisi_sas_exit);
2326 MODULE_LICENSE("GPL");
2327 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2328 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2329 MODULE_ALIAS("platform:" DRV_NAME);