John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1 | /* |
| 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 Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame^] | 15 | static 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 | |
| 22 | static 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 Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 30 | static struct scsi_transport_template *hisi_sas_stt; |
| 31 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 32 | static 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 Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 51 | static struct sas_domain_function_template hisi_sas_transport_ops = { |
| 52 | }; |
| 53 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 54 | static 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++) { |
| 61 | /* Delivery queue */ |
| 62 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 63 | hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s, |
| 64 | &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL); |
| 65 | if (!hisi_hba->cmd_hdr[i]) |
| 66 | goto err_out; |
| 67 | memset(hisi_hba->cmd_hdr[i], 0, s); |
| 68 | |
| 69 | /* Completion queue */ |
| 70 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 71 | hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s, |
| 72 | &hisi_hba->complete_hdr_dma[i], GFP_KERNEL); |
| 73 | if (!hisi_hba->complete_hdr[i]) |
| 74 | goto err_out; |
| 75 | memset(hisi_hba->complete_hdr[i], 0, s); |
| 76 | } |
| 77 | |
| 78 | s = HISI_SAS_STATUS_BUF_SZ; |
| 79 | hisi_hba->status_buffer_pool = dma_pool_create("status_buffer", |
| 80 | dev, s, 16, 0); |
| 81 | if (!hisi_hba->status_buffer_pool) |
| 82 | goto err_out; |
| 83 | |
| 84 | s = HISI_SAS_COMMAND_TABLE_SZ; |
| 85 | hisi_hba->command_table_pool = dma_pool_create("command_table", |
| 86 | dev, s, 16, 0); |
| 87 | if (!hisi_hba->command_table_pool) |
| 88 | goto err_out; |
| 89 | |
| 90 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 91 | hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma, |
| 92 | GFP_KERNEL); |
| 93 | if (!hisi_hba->itct) |
| 94 | goto err_out; |
| 95 | |
| 96 | memset(hisi_hba->itct, 0, s); |
| 97 | |
| 98 | hisi_hba->slot_info = devm_kcalloc(dev, HISI_SAS_COMMAND_ENTRIES, |
| 99 | sizeof(struct hisi_sas_slot), |
| 100 | GFP_KERNEL); |
| 101 | if (!hisi_hba->slot_info) |
| 102 | goto err_out; |
| 103 | |
| 104 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost); |
| 105 | hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma, |
| 106 | GFP_KERNEL); |
| 107 | if (!hisi_hba->iost) |
| 108 | goto err_out; |
| 109 | |
| 110 | memset(hisi_hba->iost, 0, s); |
| 111 | |
| 112 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint); |
| 113 | hisi_hba->breakpoint = dma_alloc_coherent(dev, s, |
| 114 | &hisi_hba->breakpoint_dma, GFP_KERNEL); |
| 115 | if (!hisi_hba->breakpoint) |
| 116 | goto err_out; |
| 117 | |
| 118 | memset(hisi_hba->breakpoint, 0, s); |
| 119 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame^] | 120 | hisi_hba->slot_index_count = HISI_SAS_COMMAND_ENTRIES; |
| 121 | s = hisi_hba->slot_index_count / sizeof(unsigned long); |
| 122 | hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL); |
| 123 | if (!hisi_hba->slot_index_tags) |
| 124 | goto err_out; |
| 125 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 126 | hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev, |
| 127 | sizeof(struct hisi_sas_sge_page), 16, 0); |
| 128 | if (!hisi_hba->sge_page_pool) |
| 129 | goto err_out; |
| 130 | |
| 131 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 132 | hisi_hba->initial_fis = dma_alloc_coherent(dev, s, |
| 133 | &hisi_hba->initial_fis_dma, GFP_KERNEL); |
| 134 | if (!hisi_hba->initial_fis) |
| 135 | goto err_out; |
| 136 | memset(hisi_hba->initial_fis, 0, s); |
| 137 | |
| 138 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2; |
| 139 | hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s, |
| 140 | &hisi_hba->sata_breakpoint_dma, GFP_KERNEL); |
| 141 | if (!hisi_hba->sata_breakpoint) |
| 142 | goto err_out; |
| 143 | memset(hisi_hba->sata_breakpoint, 0, s); |
| 144 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame^] | 145 | hisi_sas_slot_index_init(hisi_hba); |
| 146 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 147 | return 0; |
| 148 | err_out: |
| 149 | return -ENOMEM; |
| 150 | } |
| 151 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 152 | static void hisi_sas_free(struct hisi_hba *hisi_hba) |
| 153 | { |
| 154 | struct device *dev = &hisi_hba->pdev->dev; |
| 155 | int i, s; |
| 156 | |
| 157 | for (i = 0; i < hisi_hba->queue_count; i++) { |
| 158 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 159 | if (hisi_hba->cmd_hdr[i]) |
| 160 | dma_free_coherent(dev, s, |
| 161 | hisi_hba->cmd_hdr[i], |
| 162 | hisi_hba->cmd_hdr_dma[i]); |
| 163 | |
| 164 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 165 | if (hisi_hba->complete_hdr[i]) |
| 166 | dma_free_coherent(dev, s, |
| 167 | hisi_hba->complete_hdr[i], |
| 168 | hisi_hba->complete_hdr_dma[i]); |
| 169 | } |
| 170 | |
| 171 | dma_pool_destroy(hisi_hba->status_buffer_pool); |
| 172 | dma_pool_destroy(hisi_hba->command_table_pool); |
| 173 | dma_pool_destroy(hisi_hba->sge_page_pool); |
| 174 | |
| 175 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 176 | if (hisi_hba->itct) |
| 177 | dma_free_coherent(dev, s, |
| 178 | hisi_hba->itct, hisi_hba->itct_dma); |
| 179 | |
| 180 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost); |
| 181 | if (hisi_hba->iost) |
| 182 | dma_free_coherent(dev, s, |
| 183 | hisi_hba->iost, hisi_hba->iost_dma); |
| 184 | |
| 185 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint); |
| 186 | if (hisi_hba->breakpoint) |
| 187 | dma_free_coherent(dev, s, |
| 188 | hisi_hba->breakpoint, |
| 189 | hisi_hba->breakpoint_dma); |
| 190 | |
| 191 | |
| 192 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 193 | if (hisi_hba->initial_fis) |
| 194 | dma_free_coherent(dev, s, |
| 195 | hisi_hba->initial_fis, |
| 196 | hisi_hba->initial_fis_dma); |
| 197 | |
| 198 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2; |
| 199 | if (hisi_hba->sata_breakpoint) |
| 200 | dma_free_coherent(dev, s, |
| 201 | hisi_hba->sata_breakpoint, |
| 202 | hisi_hba->sata_breakpoint_dma); |
| 203 | |
| 204 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 205 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 206 | static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, |
| 207 | const struct hisi_sas_hw *hw) |
| 208 | { |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 209 | struct resource *res; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 210 | struct Scsi_Host *shost; |
| 211 | struct hisi_hba *hisi_hba; |
| 212 | struct device *dev = &pdev->dev; |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 213 | struct device_node *np = pdev->dev.of_node; |
| 214 | struct property *sas_addr_prop; |
| 215 | int num; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 216 | |
| 217 | shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba)); |
| 218 | if (!shost) |
| 219 | goto err_out; |
| 220 | hisi_hba = shost_priv(shost); |
| 221 | |
| 222 | hisi_hba->hw = hw; |
| 223 | hisi_hba->pdev = pdev; |
| 224 | hisi_hba->shost = shost; |
| 225 | SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; |
| 226 | |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 227 | sas_addr_prop = of_find_property(np, "sas-addr", NULL); |
| 228 | if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE)) |
| 229 | goto err_out; |
| 230 | memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE); |
| 231 | |
| 232 | if (of_property_read_u32(np, "ctrl-reset-reg", |
| 233 | &hisi_hba->ctrl_reset_reg)) |
| 234 | goto err_out; |
| 235 | |
| 236 | if (of_property_read_u32(np, "ctrl-reset-sts-reg", |
| 237 | &hisi_hba->ctrl_reset_sts_reg)) |
| 238 | goto err_out; |
| 239 | |
| 240 | if (of_property_read_u32(np, "ctrl-clock-ena-reg", |
| 241 | &hisi_hba->ctrl_clock_ena_reg)) |
| 242 | goto err_out; |
| 243 | |
| 244 | if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy)) |
| 245 | goto err_out; |
| 246 | |
| 247 | if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count)) |
| 248 | goto err_out; |
| 249 | |
| 250 | num = of_irq_count(np); |
| 251 | hisi_hba->int_names = devm_kcalloc(dev, num, |
| 252 | HISI_SAS_NAME_LEN, |
| 253 | GFP_KERNEL); |
| 254 | if (!hisi_hba->int_names) |
| 255 | goto err_out; |
| 256 | |
| 257 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 258 | hisi_hba->regs = devm_ioremap_resource(dev, res); |
| 259 | if (IS_ERR(hisi_hba->regs)) |
| 260 | goto err_out; |
| 261 | |
| 262 | hisi_hba->ctrl = syscon_regmap_lookup_by_phandle( |
| 263 | np, "hisilicon,sas-syscon"); |
| 264 | if (IS_ERR(hisi_hba->ctrl)) |
| 265 | goto err_out; |
| 266 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 267 | if (hisi_sas_alloc(hisi_hba, shost)) { |
| 268 | hisi_sas_free(hisi_hba); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 269 | goto err_out; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 270 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 271 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 272 | return shost; |
| 273 | err_out: |
| 274 | dev_err(dev, "shost alloc failed\n"); |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | int hisi_sas_probe(struct platform_device *pdev, |
| 279 | const struct hisi_sas_hw *hw) |
| 280 | { |
| 281 | struct Scsi_Host *shost; |
| 282 | struct hisi_hba *hisi_hba; |
| 283 | struct device *dev = &pdev->dev; |
| 284 | struct asd_sas_phy **arr_phy; |
| 285 | struct asd_sas_port **arr_port; |
| 286 | struct sas_ha_struct *sha; |
| 287 | int rc, phy_nr, port_nr, i; |
| 288 | |
| 289 | shost = hisi_sas_shost_alloc(pdev, hw); |
| 290 | if (!shost) { |
| 291 | rc = -ENOMEM; |
| 292 | goto err_out_ha; |
| 293 | } |
| 294 | |
| 295 | sha = SHOST_TO_SAS_HA(shost); |
| 296 | hisi_hba = shost_priv(shost); |
| 297 | platform_set_drvdata(pdev, sha); |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 298 | phy_nr = port_nr = hisi_hba->n_phy; |
| 299 | |
| 300 | arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); |
| 301 | arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); |
| 302 | if (!arr_phy || !arr_port) |
| 303 | return -ENOMEM; |
| 304 | |
| 305 | sha->sas_phy = arr_phy; |
| 306 | sha->sas_port = arr_port; |
| 307 | sha->core.shost = shost; |
| 308 | sha->lldd_ha = hisi_hba; |
| 309 | |
| 310 | shost->transportt = hisi_sas_stt; |
| 311 | shost->max_id = HISI_SAS_MAX_DEVICES; |
| 312 | shost->max_lun = ~0; |
| 313 | shost->max_channel = 1; |
| 314 | shost->max_cmd_len = 16; |
| 315 | shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT); |
| 316 | shost->can_queue = HISI_SAS_COMMAND_ENTRIES; |
| 317 | shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES; |
| 318 | |
| 319 | sha->sas_ha_name = DRV_NAME; |
| 320 | sha->dev = &hisi_hba->pdev->dev; |
| 321 | sha->lldd_module = THIS_MODULE; |
| 322 | sha->sas_addr = &hisi_hba->sas_addr[0]; |
| 323 | sha->num_phys = hisi_hba->n_phy; |
| 324 | sha->core.shost = hisi_hba->shost; |
| 325 | |
| 326 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 327 | sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; |
| 328 | sha->sas_port[i] = &hisi_hba->port[i].sas_port; |
| 329 | } |
| 330 | |
| 331 | rc = scsi_add_host(shost, &pdev->dev); |
| 332 | if (rc) |
| 333 | goto err_out_ha; |
| 334 | |
| 335 | rc = sas_register_ha(sha); |
| 336 | if (rc) |
| 337 | goto err_out_register_ha; |
| 338 | |
| 339 | scsi_scan_host(shost); |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | err_out_register_ha: |
| 344 | scsi_remove_host(shost); |
| 345 | err_out_ha: |
| 346 | kfree(shost); |
| 347 | return rc; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(hisi_sas_probe); |
| 350 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 351 | int hisi_sas_remove(struct platform_device *pdev) |
| 352 | { |
| 353 | struct sas_ha_struct *sha = platform_get_drvdata(pdev); |
| 354 | struct hisi_hba *hisi_hba = sha->lldd_ha; |
| 355 | |
| 356 | scsi_remove_host(sha->core.shost); |
| 357 | sas_unregister_ha(sha); |
| 358 | sas_remove_host(sha->core.shost); |
| 359 | |
| 360 | hisi_sas_free(hisi_hba); |
| 361 | return 0; |
| 362 | } |
| 363 | EXPORT_SYMBOL_GPL(hisi_sas_remove); |
| 364 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 365 | static __init int hisi_sas_init(void) |
| 366 | { |
| 367 | pr_info("hisi_sas: driver version %s\n", DRV_VERSION); |
| 368 | |
| 369 | hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); |
| 370 | if (!hisi_sas_stt) |
| 371 | return -ENOMEM; |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static __exit void hisi_sas_exit(void) |
| 377 | { |
| 378 | sas_release_transport(hisi_sas_stt); |
| 379 | } |
| 380 | |
| 381 | module_init(hisi_sas_init); |
| 382 | module_exit(hisi_sas_exit); |
| 383 | |
| 384 | MODULE_VERSION(DRV_VERSION); |
| 385 | MODULE_LICENSE("GPL"); |
| 386 | MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); |
| 387 | MODULE_DESCRIPTION("HISILICON SAS controller driver"); |
| 388 | MODULE_ALIAS("platform:" DRV_NAME); |