blob: 7bf6d2f5df0af69d8b22d7877a0aaba1f108671f [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 Garry66139922015-11-18 00:50:48 +080030static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
31{
32 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
33 struct asd_sas_phy *sas_phy = &phy->sas_phy;
34 struct sas_ha_struct *sas_ha;
35
36 if (!phy->phy_attached)
37 return;
38
39 sas_ha = &hisi_hba->sha;
40 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
41
42 if (sas_phy->phy) {
43 struct sas_phy *sphy = sas_phy->phy;
44
45 sphy->negotiated_linkrate = sas_phy->linkrate;
46 sphy->minimum_linkrate = phy->minimum_linkrate;
47 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
48 sphy->maximum_linkrate = phy->maximum_linkrate;
49 }
50
51 if (phy->phy_type & PORT_TYPE_SAS) {
52 struct sas_identify_frame *id;
53
54 id = (struct sas_identify_frame *)phy->frame_rcvd;
55 id->dev_type = phy->identify.device_type;
56 id->initiator_bits = SAS_PROTOCOL_ALL;
57 id->target_bits = phy->identify.target_port_protocols;
58 } else if (phy->phy_type & PORT_TYPE_SATA) {
59 /*Nothing*/
60 }
61
62 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
63 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
64}
65
66static void hisi_sas_phyup_work(struct work_struct *work)
67{
68 struct hisi_sas_phy *phy =
69 container_of(work, struct hisi_sas_phy, phyup_ws);
70 struct hisi_hba *hisi_hba = phy->hisi_hba;
71 struct asd_sas_phy *sas_phy = &phy->sas_phy;
72 int phy_no = sas_phy->id;
73
74 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
75 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
76}
John Garry976867e2015-11-18 00:50:42 +080077
78static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
79{
80 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
81 struct asd_sas_phy *sas_phy = &phy->sas_phy;
82
83 phy->hisi_hba = hisi_hba;
84 phy->port = NULL;
85 init_timer(&phy->timer);
86 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
87 sas_phy->class = SAS;
88 sas_phy->iproto = SAS_PROTOCOL_ALL;
89 sas_phy->tproto = 0;
90 sas_phy->type = PHY_TYPE_PHYSICAL;
91 sas_phy->role = PHY_ROLE_INITIATOR;
92 sas_phy->oob_mode = OOB_NOT_CONNECTED;
93 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
94 sas_phy->id = phy_no;
95 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
96 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
97 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
98 sas_phy->lldd_phy = phy;
John Garry66139922015-11-18 00:50:48 +080099
100 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
John Garry976867e2015-11-18 00:50:42 +0800101}
102
John Garrye8899fa2015-11-18 00:50:30 +0800103static struct scsi_transport_template *hisi_sas_stt;
104
John Garry7eb78692015-11-18 00:50:31 +0800105static struct scsi_host_template hisi_sas_sht = {
106 .module = THIS_MODULE,
107 .name = DRV_NAME,
108 .queuecommand = sas_queuecommand,
109 .target_alloc = sas_target_alloc,
110 .slave_configure = sas_slave_configure,
111 .change_queue_depth = sas_change_queue_depth,
112 .bios_param = sas_bios_param,
113 .can_queue = 1,
114 .this_id = -1,
115 .sg_tablesize = SG_ALL,
116 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
117 .use_clustering = ENABLE_CLUSTERING,
118 .eh_device_reset_handler = sas_eh_device_reset_handler,
119 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
120 .target_destroy = sas_target_destroy,
121 .ioctl = sas_ioctl,
122};
123
John Garrye8899fa2015-11-18 00:50:30 +0800124static struct sas_domain_function_template hisi_sas_transport_ops = {
125};
126
John Garry6be6de12015-11-18 00:50:34 +0800127static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
128{
129 int i, s;
130 struct platform_device *pdev = hisi_hba->pdev;
131 struct device *dev = &pdev->dev;
132
John Garryfa42d802015-11-18 00:50:43 +0800133 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +0800134 for (i = 0; i < hisi_hba->n_phy; i++) {
135 hisi_sas_phy_init(hisi_hba, i);
136 hisi_hba->port[i].port_attached = 0;
137 hisi_hba->port[i].id = -1;
138 INIT_LIST_HEAD(&hisi_hba->port[i].list);
139 }
140
John Garryaf740db2015-11-18 00:50:41 +0800141 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
142 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
143 hisi_hba->devices[i].device_id = i;
144 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
145 }
146
John Garry6be6de12015-11-18 00:50:34 +0800147 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +0800148 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
149
150 /* Completion queue structure */
151 cq->id = i;
152 cq->hisi_hba = hisi_hba;
153
John Garry6be6de12015-11-18 00:50:34 +0800154 /* Delivery queue */
155 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
156 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
157 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
158 if (!hisi_hba->cmd_hdr[i])
159 goto err_out;
160 memset(hisi_hba->cmd_hdr[i], 0, s);
161
162 /* Completion queue */
163 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
164 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
165 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
166 if (!hisi_hba->complete_hdr[i])
167 goto err_out;
168 memset(hisi_hba->complete_hdr[i], 0, s);
169 }
170
171 s = HISI_SAS_STATUS_BUF_SZ;
172 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
173 dev, s, 16, 0);
174 if (!hisi_hba->status_buffer_pool)
175 goto err_out;
176
177 s = HISI_SAS_COMMAND_TABLE_SZ;
178 hisi_hba->command_table_pool = dma_pool_create("command_table",
179 dev, s, 16, 0);
180 if (!hisi_hba->command_table_pool)
181 goto err_out;
182
183 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
184 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
185 GFP_KERNEL);
186 if (!hisi_hba->itct)
187 goto err_out;
188
189 memset(hisi_hba->itct, 0, s);
190
191 hisi_hba->slot_info = devm_kcalloc(dev, HISI_SAS_COMMAND_ENTRIES,
192 sizeof(struct hisi_sas_slot),
193 GFP_KERNEL);
194 if (!hisi_hba->slot_info)
195 goto err_out;
196
197 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
198 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
199 GFP_KERNEL);
200 if (!hisi_hba->iost)
201 goto err_out;
202
203 memset(hisi_hba->iost, 0, s);
204
205 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
206 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
207 &hisi_hba->breakpoint_dma, GFP_KERNEL);
208 if (!hisi_hba->breakpoint)
209 goto err_out;
210
211 memset(hisi_hba->breakpoint, 0, s);
212
John Garry257efd12015-11-18 00:50:36 +0800213 hisi_hba->slot_index_count = HISI_SAS_COMMAND_ENTRIES;
214 s = hisi_hba->slot_index_count / sizeof(unsigned long);
215 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
216 if (!hisi_hba->slot_index_tags)
217 goto err_out;
218
John Garry6be6de12015-11-18 00:50:34 +0800219 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
220 sizeof(struct hisi_sas_sge_page), 16, 0);
221 if (!hisi_hba->sge_page_pool)
222 goto err_out;
223
224 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
225 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
226 &hisi_hba->initial_fis_dma, GFP_KERNEL);
227 if (!hisi_hba->initial_fis)
228 goto err_out;
229 memset(hisi_hba->initial_fis, 0, s);
230
231 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
232 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
233 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
234 if (!hisi_hba->sata_breakpoint)
235 goto err_out;
236 memset(hisi_hba->sata_breakpoint, 0, s);
237
John Garry257efd12015-11-18 00:50:36 +0800238 hisi_sas_slot_index_init(hisi_hba);
239
John Garry7e9080e2015-11-18 00:50:40 +0800240 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
241 if (!hisi_hba->wq) {
242 dev_err(dev, "sas_alloc: failed to create workqueue\n");
243 goto err_out;
244 }
245
John Garry6be6de12015-11-18 00:50:34 +0800246 return 0;
247err_out:
248 return -ENOMEM;
249}
250
John Garry89d53322015-11-18 00:50:35 +0800251static void hisi_sas_free(struct hisi_hba *hisi_hba)
252{
253 struct device *dev = &hisi_hba->pdev->dev;
254 int i, s;
255
256 for (i = 0; i < hisi_hba->queue_count; i++) {
257 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
258 if (hisi_hba->cmd_hdr[i])
259 dma_free_coherent(dev, s,
260 hisi_hba->cmd_hdr[i],
261 hisi_hba->cmd_hdr_dma[i]);
262
263 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
264 if (hisi_hba->complete_hdr[i])
265 dma_free_coherent(dev, s,
266 hisi_hba->complete_hdr[i],
267 hisi_hba->complete_hdr_dma[i]);
268 }
269
270 dma_pool_destroy(hisi_hba->status_buffer_pool);
271 dma_pool_destroy(hisi_hba->command_table_pool);
272 dma_pool_destroy(hisi_hba->sge_page_pool);
273
274 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
275 if (hisi_hba->itct)
276 dma_free_coherent(dev, s,
277 hisi_hba->itct, hisi_hba->itct_dma);
278
279 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost);
280 if (hisi_hba->iost)
281 dma_free_coherent(dev, s,
282 hisi_hba->iost, hisi_hba->iost_dma);
283
284 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint);
285 if (hisi_hba->breakpoint)
286 dma_free_coherent(dev, s,
287 hisi_hba->breakpoint,
288 hisi_hba->breakpoint_dma);
289
290
291 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
292 if (hisi_hba->initial_fis)
293 dma_free_coherent(dev, s,
294 hisi_hba->initial_fis,
295 hisi_hba->initial_fis_dma);
296
297 s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2;
298 if (hisi_hba->sata_breakpoint)
299 dma_free_coherent(dev, s,
300 hisi_hba->sata_breakpoint,
301 hisi_hba->sata_breakpoint_dma);
302
John Garry7e9080e2015-11-18 00:50:40 +0800303 if (hisi_hba->wq)
304 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +0800305}
John Garry6be6de12015-11-18 00:50:34 +0800306
John Garry7eb78692015-11-18 00:50:31 +0800307static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
308 const struct hisi_sas_hw *hw)
309{
John Garrye26b2f42015-11-18 00:50:32 +0800310 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +0800311 struct Scsi_Host *shost;
312 struct hisi_hba *hisi_hba;
313 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +0800314 struct device_node *np = pdev->dev.of_node;
315 struct property *sas_addr_prop;
316 int num;
John Garry7eb78692015-11-18 00:50:31 +0800317
318 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
319 if (!shost)
320 goto err_out;
321 hisi_hba = shost_priv(shost);
322
323 hisi_hba->hw = hw;
324 hisi_hba->pdev = pdev;
325 hisi_hba->shost = shost;
326 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
327
John Garryfa42d802015-11-18 00:50:43 +0800328 init_timer(&hisi_hba->timer);
329
John Garrye26b2f42015-11-18 00:50:32 +0800330 sas_addr_prop = of_find_property(np, "sas-addr", NULL);
331 if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE))
332 goto err_out;
333 memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE);
334
335 if (of_property_read_u32(np, "ctrl-reset-reg",
336 &hisi_hba->ctrl_reset_reg))
337 goto err_out;
338
339 if (of_property_read_u32(np, "ctrl-reset-sts-reg",
340 &hisi_hba->ctrl_reset_sts_reg))
341 goto err_out;
342
343 if (of_property_read_u32(np, "ctrl-clock-ena-reg",
344 &hisi_hba->ctrl_clock_ena_reg))
345 goto err_out;
346
347 if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy))
348 goto err_out;
349
350 if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count))
351 goto err_out;
352
353 num = of_irq_count(np);
354 hisi_hba->int_names = devm_kcalloc(dev, num,
355 HISI_SAS_NAME_LEN,
356 GFP_KERNEL);
357 if (!hisi_hba->int_names)
358 goto err_out;
359
360 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
361 hisi_hba->regs = devm_ioremap_resource(dev, res);
362 if (IS_ERR(hisi_hba->regs))
363 goto err_out;
364
365 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(
366 np, "hisilicon,sas-syscon");
367 if (IS_ERR(hisi_hba->ctrl))
368 goto err_out;
369
John Garry89d53322015-11-18 00:50:35 +0800370 if (hisi_sas_alloc(hisi_hba, shost)) {
371 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +0800372 goto err_out;
John Garry89d53322015-11-18 00:50:35 +0800373 }
John Garry6be6de12015-11-18 00:50:34 +0800374
John Garry7eb78692015-11-18 00:50:31 +0800375 return shost;
376err_out:
377 dev_err(dev, "shost alloc failed\n");
378 return NULL;
379}
380
John Garry5d742422015-11-18 00:50:38 +0800381static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
382{
383 int i;
384
385 for (i = 0; i < hisi_hba->n_phy; i++)
386 memcpy(&hisi_hba->phy[i].dev_sas_addr,
387 hisi_hba->sas_addr,
388 SAS_ADDR_SIZE);
389}
390
John Garry7eb78692015-11-18 00:50:31 +0800391int hisi_sas_probe(struct platform_device *pdev,
392 const struct hisi_sas_hw *hw)
393{
394 struct Scsi_Host *shost;
395 struct hisi_hba *hisi_hba;
396 struct device *dev = &pdev->dev;
397 struct asd_sas_phy **arr_phy;
398 struct asd_sas_port **arr_port;
399 struct sas_ha_struct *sha;
400 int rc, phy_nr, port_nr, i;
401
402 shost = hisi_sas_shost_alloc(pdev, hw);
403 if (!shost) {
404 rc = -ENOMEM;
405 goto err_out_ha;
406 }
407
408 sha = SHOST_TO_SAS_HA(shost);
409 hisi_hba = shost_priv(shost);
410 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +0800411
412 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
413 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
414 dev_err(dev, "No usable DMA addressing method\n");
415 rc = -EIO;
416 goto err_out_ha;
417 }
418
John Garry7eb78692015-11-18 00:50:31 +0800419 phy_nr = port_nr = hisi_hba->n_phy;
420
421 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
422 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
423 if (!arr_phy || !arr_port)
424 return -ENOMEM;
425
426 sha->sas_phy = arr_phy;
427 sha->sas_port = arr_port;
428 sha->core.shost = shost;
429 sha->lldd_ha = hisi_hba;
430
431 shost->transportt = hisi_sas_stt;
432 shost->max_id = HISI_SAS_MAX_DEVICES;
433 shost->max_lun = ~0;
434 shost->max_channel = 1;
435 shost->max_cmd_len = 16;
436 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
437 shost->can_queue = HISI_SAS_COMMAND_ENTRIES;
438 shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES;
439
440 sha->sas_ha_name = DRV_NAME;
441 sha->dev = &hisi_hba->pdev->dev;
442 sha->lldd_module = THIS_MODULE;
443 sha->sas_addr = &hisi_hba->sas_addr[0];
444 sha->num_phys = hisi_hba->n_phy;
445 sha->core.shost = hisi_hba->shost;
446
447 for (i = 0; i < hisi_hba->n_phy; i++) {
448 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
449 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
450 }
451
John Garry5d742422015-11-18 00:50:38 +0800452 hisi_sas_init_add(hisi_hba);
453
John Garry8ff1d572015-11-18 00:50:46 +0800454 rc = hisi_hba->hw->hw_init(hisi_hba);
455 if (rc)
456 goto err_out_ha;
457
John Garry7eb78692015-11-18 00:50:31 +0800458 rc = scsi_add_host(shost, &pdev->dev);
459 if (rc)
460 goto err_out_ha;
461
462 rc = sas_register_ha(sha);
463 if (rc)
464 goto err_out_register_ha;
465
466 scsi_scan_host(shost);
467
468 return 0;
469
470err_out_register_ha:
471 scsi_remove_host(shost);
472err_out_ha:
473 kfree(shost);
474 return rc;
475}
476EXPORT_SYMBOL_GPL(hisi_sas_probe);
477
John Garry89d53322015-11-18 00:50:35 +0800478int hisi_sas_remove(struct platform_device *pdev)
479{
480 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
481 struct hisi_hba *hisi_hba = sha->lldd_ha;
482
483 scsi_remove_host(sha->core.shost);
484 sas_unregister_ha(sha);
485 sas_remove_host(sha->core.shost);
486
487 hisi_sas_free(hisi_hba);
488 return 0;
489}
490EXPORT_SYMBOL_GPL(hisi_sas_remove);
491
John Garrye8899fa2015-11-18 00:50:30 +0800492static __init int hisi_sas_init(void)
493{
494 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
495
496 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
497 if (!hisi_sas_stt)
498 return -ENOMEM;
499
500 return 0;
501}
502
503static __exit void hisi_sas_exit(void)
504{
505 sas_release_transport(hisi_sas_stt);
506}
507
508module_init(hisi_sas_init);
509module_exit(hisi_sas_exit);
510
511MODULE_VERSION(DRV_VERSION);
512MODULE_LICENSE("GPL");
513MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
514MODULE_DESCRIPTION("HISILICON SAS controller driver");
515MODULE_ALIAS("platform:" DRV_NAME);