blob: 21111d4b854f241fcacab3d7e32011b86f7e3284 [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
John Garryaf740db2015-11-18 00:50:41 +080060 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
61 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
62 hisi_hba->devices[i].device_id = i;
63 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
64 }
65
John Garry6be6de12015-11-18 00:50:34 +080066 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +080067 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
68
69 /* Completion queue structure */
70 cq->id = i;
71 cq->hisi_hba = hisi_hba;
72
John Garry6be6de12015-11-18 00:50:34 +080073 /* Delivery queue */
74 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
75 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
76 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
77 if (!hisi_hba->cmd_hdr[i])
78 goto err_out;
79 memset(hisi_hba->cmd_hdr[i], 0, s);
80
81 /* Completion queue */
82 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
83 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
84 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
85 if (!hisi_hba->complete_hdr[i])
86 goto err_out;
87 memset(hisi_hba->complete_hdr[i], 0, s);
88 }
89
90 s = HISI_SAS_STATUS_BUF_SZ;
91 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
92 dev, s, 16, 0);
93 if (!hisi_hba->status_buffer_pool)
94 goto err_out;
95
96 s = HISI_SAS_COMMAND_TABLE_SZ;
97 hisi_hba->command_table_pool = dma_pool_create("command_table",
98 dev, s, 16, 0);
99 if (!hisi_hba->command_table_pool)
100 goto err_out;
101
102 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
103 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
104 GFP_KERNEL);
105 if (!hisi_hba->itct)
106 goto err_out;
107
108 memset(hisi_hba->itct, 0, s);
109
110 hisi_hba->slot_info = devm_kcalloc(dev, HISI_SAS_COMMAND_ENTRIES,
111 sizeof(struct hisi_sas_slot),
112 GFP_KERNEL);
113 if (!hisi_hba->slot_info)
114 goto err_out;
115
116 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
117 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
118 GFP_KERNEL);
119 if (!hisi_hba->iost)
120 goto err_out;
121
122 memset(hisi_hba->iost, 0, s);
123
124 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
125 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
126 &hisi_hba->breakpoint_dma, GFP_KERNEL);
127 if (!hisi_hba->breakpoint)
128 goto err_out;
129
130 memset(hisi_hba->breakpoint, 0, s);
131
John Garry257efd12015-11-18 00:50:36 +0800132 hisi_hba->slot_index_count = HISI_SAS_COMMAND_ENTRIES;
133 s = hisi_hba->slot_index_count / sizeof(unsigned long);
134 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
135 if (!hisi_hba->slot_index_tags)
136 goto err_out;
137
John Garry6be6de12015-11-18 00:50:34 +0800138 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
139 sizeof(struct hisi_sas_sge_page), 16, 0);
140 if (!hisi_hba->sge_page_pool)
141 goto err_out;
142
143 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
144 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
145 &hisi_hba->initial_fis_dma, GFP_KERNEL);
146 if (!hisi_hba->initial_fis)
147 goto err_out;
148 memset(hisi_hba->initial_fis, 0, s);
149
150 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
151 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
152 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
153 if (!hisi_hba->sata_breakpoint)
154 goto err_out;
155 memset(hisi_hba->sata_breakpoint, 0, s);
156
John Garry257efd12015-11-18 00:50:36 +0800157 hisi_sas_slot_index_init(hisi_hba);
158
John Garry7e9080e2015-11-18 00:50:40 +0800159 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
160 if (!hisi_hba->wq) {
161 dev_err(dev, "sas_alloc: failed to create workqueue\n");
162 goto err_out;
163 }
164
John Garry6be6de12015-11-18 00:50:34 +0800165 return 0;
166err_out:
167 return -ENOMEM;
168}
169
John Garry89d53322015-11-18 00:50:35 +0800170static void hisi_sas_free(struct hisi_hba *hisi_hba)
171{
172 struct device *dev = &hisi_hba->pdev->dev;
173 int i, s;
174
175 for (i = 0; i < hisi_hba->queue_count; i++) {
176 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
177 if (hisi_hba->cmd_hdr[i])
178 dma_free_coherent(dev, s,
179 hisi_hba->cmd_hdr[i],
180 hisi_hba->cmd_hdr_dma[i]);
181
182 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
183 if (hisi_hba->complete_hdr[i])
184 dma_free_coherent(dev, s,
185 hisi_hba->complete_hdr[i],
186 hisi_hba->complete_hdr_dma[i]);
187 }
188
189 dma_pool_destroy(hisi_hba->status_buffer_pool);
190 dma_pool_destroy(hisi_hba->command_table_pool);
191 dma_pool_destroy(hisi_hba->sge_page_pool);
192
193 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
194 if (hisi_hba->itct)
195 dma_free_coherent(dev, s,
196 hisi_hba->itct, hisi_hba->itct_dma);
197
198 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
199 if (hisi_hba->iost)
200 dma_free_coherent(dev, s,
201 hisi_hba->iost, hisi_hba->iost_dma);
202
203 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
204 if (hisi_hba->breakpoint)
205 dma_free_coherent(dev, s,
206 hisi_hba->breakpoint,
207 hisi_hba->breakpoint_dma);
208
209
210 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
211 if (hisi_hba->initial_fis)
212 dma_free_coherent(dev, s,
213 hisi_hba->initial_fis,
214 hisi_hba->initial_fis_dma);
215
216 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
217 if (hisi_hba->sata_breakpoint)
218 dma_free_coherent(dev, s,
219 hisi_hba->sata_breakpoint,
220 hisi_hba->sata_breakpoint_dma);
221
John Garry7e9080e2015-11-18 00:50:40 +0800222 if (hisi_hba->wq)
223 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +0800224}
John Garry6be6de12015-11-18 00:50:34 +0800225
John Garry7eb78692015-11-18 00:50:31 +0800226static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
227 const struct hisi_sas_hw *hw)
228{
John Garrye26b2f42015-11-18 00:50:32 +0800229 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +0800230 struct Scsi_Host *shost;
231 struct hisi_hba *hisi_hba;
232 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +0800233 struct device_node *np = pdev->dev.of_node;
234 struct property *sas_addr_prop;
235 int num;
John Garry7eb78692015-11-18 00:50:31 +0800236
237 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
238 if (!shost)
239 goto err_out;
240 hisi_hba = shost_priv(shost);
241
242 hisi_hba->hw = hw;
243 hisi_hba->pdev = pdev;
244 hisi_hba->shost = shost;
245 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
246
John Garrye26b2f42015-11-18 00:50:32 +0800247 sas_addr_prop = of_find_property(np, "sas-addr", NULL);
248 if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE))
249 goto err_out;
250 memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE);
251
252 if (of_property_read_u32(np, "ctrl-reset-reg",
253 &hisi_hba->ctrl_reset_reg))
254 goto err_out;
255
256 if (of_property_read_u32(np, "ctrl-reset-sts-reg",
257 &hisi_hba->ctrl_reset_sts_reg))
258 goto err_out;
259
260 if (of_property_read_u32(np, "ctrl-clock-ena-reg",
261 &hisi_hba->ctrl_clock_ena_reg))
262 goto err_out;
263
264 if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy))
265 goto err_out;
266
267 if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count))
268 goto err_out;
269
270 num = of_irq_count(np);
271 hisi_hba->int_names = devm_kcalloc(dev, num,
272 HISI_SAS_NAME_LEN,
273 GFP_KERNEL);
274 if (!hisi_hba->int_names)
275 goto err_out;
276
277 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
278 hisi_hba->regs = devm_ioremap_resource(dev, res);
279 if (IS_ERR(hisi_hba->regs))
280 goto err_out;
281
282 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(
283 np, "hisilicon,sas-syscon");
284 if (IS_ERR(hisi_hba->ctrl))
285 goto err_out;
286
John Garry89d53322015-11-18 00:50:35 +0800287 if (hisi_sas_alloc(hisi_hba, shost)) {
288 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +0800289 goto err_out;
John Garry89d53322015-11-18 00:50:35 +0800290 }
John Garry6be6de12015-11-18 00:50:34 +0800291
John Garry7eb78692015-11-18 00:50:31 +0800292 return shost;
293err_out:
294 dev_err(dev, "shost alloc failed\n");
295 return NULL;
296}
297
John Garry5d742422015-11-18 00:50:38 +0800298static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
299{
300 int i;
301
302 for (i = 0; i < hisi_hba->n_phy; i++)
303 memcpy(&hisi_hba->phy[i].dev_sas_addr,
304 hisi_hba->sas_addr,
305 SAS_ADDR_SIZE);
306}
307
John Garry7eb78692015-11-18 00:50:31 +0800308int hisi_sas_probe(struct platform_device *pdev,
309 const struct hisi_sas_hw *hw)
310{
311 struct Scsi_Host *shost;
312 struct hisi_hba *hisi_hba;
313 struct device *dev = &pdev->dev;
314 struct asd_sas_phy **arr_phy;
315 struct asd_sas_port **arr_port;
316 struct sas_ha_struct *sha;
317 int rc, phy_nr, port_nr, i;
318
319 shost = hisi_sas_shost_alloc(pdev, hw);
320 if (!shost) {
321 rc = -ENOMEM;
322 goto err_out_ha;
323 }
324
325 sha = SHOST_TO_SAS_HA(shost);
326 hisi_hba = shost_priv(shost);
327 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +0800328
329 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
330 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
331 dev_err(dev, "No usable DMA addressing method\n");
332 rc = -EIO;
333 goto err_out_ha;
334 }
335
John Garry7eb78692015-11-18 00:50:31 +0800336 phy_nr = port_nr = hisi_hba->n_phy;
337
338 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
339 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
340 if (!arr_phy || !arr_port)
341 return -ENOMEM;
342
343 sha->sas_phy = arr_phy;
344 sha->sas_port = arr_port;
345 sha->core.shost = shost;
346 sha->lldd_ha = hisi_hba;
347
348 shost->transportt = hisi_sas_stt;
349 shost->max_id = HISI_SAS_MAX_DEVICES;
350 shost->max_lun = ~0;
351 shost->max_channel = 1;
352 shost->max_cmd_len = 16;
353 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
354 shost->can_queue = HISI_SAS_COMMAND_ENTRIES;
355 shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES;
356
357 sha->sas_ha_name = DRV_NAME;
358 sha->dev = &hisi_hba->pdev->dev;
359 sha->lldd_module = THIS_MODULE;
360 sha->sas_addr = &hisi_hba->sas_addr[0];
361 sha->num_phys = hisi_hba->n_phy;
362 sha->core.shost = hisi_hba->shost;
363
364 for (i = 0; i < hisi_hba->n_phy; i++) {
365 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
366 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
367 }
368
John Garry5d742422015-11-18 00:50:38 +0800369 hisi_sas_init_add(hisi_hba);
370
John Garry7eb78692015-11-18 00:50:31 +0800371 rc = scsi_add_host(shost, &pdev->dev);
372 if (rc)
373 goto err_out_ha;
374
375 rc = sas_register_ha(sha);
376 if (rc)
377 goto err_out_register_ha;
378
379 scsi_scan_host(shost);
380
381 return 0;
382
383err_out_register_ha:
384 scsi_remove_host(shost);
385err_out_ha:
386 kfree(shost);
387 return rc;
388}
389EXPORT_SYMBOL_GPL(hisi_sas_probe);
390
John Garry89d53322015-11-18 00:50:35 +0800391int hisi_sas_remove(struct platform_device *pdev)
392{
393 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
394 struct hisi_hba *hisi_hba = sha->lldd_ha;
395
396 scsi_remove_host(sha->core.shost);
397 sas_unregister_ha(sha);
398 sas_remove_host(sha->core.shost);
399
400 hisi_sas_free(hisi_hba);
401 return 0;
402}
403EXPORT_SYMBOL_GPL(hisi_sas_remove);
404
John Garrye8899fa2015-11-18 00:50:30 +0800405static __init int hisi_sas_init(void)
406{
407 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
408
409 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
410 if (!hisi_sas_stt)
411 return -ENOMEM;
412
413 return 0;
414}
415
416static __exit void hisi_sas_exit(void)
417{
418 sas_release_transport(hisi_sas_stt);
419}
420
421module_init(hisi_sas_init);
422module_exit(hisi_sas_exit);
423
424MODULE_VERSION(DRV_VERSION);
425MODULE_LICENSE("GPL");
426MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
427MODULE_DESCRIPTION("HISILICON SAS controller driver");
428MODULE_ALIAS("platform:" DRV_NAME);