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);
25 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
27 return device->port->ha->lldd_ha;
30 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
32 void *bitmap = hisi_hba->slot_index_tags;
34 clear_bit(slot_idx, bitmap);
37 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
39 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
42 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
44 void *bitmap = hisi_hba->slot_index_tags;
46 set_bit(slot_idx, bitmap);
49 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
52 void *bitmap = hisi_hba->slot_index_tags;
54 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
55 if (index >= hisi_hba->slot_index_count)
56 return -SAS_QUEUE_FULL;
57 hisi_sas_slot_index_set(hisi_hba, index);
62 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
66 for (i = 0; i < hisi_hba->slot_index_count; ++i)
67 hisi_sas_slot_index_clear(hisi_hba, i);
70 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
71 struct hisi_sas_slot *slot)
73 struct device *dev = &hisi_hba->pdev->dev;
78 if (!sas_protocol_ata(task->task_proto))
80 dma_unmap_sg(dev, task->scatter, slot->n_elem,
83 if (slot->command_table)
84 dma_pool_free(hisi_hba->command_table_pool,
85 slot->command_table, slot->command_table_dma);
87 if (slot->status_buffer)
88 dma_pool_free(hisi_hba->status_buffer_pool,
89 slot->status_buffer, slot->status_buffer_dma);
92 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
95 list_del_init(&slot->entry);
96 task->lldd_task = NULL;
99 hisi_sas_slot_index_free(hisi_hba, slot->idx);
100 /* slot memory is fully zeroed when it is reused */
102 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
104 static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
105 struct hisi_sas_slot *slot)
107 return hisi_hba->hw->prep_smp(hisi_hba, slot);
110 static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
111 struct hisi_sas_slot *slot, int is_tmf,
112 struct hisi_sas_tmf_task *tmf)
114 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
117 static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
118 struct hisi_sas_slot *slot)
120 return hisi_hba->hw->prep_stp(hisi_hba, slot);
123 static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
124 struct hisi_sas_slot *slot,
125 int device_id, int abort_flag, int tag_to_abort)
127 return hisi_hba->hw->prep_abort(hisi_hba, slot,
128 device_id, abort_flag, tag_to_abort);
132 * This function will issue an abort TMF regardless of whether the
133 * task is in the sdev or not. Then it will do the task complete
134 * cleanup and callbacks.
136 static void hisi_sas_slot_abort(struct work_struct *work)
138 struct hisi_sas_slot *abort_slot =
139 container_of(work, struct hisi_sas_slot, abort_slot);
140 struct sas_task *task = abort_slot->task;
141 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
142 struct scsi_cmnd *cmnd = task->uldd_task;
143 struct hisi_sas_tmf_task tmf_task;
144 struct domain_device *device = task->dev;
145 struct hisi_sas_device *sas_dev = device->lldd_dev;
147 struct device *dev = &hisi_hba->pdev->dev;
148 int tag = abort_slot->idx;
150 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
151 dev_err(dev, "cannot abort slot for non-ssp task\n");
155 int_to_scsilun(cmnd->device->lun, &lun);
156 tmf_task.tmf = TMF_ABORT_TASK;
157 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
159 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
161 /* Do cleanup for this task */
162 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
164 task->task_done(task);
165 if (sas_dev && sas_dev->running_req)
166 sas_dev->running_req--;
169 static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
170 int is_tmf, struct hisi_sas_tmf_task *tmf,
173 struct domain_device *device = task->dev;
174 struct hisi_sas_device *sas_dev = device->lldd_dev;
175 struct hisi_sas_port *port;
176 struct hisi_sas_slot *slot;
177 struct hisi_sas_cmd_hdr *cmd_hdr_base;
178 struct device *dev = &hisi_hba->pdev->dev;
179 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
182 struct task_status_struct *ts = &task->task_status;
184 ts->resp = SAS_TASK_UNDELIVERED;
185 ts->stat = SAS_PHY_DOWN;
187 * libsas will use dev->port, should
188 * not call task_done for sata
190 if (device->dev_type != SAS_SATA_DEV)
191 task->task_done(task);
195 if (DEV_IS_GONE(sas_dev)) {
197 dev_info(dev, "task prep: device %llu not ready\n",
200 dev_info(dev, "task prep: device %016llx not ready\n",
201 SAS_ADDR(device->sas_addr));
206 port = device->port->lldd_port;
207 if (port && !port->port_attached) {
208 if (sas_protocol_ata(task->task_proto)) {
209 struct task_status_struct *ts = &task->task_status;
212 "task prep: SATA/STP port%d not attach device\n",
214 ts->resp = SAS_TASK_COMPLETE;
215 ts->stat = SAS_PHY_DOWN;
216 task->task_done(task);
218 struct task_status_struct *ts = &task->task_status;
221 "task prep: SAS port%d does not attach device\n",
223 ts->resp = SAS_TASK_UNDELIVERED;
224 ts->stat = SAS_PHY_DOWN;
225 task->task_done(task);
230 if (!sas_protocol_ata(task->task_proto)) {
231 if (task->num_scatter) {
232 n_elem = dma_map_sg(dev, task->scatter,
233 task->num_scatter, task->data_dir);
240 n_elem = task->num_scatter;
242 if (hisi_hba->hw->slot_index_alloc)
243 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
246 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
249 rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue,
254 slot = &hisi_hba->slot_info[slot_idx];
255 memset(slot, 0, sizeof(struct hisi_sas_slot));
257 slot->idx = slot_idx;
258 slot->n_elem = n_elem;
259 slot->dlvry_queue = dlvry_queue;
260 slot->dlvry_queue_slot = dlvry_queue_slot;
261 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
262 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
265 task->lldd_task = slot;
266 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
268 slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
270 &slot->status_buffer_dma);
271 if (!slot->status_buffer) {
273 goto err_out_slot_buf;
275 memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ);
277 slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool,
279 &slot->command_table_dma);
280 if (!slot->command_table) {
282 goto err_out_status_buf;
284 memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ);
285 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
287 switch (task->task_proto) {
288 case SAS_PROTOCOL_SMP:
289 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
291 case SAS_PROTOCOL_SSP:
292 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
294 case SAS_PROTOCOL_SATA:
295 case SAS_PROTOCOL_STP:
296 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
297 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
300 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
307 dev_err(dev, "task prep: rc = 0x%x\n", rc);
310 goto err_out_command_table;
313 list_add_tail(&slot->entry, &port->list);
314 spin_lock(&task->task_state_lock);
315 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
316 spin_unlock(&task->task_state_lock);
318 hisi_hba->slot_prep = slot;
320 sas_dev->running_req++;
326 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
328 err_out_command_table:
329 dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
330 slot->command_table_dma);
332 dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
333 slot->status_buffer_dma);
335 /* Nothing to be done */
337 hisi_sas_slot_index_free(hisi_hba, slot_idx);
339 dev_err(dev, "task prep: failed[%d]!\n", rc);
340 if (!sas_protocol_ata(task->task_proto))
342 dma_unmap_sg(dev, task->scatter, n_elem,
348 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
349 int is_tmf, struct hisi_sas_tmf_task *tmf)
354 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
355 struct device *dev = &hisi_hba->pdev->dev;
357 /* protect task_prep and start_delivery sequence */
358 spin_lock_irqsave(&hisi_hba->lock, flags);
359 rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass);
361 dev_err(dev, "task exec: failed[%d]!\n", rc);
364 hisi_hba->hw->start_delivery(hisi_hba);
365 spin_unlock_irqrestore(&hisi_hba->lock, flags);
370 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
372 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
373 struct asd_sas_phy *sas_phy = &phy->sas_phy;
374 struct sas_ha_struct *sas_ha;
376 if (!phy->phy_attached)
379 sas_ha = &hisi_hba->sha;
380 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
383 struct sas_phy *sphy = sas_phy->phy;
385 sphy->negotiated_linkrate = sas_phy->linkrate;
386 sphy->minimum_linkrate = phy->minimum_linkrate;
387 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
388 sphy->maximum_linkrate = phy->maximum_linkrate;
391 if (phy->phy_type & PORT_TYPE_SAS) {
392 struct sas_identify_frame *id;
394 id = (struct sas_identify_frame *)phy->frame_rcvd;
395 id->dev_type = phy->identify.device_type;
396 id->initiator_bits = SAS_PROTOCOL_ALL;
397 id->target_bits = phy->identify.target_port_protocols;
398 } else if (phy->phy_type & PORT_TYPE_SATA) {
402 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
403 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
406 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
408 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
409 struct hisi_sas_device *sas_dev = NULL;
412 spin_lock(&hisi_hba->lock);
413 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
414 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
415 hisi_hba->devices[i].device_id = i;
416 sas_dev = &hisi_hba->devices[i];
417 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
418 sas_dev->dev_type = device->dev_type;
419 sas_dev->hisi_hba = hisi_hba;
420 sas_dev->sas_device = device;
424 spin_unlock(&hisi_hba->lock);
429 static int hisi_sas_dev_found(struct domain_device *device)
431 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
432 struct domain_device *parent_dev = device->parent;
433 struct hisi_sas_device *sas_dev;
434 struct device *dev = &hisi_hba->pdev->dev;
436 if (hisi_hba->hw->alloc_dev)
437 sas_dev = hisi_hba->hw->alloc_dev(device);
439 sas_dev = hisi_sas_alloc_dev(device);
441 dev_err(dev, "fail alloc dev: max support %d devices\n",
442 HISI_SAS_MAX_DEVICES);
446 device->lldd_dev = sas_dev;
447 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
449 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
451 u8 phy_num = parent_dev->ex_dev.num_phys;
454 for (phy_no = 0; phy_no < phy_num; phy_no++) {
455 phy = &parent_dev->ex_dev.ex_phy[phy_no];
456 if (SAS_ADDR(phy->attached_sas_addr) ==
457 SAS_ADDR(device->sas_addr)) {
458 sas_dev->attached_phy = phy_no;
463 if (phy_no == phy_num) {
464 dev_info(dev, "dev found: no attached "
465 "dev:%016llx at ex:%016llx\n",
466 SAS_ADDR(device->sas_addr),
467 SAS_ADDR(parent_dev->sas_addr));
475 static int hisi_sas_slave_configure(struct scsi_device *sdev)
477 struct domain_device *dev = sdev_to_domain_dev(sdev);
478 int ret = sas_slave_configure(sdev);
482 if (!dev_is_sata(dev))
483 sas_change_queue_depth(sdev, 64);
488 static void hisi_sas_scan_start(struct Scsi_Host *shost)
490 struct hisi_hba *hisi_hba = shost_priv(shost);
493 for (i = 0; i < hisi_hba->n_phy; ++i)
494 hisi_sas_bytes_dmaed(hisi_hba, i);
496 hisi_hba->scan_finished = 1;
499 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
501 struct hisi_hba *hisi_hba = shost_priv(shost);
502 struct sas_ha_struct *sha = &hisi_hba->sha;
504 if (hisi_hba->scan_finished == 0)
511 static void hisi_sas_phyup_work(struct work_struct *work)
513 struct hisi_sas_phy *phy =
514 container_of(work, struct hisi_sas_phy, phyup_ws);
515 struct hisi_hba *hisi_hba = phy->hisi_hba;
516 struct asd_sas_phy *sas_phy = &phy->sas_phy;
517 int phy_no = sas_phy->id;
519 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
520 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
523 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
525 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
526 struct asd_sas_phy *sas_phy = &phy->sas_phy;
528 phy->hisi_hba = hisi_hba;
530 init_timer(&phy->timer);
531 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
532 sas_phy->class = SAS;
533 sas_phy->iproto = SAS_PROTOCOL_ALL;
535 sas_phy->type = PHY_TYPE_PHYSICAL;
536 sas_phy->role = PHY_ROLE_INITIATOR;
537 sas_phy->oob_mode = OOB_NOT_CONNECTED;
538 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
539 sas_phy->id = phy_no;
540 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
541 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
542 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
543 sas_phy->lldd_phy = phy;
545 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
548 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
550 struct sas_ha_struct *sas_ha = sas_phy->ha;
551 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
552 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
553 struct asd_sas_port *sas_port = sas_phy->port;
554 struct hisi_sas_port *port = &hisi_hba->port[sas_phy->id];
560 spin_lock_irqsave(&hisi_hba->lock, flags);
561 port->port_attached = 1;
562 port->id = phy->port_id;
564 sas_port->lldd_port = port;
565 spin_unlock_irqrestore(&hisi_hba->lock, flags);
568 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no,
569 struct domain_device *device)
571 struct hisi_sas_phy *phy;
572 struct hisi_sas_port *port;
573 struct hisi_sas_slot *slot, *slot2;
574 struct device *dev = &hisi_hba->pdev->dev;
576 phy = &hisi_hba->phy[phy_no];
581 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
582 struct sas_task *task;
585 if (device && task->dev != device)
588 dev_info(dev, "Release slot [%d:%d], task [%p]:\n",
589 slot->dlvry_queue, slot->dlvry_queue_slot, task);
590 hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
594 static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy)
596 struct domain_device *device;
597 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
598 struct asd_sas_port *sas_port = sas_phy->port;
600 list_for_each_entry(device, &sas_port->dev_list, dev_list_node)
601 hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device);
604 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
605 struct domain_device *device)
607 struct asd_sas_port *port = device->port;
608 struct asd_sas_phy *sas_phy;
610 list_for_each_entry(sas_phy, &port->phy_list, port_phy_el)
611 hisi_sas_do_release_task(hisi_hba, sas_phy->id, device);
614 static void hisi_sas_dev_gone(struct domain_device *device)
616 struct hisi_sas_device *sas_dev = device->lldd_dev;
617 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
618 struct device *dev = &hisi_hba->pdev->dev;
619 u64 dev_id = sas_dev->device_id;
621 dev_info(dev, "found dev[%lld:%x] is gone\n",
622 sas_dev->device_id, sas_dev->dev_type);
624 hisi_sas_internal_task_abort(hisi_hba, device,
625 HISI_SAS_INT_ABT_DEV, 0);
627 hisi_hba->hw->free_device(hisi_hba, sas_dev);
628 device->lldd_dev = NULL;
629 memset(sas_dev, 0, sizeof(*sas_dev));
630 sas_dev->device_id = dev_id;
631 sas_dev->dev_type = SAS_PHY_UNUSED;
632 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
635 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
637 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
640 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
643 struct sas_ha_struct *sas_ha = sas_phy->ha;
644 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
645 int phy_no = sas_phy->id;
648 case PHY_FUNC_HARD_RESET:
649 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
652 case PHY_FUNC_LINK_RESET:
653 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
654 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
657 case PHY_FUNC_DISABLE:
658 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
661 case PHY_FUNC_SET_LINK_RATE:
662 case PHY_FUNC_RELEASE_SPINUP_HOLD:
669 static void hisi_sas_task_done(struct sas_task *task)
671 if (!del_timer(&task->slow_task->timer))
673 complete(&task->slow_task->completion);
676 static void hisi_sas_tmf_timedout(unsigned long data)
678 struct sas_task *task = (struct sas_task *)data;
680 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
681 complete(&task->slow_task->completion);
684 #define TASK_TIMEOUT 20
686 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
687 void *parameter, u32 para_len,
688 struct hisi_sas_tmf_task *tmf)
690 struct hisi_sas_device *sas_dev = device->lldd_dev;
691 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
692 struct device *dev = &hisi_hba->pdev->dev;
693 struct sas_task *task;
696 for (retry = 0; retry < TASK_RETRY; retry++) {
697 task = sas_alloc_slow_task(GFP_KERNEL);
702 task->task_proto = device->tproto;
704 memcpy(&task->ssp_task, parameter, para_len);
705 task->task_done = hisi_sas_task_done;
707 task->slow_task->timer.data = (unsigned long) task;
708 task->slow_task->timer.function = hisi_sas_tmf_timedout;
709 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
710 add_timer(&task->slow_task->timer);
712 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
715 del_timer(&task->slow_task->timer);
716 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
721 wait_for_completion(&task->slow_task->completion);
722 res = TMF_RESP_FUNC_FAILED;
723 /* Even TMF timed out, return direct. */
724 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
725 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
726 dev_err(dev, "abort tmf: TMF task[%d] timeout\n",
727 tmf->tag_of_task_to_be_managed);
728 if (task->lldd_task) {
729 struct hisi_sas_slot *slot =
732 hisi_sas_slot_task_free(hisi_hba,
740 if (task->task_status.resp == SAS_TASK_COMPLETE &&
741 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
742 res = TMF_RESP_FUNC_COMPLETE;
746 if (task->task_status.resp == SAS_TASK_COMPLETE &&
747 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
748 res = TMF_RESP_FUNC_SUCC;
752 if (task->task_status.resp == SAS_TASK_COMPLETE &&
753 task->task_status.stat == SAS_DATA_UNDERRUN) {
754 /* no error, but return the number of bytes of
757 dev_warn(dev, "abort tmf: task to dev %016llx "
758 "resp: 0x%x sts 0x%x underrun\n",
759 SAS_ADDR(device->sas_addr),
760 task->task_status.resp,
761 task->task_status.stat);
762 res = task->task_status.residual;
766 if (task->task_status.resp == SAS_TASK_COMPLETE &&
767 task->task_status.stat == SAS_DATA_OVERRUN) {
768 dev_warn(dev, "abort tmf: blocked task error\n");
773 dev_warn(dev, "abort tmf: task to dev "
774 "%016llx resp: 0x%x status 0x%x\n",
775 SAS_ADDR(device->sas_addr), task->task_status.resp,
776 task->task_status.stat);
781 WARN_ON(retry == TASK_RETRY);
786 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
787 u8 *lun, struct hisi_sas_tmf_task *tmf)
789 struct sas_ssp_task ssp_task;
791 if (!(device->tproto & SAS_PROTOCOL_SSP))
792 return TMF_RESP_FUNC_ESUPP;
794 memcpy(ssp_task.LUN, lun, 8);
796 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
797 sizeof(ssp_task), tmf);
800 static int hisi_sas_abort_task(struct sas_task *task)
803 struct hisi_sas_tmf_task tmf_task;
804 struct domain_device *device = task->dev;
805 struct hisi_sas_device *sas_dev = device->lldd_dev;
806 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
807 struct device *dev = &hisi_hba->pdev->dev;
808 int rc = TMF_RESP_FUNC_FAILED;
812 dev_warn(dev, "Device has been removed\n");
813 return TMF_RESP_FUNC_FAILED;
816 spin_lock_irqsave(&task->task_state_lock, flags);
817 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
818 spin_unlock_irqrestore(&task->task_state_lock, flags);
819 rc = TMF_RESP_FUNC_COMPLETE;
823 spin_unlock_irqrestore(&task->task_state_lock, flags);
824 sas_dev->dev_status = HISI_SAS_DEV_EH;
825 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
826 struct scsi_cmnd *cmnd = task->uldd_task;
827 struct hisi_sas_slot *slot = task->lldd_task;
830 int_to_scsilun(cmnd->device->lun, &lun);
831 tmf_task.tmf = TMF_ABORT_TASK;
832 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
834 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
837 /* if successful, clear the task and callback forwards.*/
838 if (rc == TMF_RESP_FUNC_COMPLETE) {
839 if (task->lldd_task) {
840 struct hisi_sas_slot *slot;
842 slot = &hisi_hba->slot_info
843 [tmf_task.tag_of_task_to_be_managed];
844 spin_lock_irqsave(&hisi_hba->lock, flags);
845 hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
846 spin_unlock_irqrestore(&hisi_hba->lock, flags);
850 hisi_sas_internal_task_abort(hisi_hba, device,
851 HISI_SAS_INT_ABT_CMD, tag);
852 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
853 task->task_proto & SAS_PROTOCOL_STP) {
854 if (task->dev->dev_type == SAS_SATA_DEV) {
855 hisi_sas_internal_task_abort(hisi_hba, device,
856 HISI_SAS_INT_ABT_DEV, 0);
857 rc = TMF_RESP_FUNC_COMPLETE;
859 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
861 struct hisi_sas_slot *slot = task->lldd_task;
864 hisi_sas_internal_task_abort(hisi_hba, device,
865 HISI_SAS_INT_ABT_CMD, tag);
869 if (rc != TMF_RESP_FUNC_COMPLETE)
870 dev_notice(dev, "abort task: rc=%d\n", rc);
874 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
876 struct hisi_sas_tmf_task tmf_task;
877 int rc = TMF_RESP_FUNC_FAILED;
879 tmf_task.tmf = TMF_ABORT_TASK_SET;
880 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
885 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
887 int rc = TMF_RESP_FUNC_FAILED;
888 struct hisi_sas_tmf_task tmf_task;
890 tmf_task.tmf = TMF_CLEAR_ACA;
891 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
896 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
898 struct sas_phy *phy = sas_get_local_phy(device);
899 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
900 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
901 rc = sas_phy_reset(phy, reset_type);
902 sas_put_local_phy(phy);
907 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
909 struct hisi_sas_device *sas_dev = device->lldd_dev;
910 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
912 int rc = TMF_RESP_FUNC_FAILED;
914 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
915 return TMF_RESP_FUNC_FAILED;
916 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
918 rc = hisi_sas_debug_I_T_nexus_reset(device);
920 spin_lock_irqsave(&hisi_hba->lock, flags);
921 hisi_sas_release_task(hisi_hba, device);
922 spin_unlock_irqrestore(&hisi_hba->lock, flags);
927 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
929 struct hisi_sas_tmf_task tmf_task;
930 struct hisi_sas_device *sas_dev = device->lldd_dev;
931 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
932 struct device *dev = &hisi_hba->pdev->dev;
934 int rc = TMF_RESP_FUNC_FAILED;
936 tmf_task.tmf = TMF_LU_RESET;
937 sas_dev->dev_status = HISI_SAS_DEV_EH;
938 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
939 if (rc == TMF_RESP_FUNC_COMPLETE) {
940 spin_lock_irqsave(&hisi_hba->lock, flags);
941 hisi_sas_release_task(hisi_hba, device);
942 spin_unlock_irqrestore(&hisi_hba->lock, flags);
945 /* If failed, fall-through I_T_Nexus reset */
946 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
947 sas_dev->device_id, rc);
951 static int hisi_sas_query_task(struct sas_task *task)
954 struct hisi_sas_tmf_task tmf_task;
955 int rc = TMF_RESP_FUNC_FAILED;
957 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
958 struct scsi_cmnd *cmnd = task->uldd_task;
959 struct domain_device *device = task->dev;
960 struct hisi_sas_slot *slot = task->lldd_task;
963 int_to_scsilun(cmnd->device->lun, &lun);
964 tmf_task.tmf = TMF_QUERY_TASK;
965 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
967 rc = hisi_sas_debug_issue_ssp_tmf(device,
971 /* The task is still in Lun, release it then */
972 case TMF_RESP_FUNC_SUCC:
973 /* The task is not in Lun or failed, reset the phy */
974 case TMF_RESP_FUNC_FAILED:
975 case TMF_RESP_FUNC_COMPLETE:
983 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
984 struct sas_task *task, int abort_flag,
987 struct domain_device *device = task->dev;
988 struct hisi_sas_device *sas_dev = device->lldd_dev;
989 struct device *dev = &hisi_hba->pdev->dev;
990 struct hisi_sas_port *port;
991 struct hisi_sas_slot *slot;
992 struct hisi_sas_cmd_hdr *cmd_hdr_base;
993 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
998 port = device->port->lldd_port;
1000 /* simply get a slot and send abort command */
1001 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1004 rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue,
1009 slot = &hisi_hba->slot_info[slot_idx];
1010 memset(slot, 0, sizeof(struct hisi_sas_slot));
1012 slot->idx = slot_idx;
1013 slot->n_elem = n_elem;
1014 slot->dlvry_queue = dlvry_queue;
1015 slot->dlvry_queue_slot = dlvry_queue_slot;
1016 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1017 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1020 task->lldd_task = slot;
1022 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1024 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1025 abort_flag, task_tag);
1029 /* Port structure is static for the HBA, so
1030 * even if the port is deformed it is ok
1033 list_add_tail(&slot->entry, &port->list);
1034 spin_lock(&task->task_state_lock);
1035 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1036 spin_unlock(&task->task_state_lock);
1038 hisi_hba->slot_prep = slot;
1040 sas_dev->running_req++;
1041 /* send abort command to our chip */
1042 hisi_hba->hw->start_delivery(hisi_hba);
1047 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1049 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1055 * hisi_sas_internal_task_abort -- execute an internal
1056 * abort command for single IO command or a device
1057 * @hisi_hba: host controller struct
1058 * @device: domain device
1059 * @abort_flag: mode of operation, device or single IO
1060 * @tag: tag of IO to be aborted (only relevant to single
1064 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1065 struct domain_device *device,
1066 int abort_flag, int tag)
1068 struct sas_task *task;
1069 struct hisi_sas_device *sas_dev = device->lldd_dev;
1070 struct device *dev = &hisi_hba->pdev->dev;
1072 unsigned long flags;
1074 if (!hisi_hba->hw->prep_abort)
1077 task = sas_alloc_slow_task(GFP_KERNEL);
1082 task->task_proto = device->tproto;
1083 task->task_done = hisi_sas_task_done;
1084 task->slow_task->timer.data = (unsigned long)task;
1085 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1086 task->slow_task->timer.expires = jiffies + 20*HZ;
1087 add_timer(&task->slow_task->timer);
1089 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1090 spin_lock_irqsave(&hisi_hba->lock, flags);
1091 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1092 task, abort_flag, tag);
1093 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1095 del_timer(&task->slow_task->timer);
1096 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1100 wait_for_completion(&task->slow_task->completion);
1101 res = TMF_RESP_FUNC_FAILED;
1103 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1104 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1105 res = TMF_RESP_FUNC_COMPLETE;
1109 /* TMF timed out, return direct. */
1110 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1111 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1112 dev_err(dev, "internal task abort: timeout.\n");
1113 if (task->lldd_task) {
1114 struct hisi_sas_slot *slot = task->lldd_task;
1116 hisi_sas_slot_task_free(hisi_hba, task, slot);
1122 dev_info(dev, "internal task abort: task to dev %016llx task=%p "
1123 "resp: 0x%x sts 0x%x\n",
1124 SAS_ADDR(device->sas_addr),
1126 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1127 task->task_status.stat);
1128 sas_free_task(task);
1133 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1135 hisi_sas_port_notify_formed(sas_phy);
1138 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1140 hisi_sas_port_notify_deformed(sas_phy);
1143 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1145 phy->phy_attached = 0;
1150 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1152 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1153 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1154 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1157 /* Phy down but ready */
1158 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1159 hisi_sas_port_notify_formed(sas_phy);
1161 struct hisi_sas_port *port = phy->port;
1163 /* Phy down and not ready */
1164 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1165 sas_phy_disconnected(sas_phy);
1168 if (phy->phy_type & PORT_TYPE_SAS) {
1169 int port_id = port->id;
1171 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1173 port->port_attached = 0;
1174 } else if (phy->phy_type & PORT_TYPE_SATA)
1175 port->port_attached = 0;
1177 hisi_sas_phy_disconnected(phy);
1180 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1182 static struct scsi_transport_template *hisi_sas_stt;
1184 static struct scsi_host_template hisi_sas_sht = {
1185 .module = THIS_MODULE,
1187 .queuecommand = sas_queuecommand,
1188 .target_alloc = sas_target_alloc,
1189 .slave_configure = hisi_sas_slave_configure,
1190 .scan_finished = hisi_sas_scan_finished,
1191 .scan_start = hisi_sas_scan_start,
1192 .change_queue_depth = sas_change_queue_depth,
1193 .bios_param = sas_bios_param,
1196 .sg_tablesize = SG_ALL,
1197 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1198 .use_clustering = ENABLE_CLUSTERING,
1199 .eh_device_reset_handler = sas_eh_device_reset_handler,
1200 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1201 .target_destroy = sas_target_destroy,
1205 static struct sas_domain_function_template hisi_sas_transport_ops = {
1206 .lldd_dev_found = hisi_sas_dev_found,
1207 .lldd_dev_gone = hisi_sas_dev_gone,
1208 .lldd_execute_task = hisi_sas_queue_command,
1209 .lldd_control_phy = hisi_sas_control_phy,
1210 .lldd_abort_task = hisi_sas_abort_task,
1211 .lldd_abort_task_set = hisi_sas_abort_task_set,
1212 .lldd_clear_aca = hisi_sas_clear_aca,
1213 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1214 .lldd_lu_reset = hisi_sas_lu_reset,
1215 .lldd_query_task = hisi_sas_query_task,
1216 .lldd_port_formed = hisi_sas_port_formed,
1217 .lldd_port_deformed = hisi_sas_port_deformed,
1220 static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1222 struct platform_device *pdev = hisi_hba->pdev;
1223 struct device *dev = &pdev->dev;
1224 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1226 spin_lock_init(&hisi_hba->lock);
1227 for (i = 0; i < hisi_hba->n_phy; i++) {
1228 hisi_sas_phy_init(hisi_hba, i);
1229 hisi_hba->port[i].port_attached = 0;
1230 hisi_hba->port[i].id = -1;
1231 INIT_LIST_HEAD(&hisi_hba->port[i].list);
1234 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1235 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1236 hisi_hba->devices[i].device_id = i;
1237 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1240 for (i = 0; i < hisi_hba->queue_count; i++) {
1241 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1242 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1244 /* Completion queue structure */
1246 cq->hisi_hba = hisi_hba;
1248 /* Delivery queue structure */
1250 dq->hisi_hba = hisi_hba;
1252 /* Delivery queue */
1253 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1254 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1255 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1256 if (!hisi_hba->cmd_hdr[i])
1258 memset(hisi_hba->cmd_hdr[i], 0, s);
1260 /* Completion queue */
1261 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1262 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1263 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1264 if (!hisi_hba->complete_hdr[i])
1266 memset(hisi_hba->complete_hdr[i], 0, s);
1269 s = HISI_SAS_STATUS_BUF_SZ;
1270 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1272 if (!hisi_hba->status_buffer_pool)
1275 s = HISI_SAS_COMMAND_TABLE_SZ;
1276 hisi_hba->command_table_pool = dma_pool_create("command_table",
1278 if (!hisi_hba->command_table_pool)
1281 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1282 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1284 if (!hisi_hba->itct)
1287 memset(hisi_hba->itct, 0, s);
1289 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1290 sizeof(struct hisi_sas_slot),
1292 if (!hisi_hba->slot_info)
1295 s = max_command_entries * sizeof(struct hisi_sas_iost);
1296 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1298 if (!hisi_hba->iost)
1301 memset(hisi_hba->iost, 0, s);
1303 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1304 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1305 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1306 if (!hisi_hba->breakpoint)
1309 memset(hisi_hba->breakpoint, 0, s);
1311 hisi_hba->slot_index_count = max_command_entries;
1312 s = hisi_hba->slot_index_count / sizeof(unsigned long);
1313 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1314 if (!hisi_hba->slot_index_tags)
1317 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1318 sizeof(struct hisi_sas_sge_page), 16, 0);
1319 if (!hisi_hba->sge_page_pool)
1322 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1323 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1324 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1325 if (!hisi_hba->initial_fis)
1327 memset(hisi_hba->initial_fis, 0, s);
1329 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1330 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1331 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1332 if (!hisi_hba->sata_breakpoint)
1334 memset(hisi_hba->sata_breakpoint, 0, s);
1336 hisi_sas_slot_index_init(hisi_hba);
1338 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1339 if (!hisi_hba->wq) {
1340 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1349 static void hisi_sas_free(struct hisi_hba *hisi_hba)
1351 struct device *dev = &hisi_hba->pdev->dev;
1352 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1354 for (i = 0; i < hisi_hba->queue_count; i++) {
1355 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1356 if (hisi_hba->cmd_hdr[i])
1357 dma_free_coherent(dev, s,
1358 hisi_hba->cmd_hdr[i],
1359 hisi_hba->cmd_hdr_dma[i]);
1361 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1362 if (hisi_hba->complete_hdr[i])
1363 dma_free_coherent(dev, s,
1364 hisi_hba->complete_hdr[i],
1365 hisi_hba->complete_hdr_dma[i]);
1368 dma_pool_destroy(hisi_hba->status_buffer_pool);
1369 dma_pool_destroy(hisi_hba->command_table_pool);
1370 dma_pool_destroy(hisi_hba->sge_page_pool);
1372 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1374 dma_free_coherent(dev, s,
1375 hisi_hba->itct, hisi_hba->itct_dma);
1377 s = max_command_entries * sizeof(struct hisi_sas_iost);
1379 dma_free_coherent(dev, s,
1380 hisi_hba->iost, hisi_hba->iost_dma);
1382 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1383 if (hisi_hba->breakpoint)
1384 dma_free_coherent(dev, s,
1385 hisi_hba->breakpoint,
1386 hisi_hba->breakpoint_dma);
1389 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1390 if (hisi_hba->initial_fis)
1391 dma_free_coherent(dev, s,
1392 hisi_hba->initial_fis,
1393 hisi_hba->initial_fis_dma);
1395 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1396 if (hisi_hba->sata_breakpoint)
1397 dma_free_coherent(dev, s,
1398 hisi_hba->sata_breakpoint,
1399 hisi_hba->sata_breakpoint_dma);
1402 destroy_workqueue(hisi_hba->wq);
1405 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1406 const struct hisi_sas_hw *hw)
1408 struct resource *res;
1409 struct Scsi_Host *shost;
1410 struct hisi_hba *hisi_hba;
1411 struct device *dev = &pdev->dev;
1412 struct device_node *np = pdev->dev.of_node;
1414 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
1417 hisi_hba = shost_priv(shost);
1420 hisi_hba->pdev = pdev;
1421 hisi_hba->shost = shost;
1422 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1424 init_timer(&hisi_hba->timer);
1426 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1431 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1432 "hisilicon,sas-syscon");
1433 if (IS_ERR(hisi_hba->ctrl))
1436 if (device_property_read_u32(dev, "ctrl-reset-reg",
1437 &hisi_hba->ctrl_reset_reg))
1440 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1441 &hisi_hba->ctrl_reset_sts_reg))
1444 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1445 &hisi_hba->ctrl_clock_ena_reg))
1449 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
1452 if (device_property_read_u32(dev, "queue-count",
1453 &hisi_hba->queue_count))
1456 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1457 hisi_hba->regs = devm_ioremap_resource(dev, res);
1458 if (IS_ERR(hisi_hba->regs))
1461 if (hisi_sas_alloc(hisi_hba, shost)) {
1462 hisi_sas_free(hisi_hba);
1468 dev_err(dev, "shost alloc failed\n");
1472 static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1476 for (i = 0; i < hisi_hba->n_phy; i++)
1477 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1482 int hisi_sas_probe(struct platform_device *pdev,
1483 const struct hisi_sas_hw *hw)
1485 struct Scsi_Host *shost;
1486 struct hisi_hba *hisi_hba;
1487 struct device *dev = &pdev->dev;
1488 struct asd_sas_phy **arr_phy;
1489 struct asd_sas_port **arr_port;
1490 struct sas_ha_struct *sha;
1491 int rc, phy_nr, port_nr, i;
1493 shost = hisi_sas_shost_alloc(pdev, hw);
1499 sha = SHOST_TO_SAS_HA(shost);
1500 hisi_hba = shost_priv(shost);
1501 platform_set_drvdata(pdev, sha);
1503 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1504 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1505 dev_err(dev, "No usable DMA addressing method\n");
1510 phy_nr = port_nr = hisi_hba->n_phy;
1512 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1513 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
1514 if (!arr_phy || !arr_port)
1517 sha->sas_phy = arr_phy;
1518 sha->sas_port = arr_port;
1519 sha->core.shost = shost;
1520 sha->lldd_ha = hisi_hba;
1522 shost->transportt = hisi_sas_stt;
1523 shost->max_id = HISI_SAS_MAX_DEVICES;
1524 shost->max_lun = ~0;
1525 shost->max_channel = 1;
1526 shost->max_cmd_len = 16;
1527 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
1528 shost->can_queue = hisi_hba->hw->max_command_entries;
1529 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
1531 sha->sas_ha_name = DRV_NAME;
1532 sha->dev = &hisi_hba->pdev->dev;
1533 sha->lldd_module = THIS_MODULE;
1534 sha->sas_addr = &hisi_hba->sas_addr[0];
1535 sha->num_phys = hisi_hba->n_phy;
1536 sha->core.shost = hisi_hba->shost;
1538 for (i = 0; i < hisi_hba->n_phy; i++) {
1539 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1540 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1543 hisi_sas_init_add(hisi_hba);
1545 rc = hisi_hba->hw->hw_init(hisi_hba);
1549 rc = scsi_add_host(shost, &pdev->dev);
1553 rc = sas_register_ha(sha);
1555 goto err_out_register_ha;
1557 scsi_scan_host(shost);
1561 err_out_register_ha:
1562 scsi_remove_host(shost);
1567 EXPORT_SYMBOL_GPL(hisi_sas_probe);
1569 int hisi_sas_remove(struct platform_device *pdev)
1571 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1572 struct hisi_hba *hisi_hba = sha->lldd_ha;
1574 scsi_remove_host(sha->core.shost);
1575 sas_unregister_ha(sha);
1576 sas_remove_host(sha->core.shost);
1578 hisi_sas_free(hisi_hba);
1581 EXPORT_SYMBOL_GPL(hisi_sas_remove);
1583 static __init int hisi_sas_init(void)
1585 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1587 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1594 static __exit void hisi_sas_exit(void)
1596 sas_release_transport(hisi_sas_stt);
1599 module_init(hisi_sas_init);
1600 module_exit(hisi_sas_exit);
1602 MODULE_VERSION(DRV_VERSION);
1603 MODULE_LICENSE("GPL");
1604 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1605 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1606 MODULE_ALIAS("platform:" DRV_NAME);