blob: 4fc5a6c6d0ce5517e24b00d63d7c30e181950b89 [file] [log] [blame]
John Garrye8899fa2015-11-18 00:50:30 +08001/*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
4 *
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.
9 *
10 */
11
12#include "hisi_sas.h"
13#define DRV_NAME "hisi_sas"
14
15static struct scsi_transport_template *hisi_sas_stt;
16
John Garry7eb78692015-11-18 00:50:31 +080017static struct scsi_host_template hisi_sas_sht = {
18 .module = THIS_MODULE,
19 .name = DRV_NAME,
20 .queuecommand = sas_queuecommand,
21 .target_alloc = sas_target_alloc,
22 .slave_configure = sas_slave_configure,
23 .change_queue_depth = sas_change_queue_depth,
24 .bios_param = sas_bios_param,
25 .can_queue = 1,
26 .this_id = -1,
27 .sg_tablesize = SG_ALL,
28 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
29 .use_clustering = ENABLE_CLUSTERING,
30 .eh_device_reset_handler = sas_eh_device_reset_handler,
31 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
32 .target_destroy = sas_target_destroy,
33 .ioctl = sas_ioctl,
34};
35
John Garrye8899fa2015-11-18 00:50:30 +080036static struct sas_domain_function_template hisi_sas_transport_ops = {
37};
38
John Garry7eb78692015-11-18 00:50:31 +080039static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
40 const struct hisi_sas_hw *hw)
41{
John Garrye26b2f42015-11-18 00:50:32 +080042 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +080043 struct Scsi_Host *shost;
44 struct hisi_hba *hisi_hba;
45 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +080046 struct device_node *np = pdev->dev.of_node;
47 struct property *sas_addr_prop;
48 int num;
John Garry7eb78692015-11-18 00:50:31 +080049
50 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
51 if (!shost)
52 goto err_out;
53 hisi_hba = shost_priv(shost);
54
55 hisi_hba->hw = hw;
56 hisi_hba->pdev = pdev;
57 hisi_hba->shost = shost;
58 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
59
John Garrye26b2f42015-11-18 00:50:32 +080060 sas_addr_prop = of_find_property(np, "sas-addr", NULL);
61 if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE))
62 goto err_out;
63 memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE);
64
65 if (of_property_read_u32(np, "ctrl-reset-reg",
66 &hisi_hba->ctrl_reset_reg))
67 goto err_out;
68
69 if (of_property_read_u32(np, "ctrl-reset-sts-reg",
70 &hisi_hba->ctrl_reset_sts_reg))
71 goto err_out;
72
73 if (of_property_read_u32(np, "ctrl-clock-ena-reg",
74 &hisi_hba->ctrl_clock_ena_reg))
75 goto err_out;
76
77 if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy))
78 goto err_out;
79
80 if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count))
81 goto err_out;
82
83 num = of_irq_count(np);
84 hisi_hba->int_names = devm_kcalloc(dev, num,
85 HISI_SAS_NAME_LEN,
86 GFP_KERNEL);
87 if (!hisi_hba->int_names)
88 goto err_out;
89
90 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
91 hisi_hba->regs = devm_ioremap_resource(dev, res);
92 if (IS_ERR(hisi_hba->regs))
93 goto err_out;
94
95 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(
96 np, "hisilicon,sas-syscon");
97 if (IS_ERR(hisi_hba->ctrl))
98 goto err_out;
99
John Garry7eb78692015-11-18 00:50:31 +0800100 return shost;
101err_out:
102 dev_err(dev, "shost alloc failed\n");
103 return NULL;
104}
105
106int hisi_sas_probe(struct platform_device *pdev,
107 const struct hisi_sas_hw *hw)
108{
109 struct Scsi_Host *shost;
110 struct hisi_hba *hisi_hba;
111 struct device *dev = &pdev->dev;
112 struct asd_sas_phy **arr_phy;
113 struct asd_sas_port **arr_port;
114 struct sas_ha_struct *sha;
115 int rc, phy_nr, port_nr, i;
116
117 shost = hisi_sas_shost_alloc(pdev, hw);
118 if (!shost) {
119 rc = -ENOMEM;
120 goto err_out_ha;
121 }
122
123 sha = SHOST_TO_SAS_HA(shost);
124 hisi_hba = shost_priv(shost);
125 platform_set_drvdata(pdev, sha);
John Garry7eb78692015-11-18 00:50:31 +0800126 phy_nr = port_nr = hisi_hba->n_phy;
127
128 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
129 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
130 if (!arr_phy || !arr_port)
131 return -ENOMEM;
132
133 sha->sas_phy = arr_phy;
134 sha->sas_port = arr_port;
135 sha->core.shost = shost;
136 sha->lldd_ha = hisi_hba;
137
138 shost->transportt = hisi_sas_stt;
139 shost->max_id = HISI_SAS_MAX_DEVICES;
140 shost->max_lun = ~0;
141 shost->max_channel = 1;
142 shost->max_cmd_len = 16;
143 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
144 shost->can_queue = HISI_SAS_COMMAND_ENTRIES;
145 shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES;
146
147 sha->sas_ha_name = DRV_NAME;
148 sha->dev = &hisi_hba->pdev->dev;
149 sha->lldd_module = THIS_MODULE;
150 sha->sas_addr = &hisi_hba->sas_addr[0];
151 sha->num_phys = hisi_hba->n_phy;
152 sha->core.shost = hisi_hba->shost;
153
154 for (i = 0; i < hisi_hba->n_phy; i++) {
155 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
156 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
157 }
158
159 rc = scsi_add_host(shost, &pdev->dev);
160 if (rc)
161 goto err_out_ha;
162
163 rc = sas_register_ha(sha);
164 if (rc)
165 goto err_out_register_ha;
166
167 scsi_scan_host(shost);
168
169 return 0;
170
171err_out_register_ha:
172 scsi_remove_host(shost);
173err_out_ha:
174 kfree(shost);
175 return rc;
176}
177EXPORT_SYMBOL_GPL(hisi_sas_probe);
178
John Garrye8899fa2015-11-18 00:50:30 +0800179static __init int hisi_sas_init(void)
180{
181 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
182
183 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
184 if (!hisi_sas_stt)
185 return -ENOMEM;
186
187 return 0;
188}
189
190static __exit void hisi_sas_exit(void)
191{
192 sas_release_transport(hisi_sas_stt);
193}
194
195module_init(hisi_sas_init);
196module_exit(hisi_sas_exit);
197
198MODULE_VERSION(DRV_VERSION);
199MODULE_LICENSE("GPL");
200MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
201MODULE_DESCRIPTION("HISILICON SAS controller driver");
202MODULE_ALIAS("platform:" DRV_NAME);