blob: 06b863c401247c2af44f12665140b5e3e9126afa [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 Garry976867e2015-11-18 00:50:42 +080030
31static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
32{
33 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
34 struct asd_sas_phy *sas_phy = &phy->sas_phy;
35
36 phy->hisi_hba = hisi_hba;
37 phy->port = NULL;
38 init_timer(&phy->timer);
39 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
40 sas_phy->class = SAS;
41 sas_phy->iproto = SAS_PROTOCOL_ALL;
42 sas_phy->tproto = 0;
43 sas_phy->type = PHY_TYPE_PHYSICAL;
44 sas_phy->role = PHY_ROLE_INITIATOR;
45 sas_phy->oob_mode = OOB_NOT_CONNECTED;
46 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
47 sas_phy->id = phy_no;
48 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
49 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
50 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
51 sas_phy->lldd_phy = phy;
52}
53
John Garrye8899fa2015-11-18 00:50:30 +080054static struct scsi_transport_template *hisi_sas_stt;
55
John Garry7eb78692015-11-18 00:50:31 +080056static struct scsi_host_template hisi_sas_sht = {
57 .module = THIS_MODULE,
58 .name = DRV_NAME,
59 .queuecommand = sas_queuecommand,
60 .target_alloc = sas_target_alloc,
61 .slave_configure = sas_slave_configure,
62 .change_queue_depth = sas_change_queue_depth,
63 .bios_param = sas_bios_param,
64 .can_queue = 1,
65 .this_id = -1,
66 .sg_tablesize = SG_ALL,
67 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
68 .use_clustering = ENABLE_CLUSTERING,
69 .eh_device_reset_handler = sas_eh_device_reset_handler,
70 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
71 .target_destroy = sas_target_destroy,
72 .ioctl = sas_ioctl,
73};
74
John Garrye8899fa2015-11-18 00:50:30 +080075static struct sas_domain_function_template hisi_sas_transport_ops = {
76};
77
John Garry6be6de12015-11-18 00:50:34 +080078static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
79{
80 int i, s;
81 struct platform_device *pdev = hisi_hba->pdev;
82 struct device *dev = &pdev->dev;
83
John Garryfa42d802015-11-18 00:50:43 +080084 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +080085 for (i = 0; i < hisi_hba->n_phy; i++) {
86 hisi_sas_phy_init(hisi_hba, i);
87 hisi_hba->port[i].port_attached = 0;
88 hisi_hba->port[i].id = -1;
89 INIT_LIST_HEAD(&hisi_hba->port[i].list);
90 }
91
John Garryaf740db2015-11-18 00:50:41 +080092 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
93 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
94 hisi_hba->devices[i].device_id = i;
95 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
96 }
97
John Garry6be6de12015-11-18 00:50:34 +080098 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +080099 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
100
101 /* Completion queue structure */
102 cq->id = i;
103 cq->hisi_hba = hisi_hba;
104
John Garry6be6de12015-11-18 00:50:34 +0800105 /* Delivery queue */
106 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
107 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
108 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
109 if (!hisi_hba->cmd_hdr[i])
110 goto err_out;
111 memset(hisi_hba->cmd_hdr[i], 0, s);
112
113 /* Completion queue */
114 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
115 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
116 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
117 if (!hisi_hba->complete_hdr[i])
118 goto err_out;
119 memset(hisi_hba->complete_hdr[i], 0, s);
120 }
121
122 s = HISI_SAS_STATUS_BUF_SZ;
123 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
124 dev, s, 16, 0);
125 if (!hisi_hba->status_buffer_pool)
126 goto err_out;
127
128 s = HISI_SAS_COMMAND_TABLE_SZ;
129 hisi_hba->command_table_pool = dma_pool_create("command_table",
130 dev, s, 16, 0);
131 if (!hisi_hba->command_table_pool)
132 goto err_out;
133
134 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
135 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
136 GFP_KERNEL);
137 if (!hisi_hba->itct)
138 goto err_out;
139
140 memset(hisi_hba->itct, 0, s);
141
142 hisi_hba->slot_info = devm_kcalloc(dev, HISI_SAS_COMMAND_ENTRIES,
143 sizeof(struct hisi_sas_slot),
144 GFP_KERNEL);
145 if (!hisi_hba->slot_info)
146 goto err_out;
147
148 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
149 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
150 GFP_KERNEL);
151 if (!hisi_hba->iost)
152 goto err_out;
153
154 memset(hisi_hba->iost, 0, s);
155
156 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
157 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
158 &hisi_hba->breakpoint_dma, GFP_KERNEL);
159 if (!hisi_hba->breakpoint)
160 goto err_out;
161
162 memset(hisi_hba->breakpoint, 0, s);
163
John Garry257efd12015-11-18 00:50:36 +0800164 hisi_hba->slot_index_count = HISI_SAS_COMMAND_ENTRIES;
165 s = hisi_hba->slot_index_count / sizeof(unsigned long);
166 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
167 if (!hisi_hba->slot_index_tags)
168 goto err_out;
169
John Garry6be6de12015-11-18 00:50:34 +0800170 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
171 sizeof(struct hisi_sas_sge_page), 16, 0);
172 if (!hisi_hba->sge_page_pool)
173 goto err_out;
174
175 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
176 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
177 &hisi_hba->initial_fis_dma, GFP_KERNEL);
178 if (!hisi_hba->initial_fis)
179 goto err_out;
180 memset(hisi_hba->initial_fis, 0, s);
181
182 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
183 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
184 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
185 if (!hisi_hba->sata_breakpoint)
186 goto err_out;
187 memset(hisi_hba->sata_breakpoint, 0, s);
188
John Garry257efd12015-11-18 00:50:36 +0800189 hisi_sas_slot_index_init(hisi_hba);
190
John Garry7e9080e2015-11-18 00:50:40 +0800191 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
192 if (!hisi_hba->wq) {
193 dev_err(dev, "sas_alloc: failed to create workqueue\n");
194 goto err_out;
195 }
196
John Garry6be6de12015-11-18 00:50:34 +0800197 return 0;
198err_out:
199 return -ENOMEM;
200}
201
John Garry89d53322015-11-18 00:50:35 +0800202static void hisi_sas_free(struct hisi_hba *hisi_hba)
203{
204 struct device *dev = &hisi_hba->pdev->dev;
205 int i, s;
206
207 for (i = 0; i < hisi_hba->queue_count; i++) {
208 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
209 if (hisi_hba->cmd_hdr[i])
210 dma_free_coherent(dev, s,
211 hisi_hba->cmd_hdr[i],
212 hisi_hba->cmd_hdr_dma[i]);
213
214 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
215 if (hisi_hba->complete_hdr[i])
216 dma_free_coherent(dev, s,
217 hisi_hba->complete_hdr[i],
218 hisi_hba->complete_hdr_dma[i]);
219 }
220
221 dma_pool_destroy(hisi_hba->status_buffer_pool);
222 dma_pool_destroy(hisi_hba->command_table_pool);
223 dma_pool_destroy(hisi_hba->sge_page_pool);
224
225 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
226 if (hisi_hba->itct)
227 dma_free_coherent(dev, s,
228 hisi_hba->itct, hisi_hba->itct_dma);
229
230 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
231 if (hisi_hba->iost)
232 dma_free_coherent(dev, s,
233 hisi_hba->iost, hisi_hba->iost_dma);
234
235 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
236 if (hisi_hba->breakpoint)
237 dma_free_coherent(dev, s,
238 hisi_hba->breakpoint,
239 hisi_hba->breakpoint_dma);
240
241
242 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
243 if (hisi_hba->initial_fis)
244 dma_free_coherent(dev, s,
245 hisi_hba->initial_fis,
246 hisi_hba->initial_fis_dma);
247
248 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
249 if (hisi_hba->sata_breakpoint)
250 dma_free_coherent(dev, s,
251 hisi_hba->sata_breakpoint,
252 hisi_hba->sata_breakpoint_dma);
253
John Garry7e9080e2015-11-18 00:50:40 +0800254 if (hisi_hba->wq)
255 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +0800256}
John Garry6be6de12015-11-18 00:50:34 +0800257
John Garry7eb78692015-11-18 00:50:31 +0800258static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
259 const struct hisi_sas_hw *hw)
260{
John Garrye26b2f42015-11-18 00:50:32 +0800261 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +0800262 struct Scsi_Host *shost;
263 struct hisi_hba *hisi_hba;
264 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +0800265 struct device_node *np = pdev->dev.of_node;
266 struct property *sas_addr_prop;
267 int num;
John Garry7eb78692015-11-18 00:50:31 +0800268
269 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
270 if (!shost)
271 goto err_out;
272 hisi_hba = shost_priv(shost);
273
274 hisi_hba->hw = hw;
275 hisi_hba->pdev = pdev;
276 hisi_hba->shost = shost;
277 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
278
John Garryfa42d802015-11-18 00:50:43 +0800279 init_timer(&hisi_hba->timer);
280
John Garrye26b2f42015-11-18 00:50:32 +0800281 sas_addr_prop = of_find_property(np, "sas-addr", NULL);
282 if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE))
283 goto err_out;
284 memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE);
285
286 if (of_property_read_u32(np, "ctrl-reset-reg",
287 &hisi_hba->ctrl_reset_reg))
288 goto err_out;
289
290 if (of_property_read_u32(np, "ctrl-reset-sts-reg",
291 &hisi_hba->ctrl_reset_sts_reg))
292 goto err_out;
293
294 if (of_property_read_u32(np, "ctrl-clock-ena-reg",
295 &hisi_hba->ctrl_clock_ena_reg))
296 goto err_out;
297
298 if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy))
299 goto err_out;
300
301 if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count))
302 goto err_out;
303
304 num = of_irq_count(np);
305 hisi_hba->int_names = devm_kcalloc(dev, num,
306 HISI_SAS_NAME_LEN,
307 GFP_KERNEL);
308 if (!hisi_hba->int_names)
309 goto err_out;
310
311 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
312 hisi_hba->regs = devm_ioremap_resource(dev, res);
313 if (IS_ERR(hisi_hba->regs))
314 goto err_out;
315
316 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(
317 np, "hisilicon,sas-syscon");
318 if (IS_ERR(hisi_hba->ctrl))
319 goto err_out;
320
John Garry89d53322015-11-18 00:50:35 +0800321 if (hisi_sas_alloc(hisi_hba, shost)) {
322 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +0800323 goto err_out;
John Garry89d53322015-11-18 00:50:35 +0800324 }
John Garry6be6de12015-11-18 00:50:34 +0800325
John Garry7eb78692015-11-18 00:50:31 +0800326 return shost;
327err_out:
328 dev_err(dev, "shost alloc failed\n");
329 return NULL;
330}
331
John Garry5d742422015-11-18 00:50:38 +0800332static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
333{
334 int i;
335
336 for (i = 0; i < hisi_hba->n_phy; i++)
337 memcpy(&hisi_hba->phy[i].dev_sas_addr,
338 hisi_hba->sas_addr,
339 SAS_ADDR_SIZE);
340}
341
John Garry7eb78692015-11-18 00:50:31 +0800342int hisi_sas_probe(struct platform_device *pdev,
343 const struct hisi_sas_hw *hw)
344{
345 struct Scsi_Host *shost;
346 struct hisi_hba *hisi_hba;
347 struct device *dev = &pdev->dev;
348 struct asd_sas_phy **arr_phy;
349 struct asd_sas_port **arr_port;
350 struct sas_ha_struct *sha;
351 int rc, phy_nr, port_nr, i;
352
353 shost = hisi_sas_shost_alloc(pdev, hw);
354 if (!shost) {
355 rc = -ENOMEM;
356 goto err_out_ha;
357 }
358
359 sha = SHOST_TO_SAS_HA(shost);
360 hisi_hba = shost_priv(shost);
361 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +0800362
363 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
364 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
365 dev_err(dev, "No usable DMA addressing method\n");
366 rc = -EIO;
367 goto err_out_ha;
368 }
369
John Garry7eb78692015-11-18 00:50:31 +0800370 phy_nr = port_nr = hisi_hba->n_phy;
371
372 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
373 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
374 if (!arr_phy || !arr_port)
375 return -ENOMEM;
376
377 sha->sas_phy = arr_phy;
378 sha->sas_port = arr_port;
379 sha->core.shost = shost;
380 sha->lldd_ha = hisi_hba;
381
382 shost->transportt = hisi_sas_stt;
383 shost->max_id = HISI_SAS_MAX_DEVICES;
384 shost->max_lun = ~0;
385 shost->max_channel = 1;
386 shost->max_cmd_len = 16;
387 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
388 shost->can_queue = HISI_SAS_COMMAND_ENTRIES;
389 shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES;
390
391 sha->sas_ha_name = DRV_NAME;
392 sha->dev = &hisi_hba->pdev->dev;
393 sha->lldd_module = THIS_MODULE;
394 sha->sas_addr = &hisi_hba->sas_addr[0];
395 sha->num_phys = hisi_hba->n_phy;
396 sha->core.shost = hisi_hba->shost;
397
398 for (i = 0; i < hisi_hba->n_phy; i++) {
399 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
400 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
401 }
402
John Garry5d742422015-11-18 00:50:38 +0800403 hisi_sas_init_add(hisi_hba);
404
John Garry7eb78692015-11-18 00:50:31 +0800405 rc = scsi_add_host(shost, &pdev->dev);
406 if (rc)
407 goto err_out_ha;
408
409 rc = sas_register_ha(sha);
410 if (rc)
411 goto err_out_register_ha;
412
413 scsi_scan_host(shost);
414
415 return 0;
416
417err_out_register_ha:
418 scsi_remove_host(shost);
419err_out_ha:
420 kfree(shost);
421 return rc;
422}
423EXPORT_SYMBOL_GPL(hisi_sas_probe);
424
John Garry89d53322015-11-18 00:50:35 +0800425int hisi_sas_remove(struct platform_device *pdev)
426{
427 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
428 struct hisi_hba *hisi_hba = sha->lldd_ha;
429
430 scsi_remove_host(sha->core.shost);
431 sas_unregister_ha(sha);
432 sas_remove_host(sha->core.shost);
433
434 hisi_sas_free(hisi_hba);
435 return 0;
436}
437EXPORT_SYMBOL_GPL(hisi_sas_remove);
438
John Garrye8899fa2015-11-18 00:50:30 +0800439static __init int hisi_sas_init(void)
440{
441 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
442
443 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
444 if (!hisi_sas_stt)
445 return -ENOMEM;
446
447 return 0;
448}
449
450static __exit void hisi_sas_exit(void)
451{
452 sas_release_transport(hisi_sas_stt);
453}
454
455module_init(hisi_sas_init);
456module_exit(hisi_sas_exit);
457
458MODULE_VERSION(DRV_VERSION);
459MODULE_LICENSE("GPL");
460MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
461MODULE_DESCRIPTION("HISILICON SAS controller driver");
462MODULE_ALIAS("platform:" DRV_NAME);