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 | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 15 | #define DEV_IS_EXPANDER(type) \ |
| 16 | ((type == SAS_EDGE_EXPANDER_DEVICE) || \ |
| 17 | (type == SAS_FANOUT_EXPANDER_DEVICE)) |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 18 | |
| 19 | #define DEV_IS_GONE(dev) \ |
| 20 | ((!dev) || (dev->dev_type == SAS_PHY_UNUSED)) |
| 21 | |
| 22 | static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device) |
| 23 | { |
| 24 | return device->port->ha->lldd_ha; |
| 25 | } |
| 26 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 27 | static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx) |
| 28 | { |
| 29 | void *bitmap = hisi_hba->slot_index_tags; |
| 30 | |
| 31 | clear_bit(slot_idx, bitmap); |
| 32 | } |
| 33 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 34 | static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx) |
| 35 | { |
| 36 | hisi_sas_slot_index_clear(hisi_hba, slot_idx); |
| 37 | } |
| 38 | |
| 39 | static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx) |
| 40 | { |
| 41 | void *bitmap = hisi_hba->slot_index_tags; |
| 42 | |
| 43 | set_bit(slot_idx, bitmap); |
| 44 | } |
| 45 | |
| 46 | static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx) |
| 47 | { |
| 48 | unsigned int index; |
| 49 | void *bitmap = hisi_hba->slot_index_tags; |
| 50 | |
| 51 | index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count); |
| 52 | if (index >= hisi_hba->slot_index_count) |
| 53 | return -SAS_QUEUE_FULL; |
| 54 | hisi_sas_slot_index_set(hisi_hba, index); |
| 55 | *slot_idx = index; |
| 56 | return 0; |
| 57 | } |
| 58 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 59 | static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba) |
| 60 | { |
| 61 | int i; |
| 62 | |
| 63 | for (i = 0; i < hisi_hba->slot_index_count; ++i) |
| 64 | hisi_sas_slot_index_clear(hisi_hba, i); |
| 65 | } |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 66 | |
| 67 | void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task, |
| 68 | struct hisi_sas_slot *slot) |
| 69 | { |
| 70 | struct device *dev = &hisi_hba->pdev->dev; |
| 71 | |
| 72 | if (!slot->task) |
| 73 | return; |
| 74 | |
| 75 | if (!sas_protocol_ata(task->task_proto)) |
| 76 | if (slot->n_elem) |
| 77 | dma_unmap_sg(dev, task->scatter, slot->n_elem, |
| 78 | task->data_dir); |
| 79 | |
| 80 | if (slot->command_table) |
| 81 | dma_pool_free(hisi_hba->command_table_pool, |
| 82 | slot->command_table, slot->command_table_dma); |
| 83 | |
| 84 | if (slot->status_buffer) |
| 85 | dma_pool_free(hisi_hba->status_buffer_pool, |
| 86 | slot->status_buffer, slot->status_buffer_dma); |
| 87 | |
| 88 | if (slot->sge_page) |
| 89 | dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page, |
| 90 | slot->sge_page_dma); |
| 91 | |
| 92 | list_del_init(&slot->entry); |
| 93 | task->lldd_task = NULL; |
| 94 | slot->task = NULL; |
| 95 | slot->port = NULL; |
| 96 | hisi_sas_slot_index_free(hisi_hba, slot->idx); |
| 97 | memset(slot, 0, sizeof(*slot)); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free); |
| 100 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 101 | static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba, |
| 102 | struct hisi_sas_slot *slot, int is_tmf, |
| 103 | struct hisi_sas_tmf_task *tmf) |
| 104 | { |
| 105 | return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf); |
| 106 | } |
| 107 | |
| 108 | static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba, |
| 109 | int is_tmf, struct hisi_sas_tmf_task *tmf, |
| 110 | int *pass) |
| 111 | { |
| 112 | struct domain_device *device = task->dev; |
| 113 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 114 | struct hisi_sas_port *port; |
| 115 | struct hisi_sas_slot *slot; |
| 116 | struct hisi_sas_cmd_hdr *cmd_hdr_base; |
| 117 | struct device *dev = &hisi_hba->pdev->dev; |
| 118 | int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; |
| 119 | |
| 120 | if (!device->port) { |
| 121 | struct task_status_struct *ts = &task->task_status; |
| 122 | |
| 123 | ts->resp = SAS_TASK_UNDELIVERED; |
| 124 | ts->stat = SAS_PHY_DOWN; |
| 125 | /* |
| 126 | * libsas will use dev->port, should |
| 127 | * not call task_done for sata |
| 128 | */ |
| 129 | if (device->dev_type != SAS_SATA_DEV) |
| 130 | task->task_done(task); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | if (DEV_IS_GONE(sas_dev)) { |
| 135 | if (sas_dev) |
| 136 | dev_info(dev, "task prep: device %llu not ready\n", |
| 137 | sas_dev->device_id); |
| 138 | else |
| 139 | dev_info(dev, "task prep: device %016llx not ready\n", |
| 140 | SAS_ADDR(device->sas_addr)); |
| 141 | |
| 142 | rc = SAS_PHY_DOWN; |
| 143 | return rc; |
| 144 | } |
| 145 | port = device->port->lldd_port; |
| 146 | if (port && !port->port_attached && !tmf) { |
| 147 | if (sas_protocol_ata(task->task_proto)) { |
| 148 | struct task_status_struct *ts = &task->task_status; |
| 149 | |
| 150 | dev_info(dev, |
| 151 | "task prep: SATA/STP port%d not attach device\n", |
| 152 | device->port->id); |
| 153 | ts->resp = SAS_TASK_COMPLETE; |
| 154 | ts->stat = SAS_PHY_DOWN; |
| 155 | task->task_done(task); |
| 156 | } else { |
| 157 | struct task_status_struct *ts = &task->task_status; |
| 158 | |
| 159 | dev_info(dev, |
| 160 | "task prep: SAS port%d does not attach device\n", |
| 161 | device->port->id); |
| 162 | ts->resp = SAS_TASK_UNDELIVERED; |
| 163 | ts->stat = SAS_PHY_DOWN; |
| 164 | task->task_done(task); |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | if (!sas_protocol_ata(task->task_proto)) { |
| 170 | if (task->num_scatter) { |
| 171 | n_elem = dma_map_sg(dev, task->scatter, |
| 172 | task->num_scatter, task->data_dir); |
| 173 | if (!n_elem) { |
| 174 | rc = -ENOMEM; |
| 175 | goto prep_out; |
| 176 | } |
| 177 | } |
| 178 | } else |
| 179 | n_elem = task->num_scatter; |
| 180 | |
| 181 | rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx); |
| 182 | if (rc) |
| 183 | goto err_out; |
| 184 | rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue, |
| 185 | &dlvry_queue_slot); |
| 186 | if (rc) |
| 187 | goto err_out_tag; |
| 188 | |
| 189 | slot = &hisi_hba->slot_info[slot_idx]; |
| 190 | memset(slot, 0, sizeof(struct hisi_sas_slot)); |
| 191 | |
| 192 | slot->idx = slot_idx; |
| 193 | slot->n_elem = n_elem; |
| 194 | slot->dlvry_queue = dlvry_queue; |
| 195 | slot->dlvry_queue_slot = dlvry_queue_slot; |
| 196 | cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; |
| 197 | slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; |
| 198 | slot->task = task; |
| 199 | slot->port = port; |
| 200 | task->lldd_task = slot; |
| 201 | |
| 202 | slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool, |
| 203 | GFP_ATOMIC, |
| 204 | &slot->status_buffer_dma); |
| 205 | if (!slot->status_buffer) |
| 206 | goto err_out_slot_buf; |
| 207 | memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ); |
| 208 | |
| 209 | slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool, |
| 210 | GFP_ATOMIC, |
| 211 | &slot->command_table_dma); |
| 212 | if (!slot->command_table) |
| 213 | goto err_out_status_buf; |
| 214 | memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ); |
| 215 | memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); |
| 216 | |
| 217 | switch (task->task_proto) { |
| 218 | case SAS_PROTOCOL_SSP: |
| 219 | rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf); |
| 220 | break; |
| 221 | case SAS_PROTOCOL_SATA: |
| 222 | case SAS_PROTOCOL_STP: |
| 223 | case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: |
| 224 | default: |
| 225 | dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n", |
| 226 | task->task_proto); |
| 227 | rc = -EINVAL; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | if (rc) { |
| 232 | dev_err(dev, "task prep: rc = 0x%x\n", rc); |
| 233 | if (slot->sge_page) |
| 234 | goto err_out_sge; |
| 235 | goto err_out_command_table; |
| 236 | } |
| 237 | |
| 238 | list_add_tail(&slot->entry, &port->list); |
| 239 | spin_lock(&task->task_state_lock); |
| 240 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; |
| 241 | spin_unlock(&task->task_state_lock); |
| 242 | |
| 243 | hisi_hba->slot_prep = slot; |
| 244 | |
| 245 | sas_dev->running_req++; |
| 246 | ++(*pass); |
| 247 | |
| 248 | return rc; |
| 249 | |
| 250 | err_out_sge: |
| 251 | dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page, |
| 252 | slot->sge_page_dma); |
| 253 | err_out_command_table: |
| 254 | dma_pool_free(hisi_hba->command_table_pool, slot->command_table, |
| 255 | slot->command_table_dma); |
| 256 | err_out_status_buf: |
| 257 | dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer, |
| 258 | slot->status_buffer_dma); |
| 259 | err_out_slot_buf: |
| 260 | /* Nothing to be done */ |
| 261 | err_out_tag: |
| 262 | hisi_sas_slot_index_free(hisi_hba, slot_idx); |
| 263 | err_out: |
| 264 | dev_err(dev, "task prep: failed[%d]!\n", rc); |
| 265 | if (!sas_protocol_ata(task->task_proto)) |
| 266 | if (n_elem) |
| 267 | dma_unmap_sg(dev, task->scatter, n_elem, |
| 268 | task->data_dir); |
| 269 | prep_out: |
| 270 | return rc; |
| 271 | } |
| 272 | |
| 273 | static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags, |
| 274 | int is_tmf, struct hisi_sas_tmf_task *tmf) |
| 275 | { |
| 276 | u32 rc; |
| 277 | u32 pass = 0; |
| 278 | unsigned long flags; |
| 279 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); |
| 280 | struct device *dev = &hisi_hba->pdev->dev; |
| 281 | |
| 282 | /* protect task_prep and start_delivery sequence */ |
| 283 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 284 | rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass); |
| 285 | if (rc) |
| 286 | dev_err(dev, "task exec: failed[%d]!\n", rc); |
| 287 | |
| 288 | if (likely(pass)) |
| 289 | hisi_hba->hw->start_delivery(hisi_hba); |
| 290 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 291 | |
| 292 | return rc; |
| 293 | } |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 294 | |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 295 | static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no) |
| 296 | { |
| 297 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 298 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 299 | struct sas_ha_struct *sas_ha; |
| 300 | |
| 301 | if (!phy->phy_attached) |
| 302 | return; |
| 303 | |
| 304 | sas_ha = &hisi_hba->sha; |
| 305 | sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE); |
| 306 | |
| 307 | if (sas_phy->phy) { |
| 308 | struct sas_phy *sphy = sas_phy->phy; |
| 309 | |
| 310 | sphy->negotiated_linkrate = sas_phy->linkrate; |
| 311 | sphy->minimum_linkrate = phy->minimum_linkrate; |
| 312 | sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS; |
| 313 | sphy->maximum_linkrate = phy->maximum_linkrate; |
| 314 | } |
| 315 | |
| 316 | if (phy->phy_type & PORT_TYPE_SAS) { |
| 317 | struct sas_identify_frame *id; |
| 318 | |
| 319 | id = (struct sas_identify_frame *)phy->frame_rcvd; |
| 320 | id->dev_type = phy->identify.device_type; |
| 321 | id->initiator_bits = SAS_PROTOCOL_ALL; |
| 322 | id->target_bits = phy->identify.target_port_protocols; |
| 323 | } else if (phy->phy_type & PORT_TYPE_SATA) { |
| 324 | /*Nothing*/ |
| 325 | } |
| 326 | |
| 327 | sas_phy->frame_rcvd_size = phy->frame_rcvd_size; |
| 328 | sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED); |
| 329 | } |
| 330 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 331 | static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device) |
| 332 | { |
| 333 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 334 | struct hisi_sas_device *sas_dev = NULL; |
| 335 | int i; |
| 336 | |
| 337 | spin_lock(&hisi_hba->lock); |
| 338 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 339 | if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) { |
| 340 | hisi_hba->devices[i].device_id = i; |
| 341 | sas_dev = &hisi_hba->devices[i]; |
| 342 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 343 | sas_dev->dev_type = device->dev_type; |
| 344 | sas_dev->hisi_hba = hisi_hba; |
| 345 | sas_dev->sas_device = device; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | spin_unlock(&hisi_hba->lock); |
| 350 | |
| 351 | return sas_dev; |
| 352 | } |
| 353 | |
| 354 | static int hisi_sas_dev_found(struct domain_device *device) |
| 355 | { |
| 356 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 357 | struct domain_device *parent_dev = device->parent; |
| 358 | struct hisi_sas_device *sas_dev; |
| 359 | struct device *dev = &hisi_hba->pdev->dev; |
| 360 | |
| 361 | sas_dev = hisi_sas_alloc_dev(device); |
| 362 | if (!sas_dev) { |
| 363 | dev_err(dev, "fail alloc dev: max support %d devices\n", |
| 364 | HISI_SAS_MAX_DEVICES); |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | |
| 368 | device->lldd_dev = sas_dev; |
| 369 | hisi_hba->hw->setup_itct(hisi_hba, sas_dev); |
| 370 | |
| 371 | if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { |
| 372 | int phy_no; |
| 373 | u8 phy_num = parent_dev->ex_dev.num_phys; |
| 374 | struct ex_phy *phy; |
| 375 | |
| 376 | for (phy_no = 0; phy_no < phy_num; phy_no++) { |
| 377 | phy = &parent_dev->ex_dev.ex_phy[phy_no]; |
| 378 | if (SAS_ADDR(phy->attached_sas_addr) == |
| 379 | SAS_ADDR(device->sas_addr)) { |
| 380 | sas_dev->attached_phy = phy_no; |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (phy_no == phy_num) { |
| 386 | dev_info(dev, "dev found: no attached " |
| 387 | "dev:%016llx at ex:%016llx\n", |
| 388 | SAS_ADDR(device->sas_addr), |
| 389 | SAS_ADDR(parent_dev->sas_addr)); |
| 390 | return -EINVAL; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 397 | static void hisi_sas_phyup_work(struct work_struct *work) |
| 398 | { |
| 399 | struct hisi_sas_phy *phy = |
| 400 | container_of(work, struct hisi_sas_phy, phyup_ws); |
| 401 | struct hisi_hba *hisi_hba = phy->hisi_hba; |
| 402 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 403 | int phy_no = sas_phy->id; |
| 404 | |
| 405 | hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */ |
| 406 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); |
| 407 | } |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 408 | |
| 409 | static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no) |
| 410 | { |
| 411 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 412 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 413 | |
| 414 | phy->hisi_hba = hisi_hba; |
| 415 | phy->port = NULL; |
| 416 | init_timer(&phy->timer); |
| 417 | sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0; |
| 418 | sas_phy->class = SAS; |
| 419 | sas_phy->iproto = SAS_PROTOCOL_ALL; |
| 420 | sas_phy->tproto = 0; |
| 421 | sas_phy->type = PHY_TYPE_PHYSICAL; |
| 422 | sas_phy->role = PHY_ROLE_INITIATOR; |
| 423 | sas_phy->oob_mode = OOB_NOT_CONNECTED; |
| 424 | sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN; |
| 425 | sas_phy->id = phy_no; |
| 426 | sas_phy->sas_addr = &hisi_hba->sas_addr[0]; |
| 427 | sas_phy->frame_rcvd = &phy->frame_rcvd[0]; |
| 428 | sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata; |
| 429 | sas_phy->lldd_phy = phy; |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 430 | |
| 431 | INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work); |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 432 | } |
| 433 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame^] | 434 | static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy) |
| 435 | { |
| 436 | struct sas_ha_struct *sas_ha = sas_phy->ha; |
| 437 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; |
| 438 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; |
| 439 | struct asd_sas_port *sas_port = sas_phy->port; |
| 440 | struct hisi_sas_port *port = &hisi_hba->port[sas_phy->id]; |
| 441 | unsigned long flags; |
| 442 | |
| 443 | if (!sas_port) |
| 444 | return; |
| 445 | |
| 446 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 447 | port->port_attached = 1; |
| 448 | port->id = phy->port_id; |
| 449 | phy->port = port; |
| 450 | sas_port->lldd_port = port; |
| 451 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 452 | } |
| 453 | |
| 454 | static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no, |
| 455 | struct domain_device *device) |
| 456 | { |
| 457 | struct hisi_sas_phy *phy; |
| 458 | struct hisi_sas_port *port; |
| 459 | struct hisi_sas_slot *slot, *slot2; |
| 460 | struct device *dev = &hisi_hba->pdev->dev; |
| 461 | |
| 462 | phy = &hisi_hba->phy[phy_no]; |
| 463 | port = phy->port; |
| 464 | if (!port) |
| 465 | return; |
| 466 | |
| 467 | list_for_each_entry_safe(slot, slot2, &port->list, entry) { |
| 468 | struct sas_task *task; |
| 469 | |
| 470 | task = slot->task; |
| 471 | if (device && task->dev != device) |
| 472 | continue; |
| 473 | |
| 474 | dev_info(dev, "Release slot [%d:%d], task [%p]:\n", |
| 475 | slot->dlvry_queue, slot->dlvry_queue_slot, task); |
| 476 | hisi_hba->hw->slot_complete(hisi_hba, slot, 1); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy) |
| 481 | { |
| 482 | struct domain_device *device; |
| 483 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; |
| 484 | struct asd_sas_port *sas_port = sas_phy->port; |
| 485 | |
| 486 | list_for_each_entry(device, &sas_port->dev_list, dev_list_node) |
| 487 | hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device); |
| 488 | } |
| 489 | |
| 490 | static void hisi_sas_release_task(struct hisi_hba *hisi_hba, |
| 491 | struct domain_device *device) |
| 492 | { |
| 493 | struct asd_sas_port *port = device->port; |
| 494 | struct asd_sas_phy *sas_phy; |
| 495 | |
| 496 | list_for_each_entry(sas_phy, &port->phy_list, port_phy_el) |
| 497 | hisi_sas_do_release_task(hisi_hba, sas_phy->id, device); |
| 498 | } |
| 499 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 500 | static void hisi_sas_dev_gone(struct domain_device *device) |
| 501 | { |
| 502 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 503 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 504 | struct device *dev = &hisi_hba->pdev->dev; |
| 505 | u64 dev_id = sas_dev->device_id; |
| 506 | |
| 507 | dev_info(dev, "found dev[%lld:%x] is gone\n", |
| 508 | sas_dev->device_id, sas_dev->dev_type); |
| 509 | |
| 510 | hisi_hba->hw->free_device(hisi_hba, sas_dev); |
| 511 | device->lldd_dev = NULL; |
| 512 | memset(sas_dev, 0, sizeof(*sas_dev)); |
| 513 | sas_dev->device_id = dev_id; |
| 514 | sas_dev->dev_type = SAS_PHY_UNUSED; |
| 515 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 516 | } |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 517 | |
| 518 | static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags) |
| 519 | { |
| 520 | return hisi_sas_task_exec(task, gfp_flags, 0, NULL); |
| 521 | } |
| 522 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame^] | 523 | |
| 524 | static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy) |
| 525 | { |
| 526 | hisi_sas_port_notify_formed(sas_phy); |
| 527 | } |
| 528 | |
| 529 | static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy) |
| 530 | { |
| 531 | hisi_sas_port_notify_deformed(sas_phy); |
| 532 | } |
| 533 | |
| 534 | static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy) |
| 535 | { |
| 536 | phy->phy_attached = 0; |
| 537 | phy->phy_type = 0; |
| 538 | phy->port = NULL; |
| 539 | } |
| 540 | |
| 541 | void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy) |
| 542 | { |
| 543 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 544 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 545 | struct sas_ha_struct *sas_ha = &hisi_hba->sha; |
| 546 | |
| 547 | if (rdy) { |
| 548 | /* Phy down but ready */ |
| 549 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); |
| 550 | hisi_sas_port_notify_formed(sas_phy); |
| 551 | } else { |
| 552 | struct hisi_sas_port *port = phy->port; |
| 553 | |
| 554 | /* Phy down and not ready */ |
| 555 | sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL); |
| 556 | sas_phy_disconnected(sas_phy); |
| 557 | |
| 558 | if (port) { |
| 559 | if (phy->phy_type & PORT_TYPE_SAS) { |
| 560 | int port_id = port->id; |
| 561 | |
| 562 | if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba, |
| 563 | port_id)) |
| 564 | port->port_attached = 0; |
| 565 | } else if (phy->phy_type & PORT_TYPE_SATA) |
| 566 | port->port_attached = 0; |
| 567 | } |
| 568 | hisi_sas_phy_disconnected(phy); |
| 569 | } |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(hisi_sas_phy_down); |
| 572 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 573 | static struct scsi_transport_template *hisi_sas_stt; |
| 574 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 575 | static struct scsi_host_template hisi_sas_sht = { |
| 576 | .module = THIS_MODULE, |
| 577 | .name = DRV_NAME, |
| 578 | .queuecommand = sas_queuecommand, |
| 579 | .target_alloc = sas_target_alloc, |
| 580 | .slave_configure = sas_slave_configure, |
| 581 | .change_queue_depth = sas_change_queue_depth, |
| 582 | .bios_param = sas_bios_param, |
| 583 | .can_queue = 1, |
| 584 | .this_id = -1, |
| 585 | .sg_tablesize = SG_ALL, |
| 586 | .max_sectors = SCSI_DEFAULT_MAX_SECTORS, |
| 587 | .use_clustering = ENABLE_CLUSTERING, |
| 588 | .eh_device_reset_handler = sas_eh_device_reset_handler, |
| 589 | .eh_bus_reset_handler = sas_eh_bus_reset_handler, |
| 590 | .target_destroy = sas_target_destroy, |
| 591 | .ioctl = sas_ioctl, |
| 592 | }; |
| 593 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 594 | static struct sas_domain_function_template hisi_sas_transport_ops = { |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 595 | .lldd_dev_found = hisi_sas_dev_found, |
| 596 | .lldd_dev_gone = hisi_sas_dev_gone, |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 597 | .lldd_execute_task = hisi_sas_queue_command, |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame^] | 598 | .lldd_port_formed = hisi_sas_port_formed, |
| 599 | .lldd_port_deformed = hisi_sas_port_deformed, |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 600 | }; |
| 601 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 602 | static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost) |
| 603 | { |
| 604 | int i, s; |
| 605 | struct platform_device *pdev = hisi_hba->pdev; |
| 606 | struct device *dev = &pdev->dev; |
| 607 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 608 | spin_lock_init(&hisi_hba->lock); |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 609 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 610 | hisi_sas_phy_init(hisi_hba, i); |
| 611 | hisi_hba->port[i].port_attached = 0; |
| 612 | hisi_hba->port[i].id = -1; |
| 613 | INIT_LIST_HEAD(&hisi_hba->port[i].list); |
| 614 | } |
| 615 | |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 616 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 617 | hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED; |
| 618 | hisi_hba->devices[i].device_id = i; |
| 619 | hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL; |
| 620 | } |
| 621 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 622 | for (i = 0; i < hisi_hba->queue_count; i++) { |
John Garry | 9101a07 | 2015-11-18 00:50:37 +0800 | [diff] [blame] | 623 | struct hisi_sas_cq *cq = &hisi_hba->cq[i]; |
| 624 | |
| 625 | /* Completion queue structure */ |
| 626 | cq->id = i; |
| 627 | cq->hisi_hba = hisi_hba; |
| 628 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 629 | /* Delivery queue */ |
| 630 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 631 | hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s, |
| 632 | &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL); |
| 633 | if (!hisi_hba->cmd_hdr[i]) |
| 634 | goto err_out; |
| 635 | memset(hisi_hba->cmd_hdr[i], 0, s); |
| 636 | |
| 637 | /* Completion queue */ |
| 638 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 639 | hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s, |
| 640 | &hisi_hba->complete_hdr_dma[i], GFP_KERNEL); |
| 641 | if (!hisi_hba->complete_hdr[i]) |
| 642 | goto err_out; |
| 643 | memset(hisi_hba->complete_hdr[i], 0, s); |
| 644 | } |
| 645 | |
| 646 | s = HISI_SAS_STATUS_BUF_SZ; |
| 647 | hisi_hba->status_buffer_pool = dma_pool_create("status_buffer", |
| 648 | dev, s, 16, 0); |
| 649 | if (!hisi_hba->status_buffer_pool) |
| 650 | goto err_out; |
| 651 | |
| 652 | s = HISI_SAS_COMMAND_TABLE_SZ; |
| 653 | hisi_hba->command_table_pool = dma_pool_create("command_table", |
| 654 | dev, s, 16, 0); |
| 655 | if (!hisi_hba->command_table_pool) |
| 656 | goto err_out; |
| 657 | |
| 658 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 659 | hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma, |
| 660 | GFP_KERNEL); |
| 661 | if (!hisi_hba->itct) |
| 662 | goto err_out; |
| 663 | |
| 664 | memset(hisi_hba->itct, 0, s); |
| 665 | |
| 666 | hisi_hba->slot_info = devm_kcalloc(dev, HISI_SAS_COMMAND_ENTRIES, |
| 667 | sizeof(struct hisi_sas_slot), |
| 668 | GFP_KERNEL); |
| 669 | if (!hisi_hba->slot_info) |
| 670 | goto err_out; |
| 671 | |
| 672 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost); |
| 673 | hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma, |
| 674 | GFP_KERNEL); |
| 675 | if (!hisi_hba->iost) |
| 676 | goto err_out; |
| 677 | |
| 678 | memset(hisi_hba->iost, 0, s); |
| 679 | |
| 680 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint); |
| 681 | hisi_hba->breakpoint = dma_alloc_coherent(dev, s, |
| 682 | &hisi_hba->breakpoint_dma, GFP_KERNEL); |
| 683 | if (!hisi_hba->breakpoint) |
| 684 | goto err_out; |
| 685 | |
| 686 | memset(hisi_hba->breakpoint, 0, s); |
| 687 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 688 | hisi_hba->slot_index_count = HISI_SAS_COMMAND_ENTRIES; |
| 689 | s = hisi_hba->slot_index_count / sizeof(unsigned long); |
| 690 | hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL); |
| 691 | if (!hisi_hba->slot_index_tags) |
| 692 | goto err_out; |
| 693 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 694 | hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev, |
| 695 | sizeof(struct hisi_sas_sge_page), 16, 0); |
| 696 | if (!hisi_hba->sge_page_pool) |
| 697 | goto err_out; |
| 698 | |
| 699 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 700 | hisi_hba->initial_fis = dma_alloc_coherent(dev, s, |
| 701 | &hisi_hba->initial_fis_dma, GFP_KERNEL); |
| 702 | if (!hisi_hba->initial_fis) |
| 703 | goto err_out; |
| 704 | memset(hisi_hba->initial_fis, 0, s); |
| 705 | |
| 706 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2; |
| 707 | hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s, |
| 708 | &hisi_hba->sata_breakpoint_dma, GFP_KERNEL); |
| 709 | if (!hisi_hba->sata_breakpoint) |
| 710 | goto err_out; |
| 711 | memset(hisi_hba->sata_breakpoint, 0, s); |
| 712 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 713 | hisi_sas_slot_index_init(hisi_hba); |
| 714 | |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 715 | hisi_hba->wq = create_singlethread_workqueue(dev_name(dev)); |
| 716 | if (!hisi_hba->wq) { |
| 717 | dev_err(dev, "sas_alloc: failed to create workqueue\n"); |
| 718 | goto err_out; |
| 719 | } |
| 720 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 721 | return 0; |
| 722 | err_out: |
| 723 | return -ENOMEM; |
| 724 | } |
| 725 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 726 | static void hisi_sas_free(struct hisi_hba *hisi_hba) |
| 727 | { |
| 728 | struct device *dev = &hisi_hba->pdev->dev; |
| 729 | int i, s; |
| 730 | |
| 731 | for (i = 0; i < hisi_hba->queue_count; i++) { |
| 732 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 733 | if (hisi_hba->cmd_hdr[i]) |
| 734 | dma_free_coherent(dev, s, |
| 735 | hisi_hba->cmd_hdr[i], |
| 736 | hisi_hba->cmd_hdr_dma[i]); |
| 737 | |
| 738 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 739 | if (hisi_hba->complete_hdr[i]) |
| 740 | dma_free_coherent(dev, s, |
| 741 | hisi_hba->complete_hdr[i], |
| 742 | hisi_hba->complete_hdr_dma[i]); |
| 743 | } |
| 744 | |
| 745 | dma_pool_destroy(hisi_hba->status_buffer_pool); |
| 746 | dma_pool_destroy(hisi_hba->command_table_pool); |
| 747 | dma_pool_destroy(hisi_hba->sge_page_pool); |
| 748 | |
| 749 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 750 | if (hisi_hba->itct) |
| 751 | dma_free_coherent(dev, s, |
| 752 | hisi_hba->itct, hisi_hba->itct_dma); |
| 753 | |
| 754 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_iost); |
| 755 | if (hisi_hba->iost) |
| 756 | dma_free_coherent(dev, s, |
| 757 | hisi_hba->iost, hisi_hba->iost_dma); |
| 758 | |
| 759 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint); |
| 760 | if (hisi_hba->breakpoint) |
| 761 | dma_free_coherent(dev, s, |
| 762 | hisi_hba->breakpoint, |
| 763 | hisi_hba->breakpoint_dma); |
| 764 | |
| 765 | |
| 766 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 767 | if (hisi_hba->initial_fis) |
| 768 | dma_free_coherent(dev, s, |
| 769 | hisi_hba->initial_fis, |
| 770 | hisi_hba->initial_fis_dma); |
| 771 | |
| 772 | s = HISI_SAS_COMMAND_ENTRIES * sizeof(struct hisi_sas_breakpoint) * 2; |
| 773 | if (hisi_hba->sata_breakpoint) |
| 774 | dma_free_coherent(dev, s, |
| 775 | hisi_hba->sata_breakpoint, |
| 776 | hisi_hba->sata_breakpoint_dma); |
| 777 | |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 778 | if (hisi_hba->wq) |
| 779 | destroy_workqueue(hisi_hba->wq); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 780 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 781 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 782 | static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, |
| 783 | const struct hisi_sas_hw *hw) |
| 784 | { |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 785 | struct resource *res; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 786 | struct Scsi_Host *shost; |
| 787 | struct hisi_hba *hisi_hba; |
| 788 | struct device *dev = &pdev->dev; |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 789 | struct device_node *np = pdev->dev.of_node; |
| 790 | struct property *sas_addr_prop; |
| 791 | int num; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 792 | |
| 793 | shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba)); |
| 794 | if (!shost) |
| 795 | goto err_out; |
| 796 | hisi_hba = shost_priv(shost); |
| 797 | |
| 798 | hisi_hba->hw = hw; |
| 799 | hisi_hba->pdev = pdev; |
| 800 | hisi_hba->shost = shost; |
| 801 | SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; |
| 802 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 803 | init_timer(&hisi_hba->timer); |
| 804 | |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 805 | sas_addr_prop = of_find_property(np, "sas-addr", NULL); |
| 806 | if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE)) |
| 807 | goto err_out; |
| 808 | memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE); |
| 809 | |
| 810 | if (of_property_read_u32(np, "ctrl-reset-reg", |
| 811 | &hisi_hba->ctrl_reset_reg)) |
| 812 | goto err_out; |
| 813 | |
| 814 | if (of_property_read_u32(np, "ctrl-reset-sts-reg", |
| 815 | &hisi_hba->ctrl_reset_sts_reg)) |
| 816 | goto err_out; |
| 817 | |
| 818 | if (of_property_read_u32(np, "ctrl-clock-ena-reg", |
| 819 | &hisi_hba->ctrl_clock_ena_reg)) |
| 820 | goto err_out; |
| 821 | |
| 822 | if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy)) |
| 823 | goto err_out; |
| 824 | |
| 825 | if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count)) |
| 826 | goto err_out; |
| 827 | |
| 828 | num = of_irq_count(np); |
| 829 | hisi_hba->int_names = devm_kcalloc(dev, num, |
| 830 | HISI_SAS_NAME_LEN, |
| 831 | GFP_KERNEL); |
| 832 | if (!hisi_hba->int_names) |
| 833 | goto err_out; |
| 834 | |
| 835 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 836 | hisi_hba->regs = devm_ioremap_resource(dev, res); |
| 837 | if (IS_ERR(hisi_hba->regs)) |
| 838 | goto err_out; |
| 839 | |
| 840 | hisi_hba->ctrl = syscon_regmap_lookup_by_phandle( |
| 841 | np, "hisilicon,sas-syscon"); |
| 842 | if (IS_ERR(hisi_hba->ctrl)) |
| 843 | goto err_out; |
| 844 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 845 | if (hisi_sas_alloc(hisi_hba, shost)) { |
| 846 | hisi_sas_free(hisi_hba); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 847 | goto err_out; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 848 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 849 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 850 | return shost; |
| 851 | err_out: |
| 852 | dev_err(dev, "shost alloc failed\n"); |
| 853 | return NULL; |
| 854 | } |
| 855 | |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 856 | static void hisi_sas_init_add(struct hisi_hba *hisi_hba) |
| 857 | { |
| 858 | int i; |
| 859 | |
| 860 | for (i = 0; i < hisi_hba->n_phy; i++) |
| 861 | memcpy(&hisi_hba->phy[i].dev_sas_addr, |
| 862 | hisi_hba->sas_addr, |
| 863 | SAS_ADDR_SIZE); |
| 864 | } |
| 865 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 866 | int hisi_sas_probe(struct platform_device *pdev, |
| 867 | const struct hisi_sas_hw *hw) |
| 868 | { |
| 869 | struct Scsi_Host *shost; |
| 870 | struct hisi_hba *hisi_hba; |
| 871 | struct device *dev = &pdev->dev; |
| 872 | struct asd_sas_phy **arr_phy; |
| 873 | struct asd_sas_port **arr_port; |
| 874 | struct sas_ha_struct *sha; |
| 875 | int rc, phy_nr, port_nr, i; |
| 876 | |
| 877 | shost = hisi_sas_shost_alloc(pdev, hw); |
| 878 | if (!shost) { |
| 879 | rc = -ENOMEM; |
| 880 | goto err_out_ha; |
| 881 | } |
| 882 | |
| 883 | sha = SHOST_TO_SAS_HA(shost); |
| 884 | hisi_hba = shost_priv(shost); |
| 885 | platform_set_drvdata(pdev, sha); |
John Garry | 50cb916 | 2015-11-18 00:50:39 +0800 | [diff] [blame] | 886 | |
| 887 | if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) && |
| 888 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) { |
| 889 | dev_err(dev, "No usable DMA addressing method\n"); |
| 890 | rc = -EIO; |
| 891 | goto err_out_ha; |
| 892 | } |
| 893 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 894 | phy_nr = port_nr = hisi_hba->n_phy; |
| 895 | |
| 896 | arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); |
| 897 | arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); |
| 898 | if (!arr_phy || !arr_port) |
| 899 | return -ENOMEM; |
| 900 | |
| 901 | sha->sas_phy = arr_phy; |
| 902 | sha->sas_port = arr_port; |
| 903 | sha->core.shost = shost; |
| 904 | sha->lldd_ha = hisi_hba; |
| 905 | |
| 906 | shost->transportt = hisi_sas_stt; |
| 907 | shost->max_id = HISI_SAS_MAX_DEVICES; |
| 908 | shost->max_lun = ~0; |
| 909 | shost->max_channel = 1; |
| 910 | shost->max_cmd_len = 16; |
| 911 | shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT); |
| 912 | shost->can_queue = HISI_SAS_COMMAND_ENTRIES; |
| 913 | shost->cmd_per_lun = HISI_SAS_COMMAND_ENTRIES; |
| 914 | |
| 915 | sha->sas_ha_name = DRV_NAME; |
| 916 | sha->dev = &hisi_hba->pdev->dev; |
| 917 | sha->lldd_module = THIS_MODULE; |
| 918 | sha->sas_addr = &hisi_hba->sas_addr[0]; |
| 919 | sha->num_phys = hisi_hba->n_phy; |
| 920 | sha->core.shost = hisi_hba->shost; |
| 921 | |
| 922 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 923 | sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; |
| 924 | sha->sas_port[i] = &hisi_hba->port[i].sas_port; |
| 925 | } |
| 926 | |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 927 | hisi_sas_init_add(hisi_hba); |
| 928 | |
John Garry | 8ff1d57 | 2015-11-18 00:50:46 +0800 | [diff] [blame] | 929 | rc = hisi_hba->hw->hw_init(hisi_hba); |
| 930 | if (rc) |
| 931 | goto err_out_ha; |
| 932 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 933 | rc = scsi_add_host(shost, &pdev->dev); |
| 934 | if (rc) |
| 935 | goto err_out_ha; |
| 936 | |
| 937 | rc = sas_register_ha(sha); |
| 938 | if (rc) |
| 939 | goto err_out_register_ha; |
| 940 | |
| 941 | scsi_scan_host(shost); |
| 942 | |
| 943 | return 0; |
| 944 | |
| 945 | err_out_register_ha: |
| 946 | scsi_remove_host(shost); |
| 947 | err_out_ha: |
| 948 | kfree(shost); |
| 949 | return rc; |
| 950 | } |
| 951 | EXPORT_SYMBOL_GPL(hisi_sas_probe); |
| 952 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 953 | int hisi_sas_remove(struct platform_device *pdev) |
| 954 | { |
| 955 | struct sas_ha_struct *sha = platform_get_drvdata(pdev); |
| 956 | struct hisi_hba *hisi_hba = sha->lldd_ha; |
| 957 | |
| 958 | scsi_remove_host(sha->core.shost); |
| 959 | sas_unregister_ha(sha); |
| 960 | sas_remove_host(sha->core.shost); |
| 961 | |
| 962 | hisi_sas_free(hisi_hba); |
| 963 | return 0; |
| 964 | } |
| 965 | EXPORT_SYMBOL_GPL(hisi_sas_remove); |
| 966 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 967 | static __init int hisi_sas_init(void) |
| 968 | { |
| 969 | pr_info("hisi_sas: driver version %s\n", DRV_VERSION); |
| 970 | |
| 971 | hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); |
| 972 | if (!hisi_sas_stt) |
| 973 | return -ENOMEM; |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static __exit void hisi_sas_exit(void) |
| 979 | { |
| 980 | sas_release_transport(hisi_sas_stt); |
| 981 | } |
| 982 | |
| 983 | module_init(hisi_sas_init); |
| 984 | module_exit(hisi_sas_exit); |
| 985 | |
| 986 | MODULE_VERSION(DRV_VERSION); |
| 987 | MODULE_LICENSE("GPL"); |
| 988 | MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); |
| 989 | MODULE_DESCRIPTION("HISILICON SAS controller driver"); |
| 990 | MODULE_ALIAS("platform:" DRV_NAME); |