blob: 7f32c6b76d0553e8b3a6d6497eab8517f6e4b61e [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
John Garry257efd12015-11-18 00:50:36 +080015static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
16{
17 void *bitmap = hisi_hba->slot_index_tags;
18
19 clear_bit(slot_idx, bitmap);
20}
21
22static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
23{
24 int i;
25
26 for (i = 0; i < hisi_hba->slot_index_count; ++i)
27 hisi_sas_slot_index_clear(hisi_hba, i);
28}
29
John Garrye8899fa2015-11-18 00:50:30 +080030static struct scsi_transport_template *hisi_sas_stt;
31
John Garry7eb78692015-11-18 00:50:31 +080032static struct scsi_host_template hisi_sas_sht = {
33 .module = THIS_MODULE,
34 .name = DRV_NAME,
35 .queuecommand = sas_queuecommand,
36 .target_alloc = sas_target_alloc,
37 .slave_configure = sas_slave_configure,
38 .change_queue_depth = sas_change_queue_depth,
39 .bios_param = sas_bios_param,
40 .can_queue = 1,
41 .this_id = -1,
42 .sg_tablesize = SG_ALL,
43 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
44 .use_clustering = ENABLE_CLUSTERING,
45 .eh_device_reset_handler = sas_eh_device_reset_handler,
46 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
47 .target_destroy = sas_target_destroy,
48 .ioctl = sas_ioctl,
49};
50
John Garrye8899fa2015-11-18 00:50:30 +080051static struct sas_domain_function_template hisi_sas_transport_ops = {
52};
53
John Garry6be6de12015-11-18 00:50:34 +080054static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
55{
56 int i, s;
57 struct platform_device *pdev = hisi_hba->pdev;
58 struct device *dev = &pdev->dev;
59
60 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +080061 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
62
63 /* Completion queue structure */
64 cq->id = i;
65 cq->hisi_hba = hisi_hba;
66
John Garry6be6de12015-11-18 00:50:34 +080067 /* Delivery queue */
68 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
69 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
70 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
71 if (!hisi_hba->cmd_hdr[i])
72 goto err_out;
73 memset(hisi_hba->cmd_hdr[i], 0, s);
74
75 /* Completion queue */
76 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
77 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
78 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
79 if (!hisi_hba->complete_hdr[i])
80 goto err_out;
81 memset(hisi_hba->complete_hdr[i], 0, s);
82 }
83
84 s = HISI_SAS_STATUS_BUF_SZ;
85 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
86 dev, s, 16, 0);
87 if (!hisi_hba->status_buffer_pool)
88 goto err_out;
89
90 s = HISI_SAS_COMMAND_TABLE_SZ;
91 hisi_hba->command_table_pool = dma_pool_create("command_table",
92 dev, s, 16, 0);
93 if (!hisi_hba->command_table_pool)
94 goto err_out;
95
96 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
97 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
98 GFP_KERNEL);
99 if (!hisi_hba->itct)
100 goto err_out;
101
102 memset(hisi_hba->itct, 0, s);
103
104 hisi_hba->slot_info = devm_kcalloc(dev, HISI_SAS_COMMAND_ENTRIES,
105 sizeof(struct hisi_sas_slot),
106 GFP_KERNEL);
107 if (!hisi_hba->slot_info)
108 goto err_out;
109
110 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
111 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
112 GFP_KERNEL);
113 if (!hisi_hba->iost)
114 goto err_out;
115
116 memset(hisi_hba->iost, 0, s);
117
118 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
119 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
120 &hisi_hba->breakpoint_dma, GFP_KERNEL);
121 if (!hisi_hba->breakpoint)
122 goto err_out;
123
124 memset(hisi_hba->breakpoint, 0, s);
125
John Garry257efd12015-11-18 00:50:36 +0800126 hisi_hba->slot_index_count = HISI_SAS_COMMAND_ENTRIES;
127 s = hisi_hba->slot_index_count / sizeof(unsigned long);
128 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
129 if (!hisi_hba->slot_index_tags)
130 goto err_out;
131
John Garry6be6de12015-11-18 00:50:34 +0800132 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
133 sizeof(struct hisi_sas_sge_page), 16, 0);
134 if (!hisi_hba->sge_page_pool)
135 goto err_out;
136
137 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
138 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
139 &hisi_hba->initial_fis_dma, GFP_KERNEL);
140 if (!hisi_hba->initial_fis)
141 goto err_out;
142 memset(hisi_hba->initial_fis, 0, s);
143
144 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
145 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
146 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
147 if (!hisi_hba->sata_breakpoint)
148 goto err_out;
149 memset(hisi_hba->sata_breakpoint, 0, s);
150
John Garry257efd12015-11-18 00:50:36 +0800151 hisi_sas_slot_index_init(hisi_hba);
152
John Garry7e9080e2015-11-18 00:50:40 +0800153 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
154 if (!hisi_hba->wq) {
155 dev_err(dev, "sas_alloc: failed to create workqueue\n");
156 goto err_out;
157 }
158
John Garry6be6de12015-11-18 00:50:34 +0800159 return 0;
160err_out:
161 return -ENOMEM;
162}
163
John Garry89d53322015-11-18 00:50:35 +0800164static void hisi_sas_free(struct hisi_hba *hisi_hba)
165{
166 struct device *dev = &hisi_hba->pdev->dev;
167 int i, s;
168
169 for (i = 0; i < hisi_hba->queue_count; i++) {
170 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
171 if (hisi_hba->cmd_hdr[i])
172 dma_free_coherent(dev, s,
173 hisi_hba->cmd_hdr[i],
174 hisi_hba->cmd_hdr_dma[i]);
175
176 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
177 if (hisi_hba->complete_hdr[i])
178 dma_free_coherent(dev, s,
179 hisi_hba->complete_hdr[i],
180 hisi_hba->complete_hdr_dma[i]);
181 }
182
183 dma_pool_destroy(hisi_hba->status_buffer_pool);
184 dma_pool_destroy(hisi_hba->command_table_pool);
185 dma_pool_destroy(hisi_hba->sge_page_pool);
186
187 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
188 if (hisi_hba->itct)
189 dma_free_coherent(dev, s,
190 hisi_hba->itct, hisi_hba->itct_dma);
191
192 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
193 if (hisi_hba->iost)
194 dma_free_coherent(dev, s,
195 hisi_hba->iost, hisi_hba->iost_dma);
196
197 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
198 if (hisi_hba->breakpoint)
199 dma_free_coherent(dev, s,
200 hisi_hba->breakpoint,
201 hisi_hba->breakpoint_dma);
202
203
204 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
205 if (hisi_hba->initial_fis)
206 dma_free_coherent(dev, s,
207 hisi_hba->initial_fis,
208 hisi_hba->initial_fis_dma);
209
210 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
211 if (hisi_hba->sata_breakpoint)
212 dma_free_coherent(dev, s,
213 hisi_hba->sata_breakpoint,
214 hisi_hba->sata_breakpoint_dma);
215
John Garry7e9080e2015-11-18 00:50:40 +0800216 if (hisi_hba->wq)
217 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +0800218}
John Garry6be6de12015-11-18 00:50:34 +0800219
John Garry7eb78692015-11-18 00:50:31 +0800220static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
221 const struct hisi_sas_hw *hw)
222{
John Garrye26b2f42015-11-18 00:50:32 +0800223 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +0800224 struct Scsi_Host *shost;
225 struct hisi_hba *hisi_hba;
226 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +0800227 struct device_node *np = pdev->dev.of_node;
228 struct property *sas_addr_prop;
229 int num;
John Garry7eb78692015-11-18 00:50:31 +0800230
231 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
232 if (!shost)
233 goto err_out;
234 hisi_hba = shost_priv(shost);
235
236 hisi_hba->hw = hw;
237 hisi_hba->pdev = pdev;
238 hisi_hba->shost = shost;
239 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
240
John Garrye26b2f42015-11-18 00:50:32 +0800241 sas_addr_prop = of_find_property(np, "sas-addr", NULL);
242 if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE))
243 goto err_out;
244 memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE);
245
246 if (of_property_read_u32(np, "ctrl-reset-reg",
247 &hisi_hba->ctrl_reset_reg))
248 goto err_out;
249
250 if (of_property_read_u32(np, "ctrl-reset-sts-reg",
251 &hisi_hba->ctrl_reset_sts_reg))
252 goto err_out;
253
254 if (of_property_read_u32(np, "ctrl-clock-ena-reg",
255 &hisi_hba->ctrl_clock_ena_reg))
256 goto err_out;
257
258 if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy))
259 goto err_out;
260
261 if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count))
262 goto err_out;
263
264 num = of_irq_count(np);
265 hisi_hba->int_names = devm_kcalloc(dev, num,
266 HISI_SAS_NAME_LEN,
267 GFP_KERNEL);
268 if (!hisi_hba->int_names)
269 goto err_out;
270
271 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
272 hisi_hba->regs = devm_ioremap_resource(dev, res);
273 if (IS_ERR(hisi_hba->regs))
274 goto err_out;
275
276 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(
277 np, "hisilicon,sas-syscon");
278 if (IS_ERR(hisi_hba->ctrl))
279 goto err_out;
280
John Garry89d53322015-11-18 00:50:35 +0800281 if (hisi_sas_alloc(hisi_hba, shost)) {
282 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +0800283 goto err_out;
John Garry89d53322015-11-18 00:50:35 +0800284 }
John Garry6be6de12015-11-18 00:50:34 +0800285
John Garry7eb78692015-11-18 00:50:31 +0800286 return shost;
287err_out:
288 dev_err(dev, "shost alloc failed\n");
289 return NULL;
290}
291
John Garry5d742422015-11-18 00:50:38 +0800292static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
293{
294 int i;
295
296 for (i = 0; i < hisi_hba->n_phy; i++)
297 memcpy(&hisi_hba->phy[i].dev_sas_addr,
298 hisi_hba->sas_addr,
299 SAS_ADDR_SIZE);
300}
301
John Garry7eb78692015-11-18 00:50:31 +0800302int hisi_sas_probe(struct platform_device *pdev,
303 const struct hisi_sas_hw *hw)
304{
305 struct Scsi_Host *shost;
306 struct hisi_hba *hisi_hba;
307 struct device *dev = &pdev->dev;
308 struct asd_sas_phy **arr_phy;
309 struct asd_sas_port **arr_port;
310 struct sas_ha_struct *sha;
311 int rc, phy_nr, port_nr, i;
312
313 shost = hisi_sas_shost_alloc(pdev, hw);
314 if (!shost) {
315 rc = -ENOMEM;
316 goto err_out_ha;
317 }
318
319 sha = SHOST_TO_SAS_HA(shost);
320 hisi_hba = shost_priv(shost);
321 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +0800322
323 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
324 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
325 dev_err(dev, "No usable DMA addressing method\n");
326 rc = -EIO;
327 goto err_out_ha;
328 }
329
John Garry7eb78692015-11-18 00:50:31 +0800330 phy_nr = port_nr = hisi_hba->n_phy;
331
332 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
333 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
334 if (!arr_phy || !arr_port)
335 return -ENOMEM;
336
337 sha->sas_phy = arr_phy;
338 sha->sas_port = arr_port;
339 sha->core.shost = shost;
340 sha->lldd_ha = hisi_hba;
341
342 shost->transportt = hisi_sas_stt;
343 shost->max_id = HISI_SAS_MAX_DEVICES;
344 shost->max_lun = ~0;
345 shost->max_channel = 1;
346 shost->max_cmd_len = 16;
347 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
348 shost->can_queue = HISI_SAS_COMMAND_ENTRIES;
349 shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES;
350
351 sha->sas_ha_name = DRV_NAME;
352 sha->dev = &hisi_hba->pdev->dev;
353 sha->lldd_module = THIS_MODULE;
354 sha->sas_addr = &hisi_hba->sas_addr[0];
355 sha->num_phys = hisi_hba->n_phy;
356 sha->core.shost = hisi_hba->shost;
357
358 for (i = 0; i < hisi_hba->n_phy; i++) {
359 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
360 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
361 }
362
John Garry5d742422015-11-18 00:50:38 +0800363 hisi_sas_init_add(hisi_hba);
364
John Garry7eb78692015-11-18 00:50:31 +0800365 rc = scsi_add_host(shost, &pdev->dev);
366 if (rc)
367 goto err_out_ha;
368
369 rc = sas_register_ha(sha);
370 if (rc)
371 goto err_out_register_ha;
372
373 scsi_scan_host(shost);
374
375 return 0;
376
377err_out_register_ha:
378 scsi_remove_host(shost);
379err_out_ha:
380 kfree(shost);
381 return rc;
382}
383EXPORT_SYMBOL_GPL(hisi_sas_probe);
384
John Garry89d53322015-11-18 00:50:35 +0800385int hisi_sas_remove(struct platform_device *pdev)
386{
387 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
388 struct hisi_hba *hisi_hba = sha->lldd_ha;
389
390 scsi_remove_host(sha->core.shost);
391 sas_unregister_ha(sha);
392 sas_remove_host(sha->core.shost);
393
394 hisi_sas_free(hisi_hba);
395 return 0;
396}
397EXPORT_SYMBOL_GPL(hisi_sas_remove);
398
John Garrye8899fa2015-11-18 00:50:30 +0800399static __init int hisi_sas_init(void)
400{
401 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
402
403 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
404 if (!hisi_sas_stt)
405 return -ENOMEM;
406
407 return 0;
408}
409
410static __exit void hisi_sas_exit(void)
411{
412 sas_release_transport(hisi_sas_stt);
413}
414
415module_init(hisi_sas_init);
416module_exit(hisi_sas_exit);
417
418MODULE_VERSION(DRV_VERSION);
419MODULE_LICENSE("GPL");
420MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
421MODULE_DESCRIPTION("HISILICON SAS controller driver");
422MODULE_ALIAS("platform:" DRV_NAME);