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 | |
| 15 | static struct scsi_transport_template *hisi_sas_stt; |
| 16 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 17 | static struct scsi_host_template hisi_sas_sht = { |
| 18 | .module = THIS_MODULE, |
| 19 | .name = DRV_NAME, |
| 20 | .queuecommand = sas_queuecommand, |
| 21 | .target_alloc = sas_target_alloc, |
| 22 | .slave_configure = sas_slave_configure, |
| 23 | .change_queue_depth = sas_change_queue_depth, |
| 24 | .bios_param = sas_bios_param, |
| 25 | .can_queue = 1, |
| 26 | .this_id = -1, |
| 27 | .sg_tablesize = SG_ALL, |
| 28 | .max_sectors = SCSI_DEFAULT_MAX_SECTORS, |
| 29 | .use_clustering = ENABLE_CLUSTERING, |
| 30 | .eh_device_reset_handler = sas_eh_device_reset_handler, |
| 31 | .eh_bus_reset_handler = sas_eh_bus_reset_handler, |
| 32 | .target_destroy = sas_target_destroy, |
| 33 | .ioctl = sas_ioctl, |
| 34 | }; |
| 35 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 36 | static struct sas_domain_function_template hisi_sas_transport_ops = { |
| 37 | }; |
| 38 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 39 | static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, |
| 40 | const struct hisi_sas_hw *hw) |
| 41 | { |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame^] | 42 | struct resource *res; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 43 | struct Scsi_Host *shost; |
| 44 | struct hisi_hba *hisi_hba; |
| 45 | struct device *dev = &pdev->dev; |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame^] | 46 | struct device_node *np = pdev->dev.of_node; |
| 47 | struct property *sas_addr_prop; |
| 48 | int num; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 49 | |
| 50 | shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba)); |
| 51 | if (!shost) |
| 52 | goto err_out; |
| 53 | hisi_hba = shost_priv(shost); |
| 54 | |
| 55 | hisi_hba->hw = hw; |
| 56 | hisi_hba->pdev = pdev; |
| 57 | hisi_hba->shost = shost; |
| 58 | SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; |
| 59 | |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame^] | 60 | sas_addr_prop = of_find_property(np, "sas-addr", NULL); |
| 61 | if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE)) |
| 62 | goto err_out; |
| 63 | memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE); |
| 64 | |
| 65 | if (of_property_read_u32(np, "ctrl-reset-reg", |
| 66 | &hisi_hba->ctrl_reset_reg)) |
| 67 | goto err_out; |
| 68 | |
| 69 | if (of_property_read_u32(np, "ctrl-reset-sts-reg", |
| 70 | &hisi_hba->ctrl_reset_sts_reg)) |
| 71 | goto err_out; |
| 72 | |
| 73 | if (of_property_read_u32(np, "ctrl-clock-ena-reg", |
| 74 | &hisi_hba->ctrl_clock_ena_reg)) |
| 75 | goto err_out; |
| 76 | |
| 77 | if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy)) |
| 78 | goto err_out; |
| 79 | |
| 80 | if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count)) |
| 81 | goto err_out; |
| 82 | |
| 83 | num = of_irq_count(np); |
| 84 | hisi_hba->int_names = devm_kcalloc(dev, num, |
| 85 | HISI_SAS_NAME_LEN, |
| 86 | GFP_KERNEL); |
| 87 | if (!hisi_hba->int_names) |
| 88 | goto err_out; |
| 89 | |
| 90 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 91 | hisi_hba->regs = devm_ioremap_resource(dev, res); |
| 92 | if (IS_ERR(hisi_hba->regs)) |
| 93 | goto err_out; |
| 94 | |
| 95 | hisi_hba->ctrl = syscon_regmap_lookup_by_phandle( |
| 96 | np, "hisilicon,sas-syscon"); |
| 97 | if (IS_ERR(hisi_hba->ctrl)) |
| 98 | goto err_out; |
| 99 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 100 | return shost; |
| 101 | err_out: |
| 102 | dev_err(dev, "shost alloc failed\n"); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | int hisi_sas_probe(struct platform_device *pdev, |
| 107 | const struct hisi_sas_hw *hw) |
| 108 | { |
| 109 | struct Scsi_Host *shost; |
| 110 | struct hisi_hba *hisi_hba; |
| 111 | struct device *dev = &pdev->dev; |
| 112 | struct asd_sas_phy **arr_phy; |
| 113 | struct asd_sas_port **arr_port; |
| 114 | struct sas_ha_struct *sha; |
| 115 | int rc, phy_nr, port_nr, i; |
| 116 | |
| 117 | shost = hisi_sas_shost_alloc(pdev, hw); |
| 118 | if (!shost) { |
| 119 | rc = -ENOMEM; |
| 120 | goto err_out_ha; |
| 121 | } |
| 122 | |
| 123 | sha = SHOST_TO_SAS_HA(shost); |
| 124 | hisi_hba = shost_priv(shost); |
| 125 | platform_set_drvdata(pdev, sha); |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 126 | phy_nr = port_nr = hisi_hba->n_phy; |
| 127 | |
| 128 | arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); |
| 129 | arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); |
| 130 | if (!arr_phy || !arr_port) |
| 131 | return -ENOMEM; |
| 132 | |
| 133 | sha->sas_phy = arr_phy; |
| 134 | sha->sas_port = arr_port; |
| 135 | sha->core.shost = shost; |
| 136 | sha->lldd_ha = hisi_hba; |
| 137 | |
| 138 | shost->transportt = hisi_sas_stt; |
| 139 | shost->max_id = HISI_SAS_MAX_DEVICES; |
| 140 | shost->max_lun = ~0; |
| 141 | shost->max_channel = 1; |
| 142 | shost->max_cmd_len = 16; |
| 143 | shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT); |
| 144 | shost->can_queue = HISI_SAS_COMMAND_ENTRIES; |
| 145 | shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES; |
| 146 | |
| 147 | sha->sas_ha_name = DRV_NAME; |
| 148 | sha->dev = &hisi_hba->pdev->dev; |
| 149 | sha->lldd_module = THIS_MODULE; |
| 150 | sha->sas_addr = &hisi_hba->sas_addr[0]; |
| 151 | sha->num_phys = hisi_hba->n_phy; |
| 152 | sha->core.shost = hisi_hba->shost; |
| 153 | |
| 154 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 155 | sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; |
| 156 | sha->sas_port[i] = &hisi_hba->port[i].sas_port; |
| 157 | } |
| 158 | |
| 159 | rc = scsi_add_host(shost, &pdev->dev); |
| 160 | if (rc) |
| 161 | goto err_out_ha; |
| 162 | |
| 163 | rc = sas_register_ha(sha); |
| 164 | if (rc) |
| 165 | goto err_out_register_ha; |
| 166 | |
| 167 | scsi_scan_host(shost); |
| 168 | |
| 169 | return 0; |
| 170 | |
| 171 | err_out_register_ha: |
| 172 | scsi_remove_host(shost); |
| 173 | err_out_ha: |
| 174 | kfree(shost); |
| 175 | return rc; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(hisi_sas_probe); |
| 178 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 179 | static __init int hisi_sas_init(void) |
| 180 | { |
| 181 | pr_info("hisi_sas: driver version %s\n", DRV_VERSION); |
| 182 | |
| 183 | hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); |
| 184 | if (!hisi_sas_stt) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static __exit void hisi_sas_exit(void) |
| 191 | { |
| 192 | sas_release_transport(hisi_sas_stt); |
| 193 | } |
| 194 | |
| 195 | module_init(hisi_sas_init); |
| 196 | module_exit(hisi_sas_exit); |
| 197 | |
| 198 | MODULE_VERSION(DRV_VERSION); |
| 199 | MODULE_LICENSE("GPL"); |
| 200 | MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); |
| 201 | MODULE_DESCRIPTION("HISILICON SAS controller driver"); |
| 202 | MODULE_ALIAS("platform:" DRV_NAME); |