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 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
23 return device->port->ha->lldd_ha;
26 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
28 void *bitmap = hisi_hba->slot_index_tags;
30 clear_bit(slot_idx, bitmap);
33 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
35 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
38 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
40 void *bitmap = hisi_hba->slot_index_tags;
42 set_bit(slot_idx, bitmap);
45 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
48 void *bitmap = hisi_hba->slot_index_tags;
50 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
51 if (index >= hisi_hba->slot_index_count)
52 return -SAS_QUEUE_FULL;
53 hisi_sas_slot_index_set(hisi_hba, index);
58 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
62 for (i = 0; i < hisi_hba->slot_index_count; ++i)
63 hisi_sas_slot_index_clear(hisi_hba, i);
66 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
67 struct hisi_sas_slot *slot)
69 struct device *dev = &hisi_hba->pdev->dev;
74 if (!sas_protocol_ata(task->task_proto))
76 dma_unmap_sg(dev, task->scatter, slot->n_elem,
79 if (slot->command_table)
80 dma_pool_free(hisi_hba->command_table_pool,
81 slot->command_table, slot->command_table_dma);
83 if (slot->status_buffer)
84 dma_pool_free(hisi_hba->status_buffer_pool,
85 slot->status_buffer, slot->status_buffer_dma);
88 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
91 list_del_init(&slot->entry);
92 task->lldd_task = NULL;
95 hisi_sas_slot_index_free(hisi_hba, slot->idx);
96 memset(slot, 0, sizeof(*slot));
98 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
100 static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
101 struct hisi_sas_slot *slot)
103 return hisi_hba->hw->prep_smp(hisi_hba, slot);
106 static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
107 struct hisi_sas_slot *slot, int is_tmf,
108 struct hisi_sas_tmf_task *tmf)
110 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
113 static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
114 struct hisi_sas_slot *slot)
116 return hisi_hba->hw->prep_stp(hisi_hba, slot);
120 * This function will issue an abort TMF regardless of whether the
121 * task is in the sdev or not. Then it will do the task complete
122 * cleanup and callbacks.
124 static void hisi_sas_slot_abort(struct work_struct *work)
126 struct hisi_sas_slot *abort_slot =
127 container_of(work, struct hisi_sas_slot, abort_slot);
128 struct sas_task *task = abort_slot->task;
129 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
130 struct scsi_cmnd *cmnd = task->uldd_task;
131 struct hisi_sas_tmf_task tmf_task;
132 struct domain_device *device = task->dev;
133 struct hisi_sas_device *sas_dev = device->lldd_dev;
135 struct device *dev = &hisi_hba->pdev->dev;
136 int tag = abort_slot->idx;
138 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
139 dev_err(dev, "cannot abort slot for non-ssp task\n");
143 int_to_scsilun(cmnd->device->lun, &lun);
144 tmf_task.tmf = TMF_ABORT_TASK;
145 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
147 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
149 /* Do cleanup for this task */
150 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
152 task->task_done(task);
153 if (sas_dev && sas_dev->running_req)
154 sas_dev->running_req--;
157 static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
158 int is_tmf, struct hisi_sas_tmf_task *tmf,
161 struct domain_device *device = task->dev;
162 struct hisi_sas_device *sas_dev = device->lldd_dev;
163 struct hisi_sas_port *port;
164 struct hisi_sas_slot *slot;
165 struct hisi_sas_cmd_hdr *cmd_hdr_base;
166 struct device *dev = &hisi_hba->pdev->dev;
167 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
170 struct task_status_struct *ts = &task->task_status;
172 ts->resp = SAS_TASK_UNDELIVERED;
173 ts->stat = SAS_PHY_DOWN;
175 * libsas will use dev->port, should
176 * not call task_done for sata
178 if (device->dev_type != SAS_SATA_DEV)
179 task->task_done(task);
183 if (DEV_IS_GONE(sas_dev)) {
185 dev_info(dev, "task prep: device %llu not ready\n",
188 dev_info(dev, "task prep: device %016llx not ready\n",
189 SAS_ADDR(device->sas_addr));
194 port = device->port->lldd_port;
195 if (port && !port->port_attached && !tmf) {
196 if (sas_protocol_ata(task->task_proto)) {
197 struct task_status_struct *ts = &task->task_status;
200 "task prep: SATA/STP port%d not attach device\n",
202 ts->resp = SAS_TASK_COMPLETE;
203 ts->stat = SAS_PHY_DOWN;
204 task->task_done(task);
206 struct task_status_struct *ts = &task->task_status;
209 "task prep: SAS port%d does not attach device\n",
211 ts->resp = SAS_TASK_UNDELIVERED;
212 ts->stat = SAS_PHY_DOWN;
213 task->task_done(task);
218 if (!sas_protocol_ata(task->task_proto)) {
219 if (task->num_scatter) {
220 n_elem = dma_map_sg(dev, task->scatter,
221 task->num_scatter, task->data_dir);
228 n_elem = task->num_scatter;
230 if (hisi_hba->hw->slot_index_alloc)
231 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
234 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
237 rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue,
242 slot = &hisi_hba->slot_info[slot_idx];
243 memset(slot, 0, sizeof(struct hisi_sas_slot));
245 slot->idx = slot_idx;
246 slot->n_elem = n_elem;
247 slot->dlvry_queue = dlvry_queue;
248 slot->dlvry_queue_slot = dlvry_queue_slot;
249 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
250 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
253 task->lldd_task = slot;
254 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
256 slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
258 &slot->status_buffer_dma);
259 if (!slot->status_buffer) {
261 goto err_out_slot_buf;
263 memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ);
265 slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool,
267 &slot->command_table_dma);
268 if (!slot->command_table) {
270 goto err_out_status_buf;
272 memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ);
273 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
275 switch (task->task_proto) {
276 case SAS_PROTOCOL_SMP:
277 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
279 case SAS_PROTOCOL_SSP:
280 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
282 case SAS_PROTOCOL_SATA:
283 case SAS_PROTOCOL_STP:
284 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
285 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
288 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
295 dev_err(dev, "task prep: rc = 0x%x\n", rc);
298 goto err_out_command_table;
301 list_add_tail(&slot->entry, &port->list);
302 spin_lock(&task->task_state_lock);
303 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
304 spin_unlock(&task->task_state_lock);
306 hisi_hba->slot_prep = slot;
308 sas_dev->running_req++;
314 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
316 err_out_command_table:
317 dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
318 slot->command_table_dma);
320 dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
321 slot->status_buffer_dma);
323 /* Nothing to be done */
325 hisi_sas_slot_index_free(hisi_hba, slot_idx);
327 dev_err(dev, "task prep: failed[%d]!\n", rc);
328 if (!sas_protocol_ata(task->task_proto))
330 dma_unmap_sg(dev, task->scatter, n_elem,
336 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
337 int is_tmf, struct hisi_sas_tmf_task *tmf)
342 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
343 struct device *dev = &hisi_hba->pdev->dev;
345 /* protect task_prep and start_delivery sequence */
346 spin_lock_irqsave(&hisi_hba->lock, flags);
347 rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass);
349 dev_err(dev, "task exec: failed[%d]!\n", rc);
352 hisi_hba->hw->start_delivery(hisi_hba);
353 spin_unlock_irqrestore(&hisi_hba->lock, flags);
358 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
360 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
361 struct asd_sas_phy *sas_phy = &phy->sas_phy;
362 struct sas_ha_struct *sas_ha;
364 if (!phy->phy_attached)
367 sas_ha = &hisi_hba->sha;
368 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
371 struct sas_phy *sphy = sas_phy->phy;
373 sphy->negotiated_linkrate = sas_phy->linkrate;
374 sphy->minimum_linkrate = phy->minimum_linkrate;
375 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
376 sphy->maximum_linkrate = phy->maximum_linkrate;
379 if (phy->phy_type & PORT_TYPE_SAS) {
380 struct sas_identify_frame *id;
382 id = (struct sas_identify_frame *)phy->frame_rcvd;
383 id->dev_type = phy->identify.device_type;
384 id->initiator_bits = SAS_PROTOCOL_ALL;
385 id->target_bits = phy->identify.target_port_protocols;
386 } else if (phy->phy_type & PORT_TYPE_SATA) {
390 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
391 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
394 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
396 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
397 struct hisi_sas_device *sas_dev = NULL;
400 spin_lock(&hisi_hba->lock);
401 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
402 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
403 hisi_hba->devices[i].device_id = i;
404 sas_dev = &hisi_hba->devices[i];
405 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
406 sas_dev->dev_type = device->dev_type;
407 sas_dev->hisi_hba = hisi_hba;
408 sas_dev->sas_device = device;
412 spin_unlock(&hisi_hba->lock);
417 static int hisi_sas_dev_found(struct domain_device *device)
419 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
420 struct domain_device *parent_dev = device->parent;
421 struct hisi_sas_device *sas_dev;
422 struct device *dev = &hisi_hba->pdev->dev;
424 if (hisi_hba->hw->alloc_dev)
425 sas_dev = hisi_hba->hw->alloc_dev(device);
427 sas_dev = hisi_sas_alloc_dev(device);
429 dev_err(dev, "fail alloc dev: max support %d devices\n",
430 HISI_SAS_MAX_DEVICES);
434 device->lldd_dev = sas_dev;
435 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
437 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
439 u8 phy_num = parent_dev->ex_dev.num_phys;
442 for (phy_no = 0; phy_no < phy_num; phy_no++) {
443 phy = &parent_dev->ex_dev.ex_phy[phy_no];
444 if (SAS_ADDR(phy->attached_sas_addr) ==
445 SAS_ADDR(device->sas_addr)) {
446 sas_dev->attached_phy = phy_no;
451 if (phy_no == phy_num) {
452 dev_info(dev, "dev found: no attached "
453 "dev:%016llx at ex:%016llx\n",
454 SAS_ADDR(device->sas_addr),
455 SAS_ADDR(parent_dev->sas_addr));
463 static int hisi_sas_slave_configure(struct scsi_device *sdev)
465 struct domain_device *dev = sdev_to_domain_dev(sdev);
466 int ret = sas_slave_configure(sdev);
470 if (!dev_is_sata(dev))
471 sas_change_queue_depth(sdev, 64);
476 static void hisi_sas_scan_start(struct Scsi_Host *shost)
478 struct hisi_hba *hisi_hba = shost_priv(shost);
481 for (i = 0; i < hisi_hba->n_phy; ++i)
482 hisi_sas_bytes_dmaed(hisi_hba, i);
484 hisi_hba->scan_finished = 1;
487 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
489 struct hisi_hba *hisi_hba = shost_priv(shost);
490 struct sas_ha_struct *sha = &hisi_hba->sha;
492 if (hisi_hba->scan_finished == 0)
499 static void hisi_sas_phyup_work(struct work_struct *work)
501 struct hisi_sas_phy *phy =
502 container_of(work, struct hisi_sas_phy, phyup_ws);
503 struct hisi_hba *hisi_hba = phy->hisi_hba;
504 struct asd_sas_phy *sas_phy = &phy->sas_phy;
505 int phy_no = sas_phy->id;
507 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
508 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
511 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
513 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
514 struct asd_sas_phy *sas_phy = &phy->sas_phy;
516 phy->hisi_hba = hisi_hba;
518 init_timer(&phy->timer);
519 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
520 sas_phy->class = SAS;
521 sas_phy->iproto = SAS_PROTOCOL_ALL;
523 sas_phy->type = PHY_TYPE_PHYSICAL;
524 sas_phy->role = PHY_ROLE_INITIATOR;
525 sas_phy->oob_mode = OOB_NOT_CONNECTED;
526 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
527 sas_phy->id = phy_no;
528 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
529 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
530 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
531 sas_phy->lldd_phy = phy;
533 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
536 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
538 struct sas_ha_struct *sas_ha = sas_phy->ha;
539 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
540 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
541 struct asd_sas_port *sas_port = sas_phy->port;
542 struct hisi_sas_port *port = &hisi_hba->port[sas_phy->id];
548 spin_lock_irqsave(&hisi_hba->lock, flags);
549 port->port_attached = 1;
550 port->id = phy->port_id;
552 sas_port->lldd_port = port;
553 spin_unlock_irqrestore(&hisi_hba->lock, flags);
556 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no,
557 struct domain_device *device)
559 struct hisi_sas_phy *phy;
560 struct hisi_sas_port *port;
561 struct hisi_sas_slot *slot, *slot2;
562 struct device *dev = &hisi_hba->pdev->dev;
564 phy = &hisi_hba->phy[phy_no];
569 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
570 struct sas_task *task;
573 if (device && task->dev != device)
576 dev_info(dev, "Release slot [%d:%d], task [%p]:\n",
577 slot->dlvry_queue, slot->dlvry_queue_slot, task);
578 hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
582 static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy)
584 struct domain_device *device;
585 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
586 struct asd_sas_port *sas_port = sas_phy->port;
588 list_for_each_entry(device, &sas_port->dev_list, dev_list_node)
589 hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device);
592 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
593 struct domain_device *device)
595 struct asd_sas_port *port = device->port;
596 struct asd_sas_phy *sas_phy;
598 list_for_each_entry(sas_phy, &port->phy_list, port_phy_el)
599 hisi_sas_do_release_task(hisi_hba, sas_phy->id, device);
602 static void hisi_sas_dev_gone(struct domain_device *device)
604 struct hisi_sas_device *sas_dev = device->lldd_dev;
605 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
606 struct device *dev = &hisi_hba->pdev->dev;
607 u64 dev_id = sas_dev->device_id;
609 dev_info(dev, "found dev[%lld:%x] is gone\n",
610 sas_dev->device_id, sas_dev->dev_type);
612 hisi_hba->hw->free_device(hisi_hba, sas_dev);
613 device->lldd_dev = NULL;
614 memset(sas_dev, 0, sizeof(*sas_dev));
615 sas_dev->device_id = dev_id;
616 sas_dev->dev_type = SAS_PHY_UNUSED;
617 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
620 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
622 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
625 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
628 struct sas_ha_struct *sas_ha = sas_phy->ha;
629 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
630 int phy_no = sas_phy->id;
633 case PHY_FUNC_HARD_RESET:
634 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
637 case PHY_FUNC_LINK_RESET:
638 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
639 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
642 case PHY_FUNC_DISABLE:
643 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
646 case PHY_FUNC_SET_LINK_RATE:
647 case PHY_FUNC_RELEASE_SPINUP_HOLD:
654 static void hisi_sas_task_done(struct sas_task *task)
656 if (!del_timer(&task->slow_task->timer))
658 complete(&task->slow_task->completion);
661 static void hisi_sas_tmf_timedout(unsigned long data)
663 struct sas_task *task = (struct sas_task *)data;
665 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
666 complete(&task->slow_task->completion);
669 #define TASK_TIMEOUT 20
671 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
672 void *parameter, u32 para_len,
673 struct hisi_sas_tmf_task *tmf)
675 struct hisi_sas_device *sas_dev = device->lldd_dev;
676 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
677 struct device *dev = &hisi_hba->pdev->dev;
678 struct sas_task *task;
681 for (retry = 0; retry < TASK_RETRY; retry++) {
682 task = sas_alloc_slow_task(GFP_KERNEL);
687 task->task_proto = device->tproto;
689 memcpy(&task->ssp_task, parameter, para_len);
690 task->task_done = hisi_sas_task_done;
692 task->slow_task->timer.data = (unsigned long) task;
693 task->slow_task->timer.function = hisi_sas_tmf_timedout;
694 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
695 add_timer(&task->slow_task->timer);
697 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
700 del_timer(&task->slow_task->timer);
701 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
706 wait_for_completion(&task->slow_task->completion);
707 res = TMF_RESP_FUNC_FAILED;
708 /* Even TMF timed out, return direct. */
709 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
710 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
711 dev_err(dev, "abort tmf: TMF task[%d] timeout\n",
712 tmf->tag_of_task_to_be_managed);
713 if (task->lldd_task) {
714 struct hisi_sas_slot *slot =
717 hisi_sas_slot_task_free(hisi_hba,
725 if (task->task_status.resp == SAS_TASK_COMPLETE &&
726 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
727 res = TMF_RESP_FUNC_COMPLETE;
731 if (task->task_status.resp == SAS_TASK_COMPLETE &&
732 task->task_status.stat == SAS_DATA_UNDERRUN) {
733 /* no error, but return the number of bytes of
736 dev_warn(dev, "abort tmf: task to dev %016llx "
737 "resp: 0x%x sts 0x%x underrun\n",
738 SAS_ADDR(device->sas_addr),
739 task->task_status.resp,
740 task->task_status.stat);
741 res = task->task_status.residual;
745 if (task->task_status.resp == SAS_TASK_COMPLETE &&
746 task->task_status.stat == SAS_DATA_OVERRUN) {
747 dev_warn(dev, "abort tmf: blocked task error\n");
752 dev_warn(dev, "abort tmf: task to dev "
753 "%016llx resp: 0x%x status 0x%x\n",
754 SAS_ADDR(device->sas_addr), task->task_status.resp,
755 task->task_status.stat);
760 WARN_ON(retry == TASK_RETRY);
765 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
766 u8 *lun, struct hisi_sas_tmf_task *tmf)
768 struct sas_ssp_task ssp_task;
770 if (!(device->tproto & SAS_PROTOCOL_SSP))
771 return TMF_RESP_FUNC_ESUPP;
773 memcpy(ssp_task.LUN, lun, 8);
775 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
776 sizeof(ssp_task), tmf);
779 static int hisi_sas_abort_task(struct sas_task *task)
782 struct hisi_sas_tmf_task tmf_task;
783 struct domain_device *device = task->dev;
784 struct hisi_sas_device *sas_dev = device->lldd_dev;
785 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
786 struct device *dev = &hisi_hba->pdev->dev;
787 int rc = TMF_RESP_FUNC_FAILED;
791 dev_warn(dev, "Device has been removed\n");
792 return TMF_RESP_FUNC_FAILED;
795 spin_lock_irqsave(&task->task_state_lock, flags);
796 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
797 spin_unlock_irqrestore(&task->task_state_lock, flags);
798 rc = TMF_RESP_FUNC_COMPLETE;
802 spin_unlock_irqrestore(&task->task_state_lock, flags);
803 sas_dev->dev_status = HISI_SAS_DEV_EH;
804 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
805 struct scsi_cmnd *cmnd = task->uldd_task;
806 struct hisi_sas_slot *slot = task->lldd_task;
809 int_to_scsilun(cmnd->device->lun, &lun);
810 tmf_task.tmf = TMF_ABORT_TASK;
811 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
813 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
816 /* if successful, clear the task and callback forwards.*/
817 if (rc == TMF_RESP_FUNC_COMPLETE) {
818 if (task->lldd_task) {
819 struct hisi_sas_slot *slot;
821 slot = &hisi_hba->slot_info
822 [tmf_task.tag_of_task_to_be_managed];
823 spin_lock_irqsave(&hisi_hba->lock, flags);
824 hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
825 spin_unlock_irqrestore(&hisi_hba->lock, flags);
829 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
830 task->task_proto & SAS_PROTOCOL_STP) {
831 if (task->dev->dev_type == SAS_SATA_DEV) {
832 struct hisi_slot_info *slot = task->lldd_task;
834 dev_notice(dev, "abort task: hba=%p task=%p slot=%p\n",
835 hisi_hba, task, slot);
836 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
837 rc = TMF_RESP_FUNC_COMPLETE;
844 if (rc != TMF_RESP_FUNC_COMPLETE)
845 dev_notice(dev, "abort task: rc=%d\n", rc);
849 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
851 struct hisi_sas_tmf_task tmf_task;
852 int rc = TMF_RESP_FUNC_FAILED;
854 tmf_task.tmf = TMF_ABORT_TASK_SET;
855 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
860 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
862 int rc = TMF_RESP_FUNC_FAILED;
863 struct hisi_sas_tmf_task tmf_task;
865 tmf_task.tmf = TMF_CLEAR_ACA;
866 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
871 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
873 struct sas_phy *phy = sas_get_local_phy(device);
874 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
875 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
876 rc = sas_phy_reset(phy, reset_type);
877 sas_put_local_phy(phy);
882 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
884 struct hisi_sas_device *sas_dev = device->lldd_dev;
885 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
887 int rc = TMF_RESP_FUNC_FAILED;
889 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
890 return TMF_RESP_FUNC_FAILED;
891 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
893 rc = hisi_sas_debug_I_T_nexus_reset(device);
895 spin_lock_irqsave(&hisi_hba->lock, flags);
896 hisi_sas_release_task(hisi_hba, device);
897 spin_unlock_irqrestore(&hisi_hba->lock, flags);
902 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
904 struct hisi_sas_tmf_task tmf_task;
905 struct hisi_sas_device *sas_dev = device->lldd_dev;
906 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
907 struct device *dev = &hisi_hba->pdev->dev;
909 int rc = TMF_RESP_FUNC_FAILED;
911 tmf_task.tmf = TMF_LU_RESET;
912 sas_dev->dev_status = HISI_SAS_DEV_EH;
913 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
914 if (rc == TMF_RESP_FUNC_COMPLETE) {
915 spin_lock_irqsave(&hisi_hba->lock, flags);
916 hisi_sas_release_task(hisi_hba, device);
917 spin_unlock_irqrestore(&hisi_hba->lock, flags);
920 /* If failed, fall-through I_T_Nexus reset */
921 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
922 sas_dev->device_id, rc);
926 static int hisi_sas_query_task(struct sas_task *task)
929 struct hisi_sas_tmf_task tmf_task;
930 int rc = TMF_RESP_FUNC_FAILED;
932 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
933 struct scsi_cmnd *cmnd = task->uldd_task;
934 struct domain_device *device = task->dev;
935 struct hisi_sas_slot *slot = task->lldd_task;
938 int_to_scsilun(cmnd->device->lun, &lun);
939 tmf_task.tmf = TMF_QUERY_TASK;
940 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
942 rc = hisi_sas_debug_issue_ssp_tmf(device,
946 /* The task is still in Lun, release it then */
947 case TMF_RESP_FUNC_SUCC:
948 /* The task is not in Lun or failed, reset the phy */
949 case TMF_RESP_FUNC_FAILED:
950 case TMF_RESP_FUNC_COMPLETE:
957 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
959 hisi_sas_port_notify_formed(sas_phy);
962 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
964 hisi_sas_port_notify_deformed(sas_phy);
967 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
969 phy->phy_attached = 0;
974 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
976 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
977 struct asd_sas_phy *sas_phy = &phy->sas_phy;
978 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
981 /* Phy down but ready */
982 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
983 hisi_sas_port_notify_formed(sas_phy);
985 struct hisi_sas_port *port = phy->port;
987 /* Phy down and not ready */
988 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
989 sas_phy_disconnected(sas_phy);
992 if (phy->phy_type & PORT_TYPE_SAS) {
993 int port_id = port->id;
995 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
997 port->port_attached = 0;
998 } else if (phy->phy_type & PORT_TYPE_SATA)
999 port->port_attached = 0;
1001 hisi_sas_phy_disconnected(phy);
1004 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1006 static struct scsi_transport_template *hisi_sas_stt;
1008 static struct scsi_host_template hisi_sas_sht = {
1009 .module = THIS_MODULE,
1011 .queuecommand = sas_queuecommand,
1012 .target_alloc = sas_target_alloc,
1013 .slave_configure = hisi_sas_slave_configure,
1014 .scan_finished = hisi_sas_scan_finished,
1015 .scan_start = hisi_sas_scan_start,
1016 .change_queue_depth = sas_change_queue_depth,
1017 .bios_param = sas_bios_param,
1020 .sg_tablesize = SG_ALL,
1021 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1022 .use_clustering = ENABLE_CLUSTERING,
1023 .eh_device_reset_handler = sas_eh_device_reset_handler,
1024 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1025 .target_destroy = sas_target_destroy,
1029 static struct sas_domain_function_template hisi_sas_transport_ops = {
1030 .lldd_dev_found = hisi_sas_dev_found,
1031 .lldd_dev_gone = hisi_sas_dev_gone,
1032 .lldd_execute_task = hisi_sas_queue_command,
1033 .lldd_control_phy = hisi_sas_control_phy,
1034 .lldd_abort_task = hisi_sas_abort_task,
1035 .lldd_abort_task_set = hisi_sas_abort_task_set,
1036 .lldd_clear_aca = hisi_sas_clear_aca,
1037 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1038 .lldd_lu_reset = hisi_sas_lu_reset,
1039 .lldd_query_task = hisi_sas_query_task,
1040 .lldd_port_formed = hisi_sas_port_formed,
1041 .lldd_port_deformed = hisi_sas_port_deformed,
1044 static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1046 struct platform_device *pdev = hisi_hba->pdev;
1047 struct device *dev = &pdev->dev;
1048 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1050 spin_lock_init(&hisi_hba->lock);
1051 for (i = 0; i < hisi_hba->n_phy; i++) {
1052 hisi_sas_phy_init(hisi_hba, i);
1053 hisi_hba->port[i].port_attached = 0;
1054 hisi_hba->port[i].id = -1;
1055 INIT_LIST_HEAD(&hisi_hba->port[i].list);
1058 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1059 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1060 hisi_hba->devices[i].device_id = i;
1061 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1064 for (i = 0; i < hisi_hba->queue_count; i++) {
1065 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1067 /* Completion queue structure */
1069 cq->hisi_hba = hisi_hba;
1071 /* Delivery queue */
1072 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1073 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1074 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1075 if (!hisi_hba->cmd_hdr[i])
1077 memset(hisi_hba->cmd_hdr[i], 0, s);
1079 /* Completion queue */
1080 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1081 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1082 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1083 if (!hisi_hba->complete_hdr[i])
1085 memset(hisi_hba->complete_hdr[i], 0, s);
1088 s = HISI_SAS_STATUS_BUF_SZ;
1089 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1091 if (!hisi_hba->status_buffer_pool)
1094 s = HISI_SAS_COMMAND_TABLE_SZ;
1095 hisi_hba->command_table_pool = dma_pool_create("command_table",
1097 if (!hisi_hba->command_table_pool)
1100 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1101 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1103 if (!hisi_hba->itct)
1106 memset(hisi_hba->itct, 0, s);
1108 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1109 sizeof(struct hisi_sas_slot),
1111 if (!hisi_hba->slot_info)
1114 s = max_command_entries * sizeof(struct hisi_sas_iost);
1115 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1117 if (!hisi_hba->iost)
1120 memset(hisi_hba->iost, 0, s);
1122 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1123 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1124 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1125 if (!hisi_hba->breakpoint)
1128 memset(hisi_hba->breakpoint, 0, s);
1130 hisi_hba->slot_index_count = max_command_entries;
1131 s = hisi_hba->slot_index_count / sizeof(unsigned long);
1132 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1133 if (!hisi_hba->slot_index_tags)
1136 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1137 sizeof(struct hisi_sas_sge_page), 16, 0);
1138 if (!hisi_hba->sge_page_pool)
1141 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1142 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1143 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1144 if (!hisi_hba->initial_fis)
1146 memset(hisi_hba->initial_fis, 0, s);
1148 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1149 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1150 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1151 if (!hisi_hba->sata_breakpoint)
1153 memset(hisi_hba->sata_breakpoint, 0, s);
1155 hisi_sas_slot_index_init(hisi_hba);
1157 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1158 if (!hisi_hba->wq) {
1159 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1168 static void hisi_sas_free(struct hisi_hba *hisi_hba)
1170 struct device *dev = &hisi_hba->pdev->dev;
1171 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1173 for (i = 0; i < hisi_hba->queue_count; i++) {
1174 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1175 if (hisi_hba->cmd_hdr[i])
1176 dma_free_coherent(dev, s,
1177 hisi_hba->cmd_hdr[i],
1178 hisi_hba->cmd_hdr_dma[i]);
1180 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1181 if (hisi_hba->complete_hdr[i])
1182 dma_free_coherent(dev, s,
1183 hisi_hba->complete_hdr[i],
1184 hisi_hba->complete_hdr_dma[i]);
1187 dma_pool_destroy(hisi_hba->status_buffer_pool);
1188 dma_pool_destroy(hisi_hba->command_table_pool);
1189 dma_pool_destroy(hisi_hba->sge_page_pool);
1191 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1193 dma_free_coherent(dev, s,
1194 hisi_hba->itct, hisi_hba->itct_dma);
1196 s = max_command_entries * sizeof(struct hisi_sas_iost);
1198 dma_free_coherent(dev, s,
1199 hisi_hba->iost, hisi_hba->iost_dma);
1201 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1202 if (hisi_hba->breakpoint)
1203 dma_free_coherent(dev, s,
1204 hisi_hba->breakpoint,
1205 hisi_hba->breakpoint_dma);
1208 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1209 if (hisi_hba->initial_fis)
1210 dma_free_coherent(dev, s,
1211 hisi_hba->initial_fis,
1212 hisi_hba->initial_fis_dma);
1214 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1215 if (hisi_hba->sata_breakpoint)
1216 dma_free_coherent(dev, s,
1217 hisi_hba->sata_breakpoint,
1218 hisi_hba->sata_breakpoint_dma);
1221 destroy_workqueue(hisi_hba->wq);
1224 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1225 const struct hisi_sas_hw *hw)
1227 struct resource *res;
1228 struct Scsi_Host *shost;
1229 struct hisi_hba *hisi_hba;
1230 struct device *dev = &pdev->dev;
1231 struct device_node *np = pdev->dev.of_node;
1233 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
1236 hisi_hba = shost_priv(shost);
1239 hisi_hba->pdev = pdev;
1240 hisi_hba->shost = shost;
1241 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1243 init_timer(&hisi_hba->timer);
1245 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1250 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1251 "hisilicon,sas-syscon");
1252 if (IS_ERR(hisi_hba->ctrl))
1255 if (device_property_read_u32(dev, "ctrl-reset-reg",
1256 &hisi_hba->ctrl_reset_reg))
1259 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1260 &hisi_hba->ctrl_reset_sts_reg))
1263 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1264 &hisi_hba->ctrl_clock_ena_reg))
1268 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
1271 if (device_property_read_u32(dev, "queue-count",
1272 &hisi_hba->queue_count))
1275 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1276 hisi_hba->regs = devm_ioremap_resource(dev, res);
1277 if (IS_ERR(hisi_hba->regs))
1280 if (hisi_sas_alloc(hisi_hba, shost)) {
1281 hisi_sas_free(hisi_hba);
1287 dev_err(dev, "shost alloc failed\n");
1291 static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1295 for (i = 0; i < hisi_hba->n_phy; i++)
1296 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1301 int hisi_sas_probe(struct platform_device *pdev,
1302 const struct hisi_sas_hw *hw)
1304 struct Scsi_Host *shost;
1305 struct hisi_hba *hisi_hba;
1306 struct device *dev = &pdev->dev;
1307 struct asd_sas_phy **arr_phy;
1308 struct asd_sas_port **arr_port;
1309 struct sas_ha_struct *sha;
1310 int rc, phy_nr, port_nr, i;
1312 shost = hisi_sas_shost_alloc(pdev, hw);
1318 sha = SHOST_TO_SAS_HA(shost);
1319 hisi_hba = shost_priv(shost);
1320 platform_set_drvdata(pdev, sha);
1322 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1323 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1324 dev_err(dev, "No usable DMA addressing method\n");
1329 phy_nr = port_nr = hisi_hba->n_phy;
1331 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1332 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
1333 if (!arr_phy || !arr_port)
1336 sha->sas_phy = arr_phy;
1337 sha->sas_port = arr_port;
1338 sha->core.shost = shost;
1339 sha->lldd_ha = hisi_hba;
1341 shost->transportt = hisi_sas_stt;
1342 shost->max_id = HISI_SAS_MAX_DEVICES;
1343 shost->max_lun = ~0;
1344 shost->max_channel = 1;
1345 shost->max_cmd_len = 16;
1346 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
1347 shost->can_queue = hisi_hba->hw->max_command_entries;
1348 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
1350 sha->sas_ha_name = DRV_NAME;
1351 sha->dev = &hisi_hba->pdev->dev;
1352 sha->lldd_module = THIS_MODULE;
1353 sha->sas_addr = &hisi_hba->sas_addr[0];
1354 sha->num_phys = hisi_hba->n_phy;
1355 sha->core.shost = hisi_hba->shost;
1357 for (i = 0; i < hisi_hba->n_phy; i++) {
1358 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1359 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1362 hisi_sas_init_add(hisi_hba);
1364 rc = hisi_hba->hw->hw_init(hisi_hba);
1368 rc = scsi_add_host(shost, &pdev->dev);
1372 rc = sas_register_ha(sha);
1374 goto err_out_register_ha;
1376 scsi_scan_host(shost);
1380 err_out_register_ha:
1381 scsi_remove_host(shost);
1386 EXPORT_SYMBOL_GPL(hisi_sas_probe);
1388 int hisi_sas_remove(struct platform_device *pdev)
1390 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1391 struct hisi_hba *hisi_hba = sha->lldd_ha;
1393 scsi_remove_host(sha->core.shost);
1394 sas_unregister_ha(sha);
1395 sas_remove_host(sha->core.shost);
1397 hisi_sas_free(hisi_hba);
1400 EXPORT_SYMBOL_GPL(hisi_sas_remove);
1402 static __init int hisi_sas_init(void)
1404 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1406 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1413 static __exit void hisi_sas_exit(void)
1415 sas_release_transport(hisi_sas_stt);
1418 module_init(hisi_sas_init);
1419 module_exit(hisi_sas_exit);
1421 MODULE_VERSION(DRV_VERSION);
1422 MODULE_LICENSE("GPL");
1423 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1424 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1425 MODULE_ALIAS("platform:" DRV_NAME);