]>
Commit | Line | Data |
---|---|---|
e8899fad JG |
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 | ||
42e7a693 JG |
15 | #define DEV_IS_GONE(dev) \ |
16 | ((!dev) || (dev->dev_type == SAS_PHY_UNUSED)) | |
17 | ||
cac9b2a2 JG |
18 | static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device, |
19 | u8 *lun, struct hisi_sas_tmf_task *tmf); | |
441c2740 JG |
20 | static int |
21 | hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba, | |
22 | struct domain_device *device, | |
23 | int abort_flag, int tag); | |
cac9b2a2 | 24 | |
42e7a693 JG |
25 | static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device) |
26 | { | |
27 | return device->port->ha->lldd_ha; | |
28 | } | |
29 | ||
257efd1f JG |
30 | static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx) |
31 | { | |
32 | void *bitmap = hisi_hba->slot_index_tags; | |
33 | ||
34 | clear_bit(slot_idx, bitmap); | |
35 | } | |
36 | ||
42e7a693 JG |
37 | static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx) |
38 | { | |
39 | hisi_sas_slot_index_clear(hisi_hba, slot_idx); | |
40 | } | |
41 | ||
42 | static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx) | |
43 | { | |
44 | void *bitmap = hisi_hba->slot_index_tags; | |
45 | ||
46 | set_bit(slot_idx, bitmap); | |
47 | } | |
48 | ||
49 | static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx) | |
50 | { | |
51 | unsigned int index; | |
52 | void *bitmap = hisi_hba->slot_index_tags; | |
53 | ||
54 | index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count); | |
55 | if (index >= hisi_hba->slot_index_count) | |
56 | return -SAS_QUEUE_FULL; | |
57 | hisi_sas_slot_index_set(hisi_hba, index); | |
58 | *slot_idx = index; | |
59 | return 0; | |
60 | } | |
61 | ||
257efd1f JG |
62 | static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba) |
63 | { | |
64 | int i; | |
65 | ||
66 | for (i = 0; i < hisi_hba->slot_index_count; ++i) | |
67 | hisi_sas_slot_index_clear(hisi_hba, i); | |
68 | } | |
27a3f229 JG |
69 | |
70 | void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task, | |
71 | struct hisi_sas_slot *slot) | |
72 | { | |
73 | struct device *dev = &hisi_hba->pdev->dev; | |
74 | ||
75 | if (!slot->task) | |
76 | return; | |
77 | ||
78 | if (!sas_protocol_ata(task->task_proto)) | |
79 | if (slot->n_elem) | |
80 | dma_unmap_sg(dev, task->scatter, slot->n_elem, | |
81 | task->data_dir); | |
82 | ||
83 | if (slot->command_table) | |
84 | dma_pool_free(hisi_hba->command_table_pool, | |
85 | slot->command_table, slot->command_table_dma); | |
86 | ||
87 | if (slot->status_buffer) | |
88 | dma_pool_free(hisi_hba->status_buffer_pool, | |
89 | slot->status_buffer, slot->status_buffer_dma); | |
90 | ||
91 | if (slot->sge_page) | |
92 | dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page, | |
93 | slot->sge_page_dma); | |
94 | ||
95 | list_del_init(&slot->entry); | |
96 | task->lldd_task = NULL; | |
97 | slot->task = NULL; | |
98 | slot->port = NULL; | |
99 | hisi_sas_slot_index_free(hisi_hba, slot->idx); | |
59ba49f9 | 100 | /* slot memory is fully zeroed when it is reused */ |
27a3f229 JG |
101 | } |
102 | EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free); | |
103 | ||
66ee999b JG |
104 | static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba, |
105 | struct hisi_sas_slot *slot) | |
106 | { | |
107 | return hisi_hba->hw->prep_smp(hisi_hba, slot); | |
108 | } | |
109 | ||
42e7a693 JG |
110 | static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba, |
111 | struct hisi_sas_slot *slot, int is_tmf, | |
112 | struct hisi_sas_tmf_task *tmf) | |
113 | { | |
114 | return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf); | |
115 | } | |
116 | ||
6f2ff1a1 JG |
117 | static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba, |
118 | struct hisi_sas_slot *slot) | |
119 | { | |
120 | return hisi_hba->hw->prep_stp(hisi_hba, slot); | |
121 | } | |
122 | ||
441c2740 JG |
123 | static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba, |
124 | struct hisi_sas_slot *slot, | |
125 | int device_id, int abort_flag, int tag_to_abort) | |
126 | { | |
127 | return hisi_hba->hw->prep_abort(hisi_hba, slot, | |
128 | device_id, abort_flag, tag_to_abort); | |
129 | } | |
130 | ||
cac9b2a2 JG |
131 | /* |
132 | * This function will issue an abort TMF regardless of whether the | |
133 | * task is in the sdev or not. Then it will do the task complete | |
134 | * cleanup and callbacks. | |
135 | */ | |
136 | static void hisi_sas_slot_abort(struct work_struct *work) | |
137 | { | |
138 | struct hisi_sas_slot *abort_slot = | |
139 | container_of(work, struct hisi_sas_slot, abort_slot); | |
140 | struct sas_task *task = abort_slot->task; | |
141 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); | |
142 | struct scsi_cmnd *cmnd = task->uldd_task; | |
143 | struct hisi_sas_tmf_task tmf_task; | |
144 | struct domain_device *device = task->dev; | |
145 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
146 | struct scsi_lun lun; | |
147 | struct device *dev = &hisi_hba->pdev->dev; | |
148 | int tag = abort_slot->idx; | |
149 | ||
150 | if (!(task->task_proto & SAS_PROTOCOL_SSP)) { | |
151 | dev_err(dev, "cannot abort slot for non-ssp task\n"); | |
152 | goto out; | |
153 | } | |
154 | ||
155 | int_to_scsilun(cmnd->device->lun, &lun); | |
156 | tmf_task.tmf = TMF_ABORT_TASK; | |
157 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); | |
158 | ||
159 | hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task); | |
160 | out: | |
161 | /* Do cleanup for this task */ | |
162 | hisi_sas_slot_task_free(hisi_hba, task, abort_slot); | |
163 | if (task->task_done) | |
164 | task->task_done(task); | |
165 | if (sas_dev && sas_dev->running_req) | |
166 | sas_dev->running_req--; | |
167 | } | |
168 | ||
42e7a693 JG |
169 | static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba, |
170 | int is_tmf, struct hisi_sas_tmf_task *tmf, | |
171 | int *pass) | |
172 | { | |
173 | struct domain_device *device = task->dev; | |
174 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
175 | struct hisi_sas_port *port; | |
176 | struct hisi_sas_slot *slot; | |
177 | struct hisi_sas_cmd_hdr *cmd_hdr_base; | |
178 | struct device *dev = &hisi_hba->pdev->dev; | |
179 | int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; | |
180 | ||
181 | if (!device->port) { | |
182 | struct task_status_struct *ts = &task->task_status; | |
183 | ||
184 | ts->resp = SAS_TASK_UNDELIVERED; | |
185 | ts->stat = SAS_PHY_DOWN; | |
186 | /* | |
187 | * libsas will use dev->port, should | |
188 | * not call task_done for sata | |
189 | */ | |
190 | if (device->dev_type != SAS_SATA_DEV) | |
191 | task->task_done(task); | |
192 | return 0; | |
193 | } | |
194 | ||
195 | if (DEV_IS_GONE(sas_dev)) { | |
196 | if (sas_dev) | |
197 | dev_info(dev, "task prep: device %llu not ready\n", | |
198 | sas_dev->device_id); | |
199 | else | |
200 | dev_info(dev, "task prep: device %016llx not ready\n", | |
201 | SAS_ADDR(device->sas_addr)); | |
202 | ||
203 | rc = SAS_PHY_DOWN; | |
204 | return rc; | |
205 | } | |
206 | port = device->port->lldd_port; | |
9859f24e | 207 | if (port && !port->port_attached) { |
09fe9ecb JG |
208 | dev_info(dev, "task prep: %s port%d not attach device\n", |
209 | (sas_protocol_ata(task->task_proto)) ? | |
210 | "SATA/STP" : "SAS", | |
211 | device->port->id); | |
212 | ||
213 | return SAS_PHY_DOWN; | |
42e7a693 JG |
214 | } |
215 | ||
216 | if (!sas_protocol_ata(task->task_proto)) { | |
217 | if (task->num_scatter) { | |
218 | n_elem = dma_map_sg(dev, task->scatter, | |
219 | task->num_scatter, task->data_dir); | |
220 | if (!n_elem) { | |
221 | rc = -ENOMEM; | |
222 | goto prep_out; | |
223 | } | |
224 | } | |
225 | } else | |
226 | n_elem = task->num_scatter; | |
227 | ||
685b6d6e JG |
228 | if (hisi_hba->hw->slot_index_alloc) |
229 | rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx, | |
230 | device); | |
231 | else | |
232 | rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx); | |
42e7a693 JG |
233 | if (rc) |
234 | goto err_out; | |
235 | rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue, | |
236 | &dlvry_queue_slot); | |
237 | if (rc) | |
238 | goto err_out_tag; | |
239 | ||
240 | slot = &hisi_hba->slot_info[slot_idx]; | |
241 | memset(slot, 0, sizeof(struct hisi_sas_slot)); | |
242 | ||
243 | slot->idx = slot_idx; | |
244 | slot->n_elem = n_elem; | |
245 | slot->dlvry_queue = dlvry_queue; | |
246 | slot->dlvry_queue_slot = dlvry_queue_slot; | |
247 | cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; | |
248 | slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; | |
249 | slot->task = task; | |
250 | slot->port = port; | |
251 | task->lldd_task = slot; | |
cac9b2a2 | 252 | INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort); |
42e7a693 JG |
253 | |
254 | slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool, | |
255 | GFP_ATOMIC, | |
256 | &slot->status_buffer_dma); | |
9c9d18e7 DC |
257 | if (!slot->status_buffer) { |
258 | rc = -ENOMEM; | |
42e7a693 | 259 | goto err_out_slot_buf; |
9c9d18e7 | 260 | } |
42e7a693 JG |
261 | memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ); |
262 | ||
263 | slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool, | |
264 | GFP_ATOMIC, | |
265 | &slot->command_table_dma); | |
9c9d18e7 DC |
266 | if (!slot->command_table) { |
267 | rc = -ENOMEM; | |
42e7a693 | 268 | goto err_out_status_buf; |
9c9d18e7 | 269 | } |
42e7a693 JG |
270 | memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ); |
271 | memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); | |
272 | ||
273 | switch (task->task_proto) { | |
66ee999b JG |
274 | case SAS_PROTOCOL_SMP: |
275 | rc = hisi_sas_task_prep_smp(hisi_hba, slot); | |
276 | break; | |
42e7a693 JG |
277 | case SAS_PROTOCOL_SSP: |
278 | rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf); | |
279 | break; | |
280 | case SAS_PROTOCOL_SATA: | |
281 | case SAS_PROTOCOL_STP: | |
282 | case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: | |
6f2ff1a1 JG |
283 | rc = hisi_sas_task_prep_ata(hisi_hba, slot); |
284 | break; | |
42e7a693 JG |
285 | default: |
286 | dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n", | |
287 | task->task_proto); | |
288 | rc = -EINVAL; | |
289 | break; | |
290 | } | |
291 | ||
292 | if (rc) { | |
293 | dev_err(dev, "task prep: rc = 0x%x\n", rc); | |
294 | if (slot->sge_page) | |
295 | goto err_out_sge; | |
296 | goto err_out_command_table; | |
297 | } | |
298 | ||
299 | list_add_tail(&slot->entry, &port->list); | |
300 | spin_lock(&task->task_state_lock); | |
301 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; | |
302 | spin_unlock(&task->task_state_lock); | |
303 | ||
304 | hisi_hba->slot_prep = slot; | |
305 | ||
306 | sas_dev->running_req++; | |
307 | ++(*pass); | |
308 | ||
9c9d18e7 | 309 | return 0; |
42e7a693 JG |
310 | |
311 | err_out_sge: | |
312 | dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page, | |
313 | slot->sge_page_dma); | |
314 | err_out_command_table: | |
315 | dma_pool_free(hisi_hba->command_table_pool, slot->command_table, | |
316 | slot->command_table_dma); | |
317 | err_out_status_buf: | |
318 | dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer, | |
319 | slot->status_buffer_dma); | |
320 | err_out_slot_buf: | |
321 | /* Nothing to be done */ | |
322 | err_out_tag: | |
323 | hisi_sas_slot_index_free(hisi_hba, slot_idx); | |
324 | err_out: | |
325 | dev_err(dev, "task prep: failed[%d]!\n", rc); | |
326 | if (!sas_protocol_ata(task->task_proto)) | |
327 | if (n_elem) | |
328 | dma_unmap_sg(dev, task->scatter, n_elem, | |
329 | task->data_dir); | |
330 | prep_out: | |
331 | return rc; | |
332 | } | |
333 | ||
334 | static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags, | |
335 | int is_tmf, struct hisi_sas_tmf_task *tmf) | |
336 | { | |
337 | u32 rc; | |
338 | u32 pass = 0; | |
339 | unsigned long flags; | |
340 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); | |
341 | struct device *dev = &hisi_hba->pdev->dev; | |
342 | ||
343 | /* protect task_prep and start_delivery sequence */ | |
344 | spin_lock_irqsave(&hisi_hba->lock, flags); | |
345 | rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass); | |
346 | if (rc) | |
347 | dev_err(dev, "task exec: failed[%d]!\n", rc); | |
348 | ||
349 | if (likely(pass)) | |
350 | hisi_hba->hw->start_delivery(hisi_hba); | |
351 | spin_unlock_irqrestore(&hisi_hba->lock, flags); | |
352 | ||
353 | return rc; | |
354 | } | |
257efd1f | 355 | |
66139921 JG |
356 | static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no) |
357 | { | |
358 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; | |
359 | struct asd_sas_phy *sas_phy = &phy->sas_phy; | |
360 | struct sas_ha_struct *sas_ha; | |
361 | ||
362 | if (!phy->phy_attached) | |
363 | return; | |
364 | ||
365 | sas_ha = &hisi_hba->sha; | |
366 | sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE); | |
367 | ||
368 | if (sas_phy->phy) { | |
369 | struct sas_phy *sphy = sas_phy->phy; | |
370 | ||
371 | sphy->negotiated_linkrate = sas_phy->linkrate; | |
372 | sphy->minimum_linkrate = phy->minimum_linkrate; | |
373 | sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS; | |
374 | sphy->maximum_linkrate = phy->maximum_linkrate; | |
375 | } | |
376 | ||
377 | if (phy->phy_type & PORT_TYPE_SAS) { | |
378 | struct sas_identify_frame *id; | |
379 | ||
380 | id = (struct sas_identify_frame *)phy->frame_rcvd; | |
381 | id->dev_type = phy->identify.device_type; | |
382 | id->initiator_bits = SAS_PROTOCOL_ALL; | |
383 | id->target_bits = phy->identify.target_port_protocols; | |
384 | } else if (phy->phy_type & PORT_TYPE_SATA) { | |
385 | /*Nothing*/ | |
386 | } | |
387 | ||
388 | sas_phy->frame_rcvd_size = phy->frame_rcvd_size; | |
389 | sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED); | |
390 | } | |
391 | ||
abda97c2 JG |
392 | static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device) |
393 | { | |
394 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); | |
395 | struct hisi_sas_device *sas_dev = NULL; | |
396 | int i; | |
397 | ||
398 | spin_lock(&hisi_hba->lock); | |
399 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { | |
400 | if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) { | |
401 | hisi_hba->devices[i].device_id = i; | |
402 | sas_dev = &hisi_hba->devices[i]; | |
403 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; | |
404 | sas_dev->dev_type = device->dev_type; | |
405 | sas_dev->hisi_hba = hisi_hba; | |
406 | sas_dev->sas_device = device; | |
407 | break; | |
408 | } | |
409 | } | |
410 | spin_unlock(&hisi_hba->lock); | |
411 | ||
412 | return sas_dev; | |
413 | } | |
414 | ||
415 | static int hisi_sas_dev_found(struct domain_device *device) | |
416 | { | |
417 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); | |
418 | struct domain_device *parent_dev = device->parent; | |
419 | struct hisi_sas_device *sas_dev; | |
420 | struct device *dev = &hisi_hba->pdev->dev; | |
421 | ||
685b6d6e JG |
422 | if (hisi_hba->hw->alloc_dev) |
423 | sas_dev = hisi_hba->hw->alloc_dev(device); | |
424 | else | |
425 | sas_dev = hisi_sas_alloc_dev(device); | |
abda97c2 JG |
426 | if (!sas_dev) { |
427 | dev_err(dev, "fail alloc dev: max support %d devices\n", | |
428 | HISI_SAS_MAX_DEVICES); | |
429 | return -EINVAL; | |
430 | } | |
431 | ||
432 | device->lldd_dev = sas_dev; | |
433 | hisi_hba->hw->setup_itct(hisi_hba, sas_dev); | |
434 | ||
435 | if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { | |
436 | int phy_no; | |
437 | u8 phy_num = parent_dev->ex_dev.num_phys; | |
438 | struct ex_phy *phy; | |
439 | ||
440 | for (phy_no = 0; phy_no < phy_num; phy_no++) { | |
441 | phy = &parent_dev->ex_dev.ex_phy[phy_no]; | |
442 | if (SAS_ADDR(phy->attached_sas_addr) == | |
443 | SAS_ADDR(device->sas_addr)) { | |
444 | sas_dev->attached_phy = phy_no; | |
445 | break; | |
446 | } | |
447 | } | |
448 | ||
449 | if (phy_no == phy_num) { | |
450 | dev_info(dev, "dev found: no attached " | |
451 | "dev:%016llx at ex:%016llx\n", | |
452 | SAS_ADDR(device->sas_addr), | |
453 | SAS_ADDR(parent_dev->sas_addr)); | |
454 | return -EINVAL; | |
455 | } | |
456 | } | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
31eec8a6 JG |
461 | static int hisi_sas_slave_configure(struct scsi_device *sdev) |
462 | { | |
463 | struct domain_device *dev = sdev_to_domain_dev(sdev); | |
464 | int ret = sas_slave_configure(sdev); | |
465 | ||
466 | if (ret) | |
467 | return ret; | |
468 | if (!dev_is_sata(dev)) | |
469 | sas_change_queue_depth(sdev, 64); | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
701f75ec JG |
474 | static void hisi_sas_scan_start(struct Scsi_Host *shost) |
475 | { | |
476 | struct hisi_hba *hisi_hba = shost_priv(shost); | |
477 | int i; | |
478 | ||
479 | for (i = 0; i < hisi_hba->n_phy; ++i) | |
480 | hisi_sas_bytes_dmaed(hisi_hba, i); | |
481 | ||
482 | hisi_hba->scan_finished = 1; | |
483 | } | |
484 | ||
485 | static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time) | |
486 | { | |
487 | struct hisi_hba *hisi_hba = shost_priv(shost); | |
488 | struct sas_ha_struct *sha = &hisi_hba->sha; | |
489 | ||
490 | if (hisi_hba->scan_finished == 0) | |
491 | return 0; | |
492 | ||
493 | sas_drain_work(sha); | |
494 | return 1; | |
495 | } | |
496 | ||
66139921 JG |
497 | static void hisi_sas_phyup_work(struct work_struct *work) |
498 | { | |
499 | struct hisi_sas_phy *phy = | |
500 | container_of(work, struct hisi_sas_phy, phyup_ws); | |
501 | struct hisi_hba *hisi_hba = phy->hisi_hba; | |
502 | struct asd_sas_phy *sas_phy = &phy->sas_phy; | |
503 | int phy_no = sas_phy->id; | |
504 | ||
505 | hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */ | |
506 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); | |
507 | } | |
976867e6 JG |
508 | |
509 | static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no) | |
510 | { | |
511 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; | |
512 | struct asd_sas_phy *sas_phy = &phy->sas_phy; | |
513 | ||
514 | phy->hisi_hba = hisi_hba; | |
515 | phy->port = NULL; | |
516 | init_timer(&phy->timer); | |
517 | sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0; | |
518 | sas_phy->class = SAS; | |
519 | sas_phy->iproto = SAS_PROTOCOL_ALL; | |
520 | sas_phy->tproto = 0; | |
521 | sas_phy->type = PHY_TYPE_PHYSICAL; | |
522 | sas_phy->role = PHY_ROLE_INITIATOR; | |
523 | sas_phy->oob_mode = OOB_NOT_CONNECTED; | |
524 | sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN; | |
525 | sas_phy->id = phy_no; | |
526 | sas_phy->sas_addr = &hisi_hba->sas_addr[0]; | |
527 | sas_phy->frame_rcvd = &phy->frame_rcvd[0]; | |
528 | sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata; | |
529 | sas_phy->lldd_phy = phy; | |
66139921 JG |
530 | |
531 | INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work); | |
976867e6 JG |
532 | } |
533 | ||
184a4635 JG |
534 | static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy) |
535 | { | |
536 | struct sas_ha_struct *sas_ha = sas_phy->ha; | |
537 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; | |
538 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; | |
539 | struct asd_sas_port *sas_port = sas_phy->port; | |
540 | struct hisi_sas_port *port = &hisi_hba->port[sas_phy->id]; | |
541 | unsigned long flags; | |
542 | ||
543 | if (!sas_port) | |
544 | return; | |
545 | ||
546 | spin_lock_irqsave(&hisi_hba->lock, flags); | |
547 | port->port_attached = 1; | |
548 | port->id = phy->port_id; | |
549 | phy->port = port; | |
550 | sas_port->lldd_port = port; | |
551 | spin_unlock_irqrestore(&hisi_hba->lock, flags); | |
552 | } | |
553 | ||
554 | static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no, | |
555 | struct domain_device *device) | |
556 | { | |
557 | struct hisi_sas_phy *phy; | |
558 | struct hisi_sas_port *port; | |
559 | struct hisi_sas_slot *slot, *slot2; | |
560 | struct device *dev = &hisi_hba->pdev->dev; | |
561 | ||
562 | phy = &hisi_hba->phy[phy_no]; | |
563 | port = phy->port; | |
564 | if (!port) | |
565 | return; | |
566 | ||
567 | list_for_each_entry_safe(slot, slot2, &port->list, entry) { | |
568 | struct sas_task *task; | |
569 | ||
570 | task = slot->task; | |
571 | if (device && task->dev != device) | |
572 | continue; | |
573 | ||
574 | dev_info(dev, "Release slot [%d:%d], task [%p]:\n", | |
575 | slot->dlvry_queue, slot->dlvry_queue_slot, task); | |
576 | hisi_hba->hw->slot_complete(hisi_hba, slot, 1); | |
577 | } | |
578 | } | |
579 | ||
580 | static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy) | |
581 | { | |
582 | struct domain_device *device; | |
583 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; | |
584 | struct asd_sas_port *sas_port = sas_phy->port; | |
585 | ||
586 | list_for_each_entry(device, &sas_port->dev_list, dev_list_node) | |
587 | hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device); | |
588 | } | |
589 | ||
590 | static void hisi_sas_release_task(struct hisi_hba *hisi_hba, | |
591 | struct domain_device *device) | |
592 | { | |
593 | struct asd_sas_port *port = device->port; | |
594 | struct asd_sas_phy *sas_phy; | |
595 | ||
596 | list_for_each_entry(sas_phy, &port->phy_list, port_phy_el) | |
597 | hisi_sas_do_release_task(hisi_hba, sas_phy->id, device); | |
598 | } | |
599 | ||
abda97c2 JG |
600 | static void hisi_sas_dev_gone(struct domain_device *device) |
601 | { | |
602 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
603 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); | |
604 | struct device *dev = &hisi_hba->pdev->dev; | |
605 | u64 dev_id = sas_dev->device_id; | |
606 | ||
607 | dev_info(dev, "found dev[%lld:%x] is gone\n", | |
608 | sas_dev->device_id, sas_dev->dev_type); | |
609 | ||
40f2702b JG |
610 | hisi_sas_internal_task_abort(hisi_hba, device, |
611 | HISI_SAS_INT_ABT_DEV, 0); | |
612 | ||
abda97c2 JG |
613 | hisi_hba->hw->free_device(hisi_hba, sas_dev); |
614 | device->lldd_dev = NULL; | |
615 | memset(sas_dev, 0, sizeof(*sas_dev)); | |
616 | sas_dev->device_id = dev_id; | |
617 | sas_dev->dev_type = SAS_PHY_UNUSED; | |
618 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; | |
619 | } | |
42e7a693 JG |
620 | |
621 | static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags) | |
622 | { | |
623 | return hisi_sas_task_exec(task, gfp_flags, 0, NULL); | |
624 | } | |
625 | ||
e4189d53 JG |
626 | static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func, |
627 | void *funcdata) | |
628 | { | |
629 | struct sas_ha_struct *sas_ha = sas_phy->ha; | |
630 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; | |
631 | int phy_no = sas_phy->id; | |
632 | ||
633 | switch (func) { | |
634 | case PHY_FUNC_HARD_RESET: | |
635 | hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); | |
636 | break; | |
637 | ||
638 | case PHY_FUNC_LINK_RESET: | |
639 | hisi_hba->hw->phy_enable(hisi_hba, phy_no); | |
640 | hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); | |
641 | break; | |
642 | ||
643 | case PHY_FUNC_DISABLE: | |
644 | hisi_hba->hw->phy_disable(hisi_hba, phy_no); | |
645 | break; | |
646 | ||
647 | case PHY_FUNC_SET_LINK_RATE: | |
648 | case PHY_FUNC_RELEASE_SPINUP_HOLD: | |
649 | default: | |
650 | return -EOPNOTSUPP; | |
651 | } | |
652 | return 0; | |
653 | } | |
184a4635 | 654 | |
0efff300 JG |
655 | static void hisi_sas_task_done(struct sas_task *task) |
656 | { | |
657 | if (!del_timer(&task->slow_task->timer)) | |
658 | return; | |
659 | complete(&task->slow_task->completion); | |
660 | } | |
661 | ||
662 | static void hisi_sas_tmf_timedout(unsigned long data) | |
663 | { | |
664 | struct sas_task *task = (struct sas_task *)data; | |
665 | ||
666 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; | |
667 | complete(&task->slow_task->completion); | |
668 | } | |
669 | ||
670 | #define TASK_TIMEOUT 20 | |
671 | #define TASK_RETRY 3 | |
672 | static int hisi_sas_exec_internal_tmf_task(struct domain_device *device, | |
673 | void *parameter, u32 para_len, | |
674 | struct hisi_sas_tmf_task *tmf) | |
675 | { | |
676 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
677 | struct hisi_hba *hisi_hba = sas_dev->hisi_hba; | |
678 | struct device *dev = &hisi_hba->pdev->dev; | |
679 | struct sas_task *task; | |
680 | int res, retry; | |
681 | ||
682 | for (retry = 0; retry < TASK_RETRY; retry++) { | |
683 | task = sas_alloc_slow_task(GFP_KERNEL); | |
684 | if (!task) | |
685 | return -ENOMEM; | |
686 | ||
687 | task->dev = device; | |
688 | task->task_proto = device->tproto; | |
689 | ||
690 | memcpy(&task->ssp_task, parameter, para_len); | |
691 | task->task_done = hisi_sas_task_done; | |
692 | ||
693 | task->slow_task->timer.data = (unsigned long) task; | |
694 | task->slow_task->timer.function = hisi_sas_tmf_timedout; | |
695 | task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ; | |
696 | add_timer(&task->slow_task->timer); | |
697 | ||
698 | res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf); | |
699 | ||
700 | if (res) { | |
701 | del_timer(&task->slow_task->timer); | |
702 | dev_err(dev, "abort tmf: executing internal task failed: %d\n", | |
703 | res); | |
704 | goto ex_err; | |
705 | } | |
706 | ||
707 | wait_for_completion(&task->slow_task->completion); | |
708 | res = TMF_RESP_FUNC_FAILED; | |
709 | /* Even TMF timed out, return direct. */ | |
710 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | |
711 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | |
712 | dev_err(dev, "abort tmf: TMF task[%d] timeout\n", | |
713 | tmf->tag_of_task_to_be_managed); | |
714 | if (task->lldd_task) { | |
715 | struct hisi_sas_slot *slot = | |
716 | task->lldd_task; | |
717 | ||
718 | hisi_sas_slot_task_free(hisi_hba, | |
719 | task, slot); | |
720 | } | |
721 | ||
722 | goto ex_err; | |
723 | } | |
724 | } | |
725 | ||
726 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
1af1b808 | 727 | task->task_status.stat == TMF_RESP_FUNC_COMPLETE) { |
0efff300 JG |
728 | res = TMF_RESP_FUNC_COMPLETE; |
729 | break; | |
730 | } | |
731 | ||
4ffde482 JG |
732 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
733 | task->task_status.stat == TMF_RESP_FUNC_SUCC) { | |
734 | res = TMF_RESP_FUNC_SUCC; | |
735 | break; | |
736 | } | |
737 | ||
0efff300 JG |
738 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
739 | task->task_status.stat == SAS_DATA_UNDERRUN) { | |
740 | /* no error, but return the number of bytes of | |
741 | * underrun | |
742 | */ | |
743 | dev_warn(dev, "abort tmf: task to dev %016llx " | |
744 | "resp: 0x%x sts 0x%x underrun\n", | |
745 | SAS_ADDR(device->sas_addr), | |
746 | task->task_status.resp, | |
747 | task->task_status.stat); | |
748 | res = task->task_status.residual; | |
749 | break; | |
750 | } | |
751 | ||
752 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
753 | task->task_status.stat == SAS_DATA_OVERRUN) { | |
754 | dev_warn(dev, "abort tmf: blocked task error\n"); | |
755 | res = -EMSGSIZE; | |
756 | break; | |
757 | } | |
758 | ||
759 | dev_warn(dev, "abort tmf: task to dev " | |
760 | "%016llx resp: 0x%x status 0x%x\n", | |
761 | SAS_ADDR(device->sas_addr), task->task_status.resp, | |
762 | task->task_status.stat); | |
763 | sas_free_task(task); | |
764 | task = NULL; | |
765 | } | |
766 | ex_err: | |
767 | WARN_ON(retry == TASK_RETRY); | |
768 | sas_free_task(task); | |
769 | return res; | |
770 | } | |
771 | ||
772 | static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device, | |
773 | u8 *lun, struct hisi_sas_tmf_task *tmf) | |
774 | { | |
775 | struct sas_ssp_task ssp_task; | |
776 | ||
777 | if (!(device->tproto & SAS_PROTOCOL_SSP)) | |
778 | return TMF_RESP_FUNC_ESUPP; | |
779 | ||
780 | memcpy(ssp_task.LUN, lun, 8); | |
781 | ||
782 | return hisi_sas_exec_internal_tmf_task(device, &ssp_task, | |
783 | sizeof(ssp_task), tmf); | |
784 | } | |
785 | ||
786 | static int hisi_sas_abort_task(struct sas_task *task) | |
787 | { | |
788 | struct scsi_lun lun; | |
789 | struct hisi_sas_tmf_task tmf_task; | |
790 | struct domain_device *device = task->dev; | |
791 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
792 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); | |
793 | struct device *dev = &hisi_hba->pdev->dev; | |
794 | int rc = TMF_RESP_FUNC_FAILED; | |
795 | unsigned long flags; | |
796 | ||
797 | if (!sas_dev) { | |
798 | dev_warn(dev, "Device has been removed\n"); | |
799 | return TMF_RESP_FUNC_FAILED; | |
800 | } | |
801 | ||
802 | spin_lock_irqsave(&task->task_state_lock, flags); | |
803 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { | |
804 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
805 | rc = TMF_RESP_FUNC_COMPLETE; | |
806 | goto out; | |
807 | } | |
808 | ||
809 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
810 | sas_dev->dev_status = HISI_SAS_DEV_EH; | |
811 | if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { | |
812 | struct scsi_cmnd *cmnd = task->uldd_task; | |
813 | struct hisi_sas_slot *slot = task->lldd_task; | |
814 | u32 tag = slot->idx; | |
815 | ||
816 | int_to_scsilun(cmnd->device->lun, &lun); | |
817 | tmf_task.tmf = TMF_ABORT_TASK; | |
818 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); | |
819 | ||
820 | rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, | |
821 | &tmf_task); | |
822 | ||
823 | /* if successful, clear the task and callback forwards.*/ | |
824 | if (rc == TMF_RESP_FUNC_COMPLETE) { | |
825 | if (task->lldd_task) { | |
826 | struct hisi_sas_slot *slot; | |
827 | ||
828 | slot = &hisi_hba->slot_info | |
829 | [tmf_task.tag_of_task_to_be_managed]; | |
830 | spin_lock_irqsave(&hisi_hba->lock, flags); | |
831 | hisi_hba->hw->slot_complete(hisi_hba, slot, 1); | |
832 | spin_unlock_irqrestore(&hisi_hba->lock, flags); | |
833 | } | |
834 | } | |
835 | ||
dc8a49ca JG |
836 | hisi_sas_internal_task_abort(hisi_hba, device, |
837 | HISI_SAS_INT_ABT_CMD, tag); | |
0efff300 JG |
838 | } else if (task->task_proto & SAS_PROTOCOL_SATA || |
839 | task->task_proto & SAS_PROTOCOL_STP) { | |
840 | if (task->dev->dev_type == SAS_SATA_DEV) { | |
dc8a49ca JG |
841 | hisi_sas_internal_task_abort(hisi_hba, device, |
842 | HISI_SAS_INT_ABT_DEV, 0); | |
0efff300 | 843 | rc = TMF_RESP_FUNC_COMPLETE; |
0efff300 | 844 | } |
dc8a49ca JG |
845 | } else if (task->task_proto & SAS_PROTOCOL_SMP) { |
846 | /* SMP */ | |
847 | struct hisi_sas_slot *slot = task->lldd_task; | |
848 | u32 tag = slot->idx; | |
0efff300 | 849 | |
dc8a49ca JG |
850 | hisi_sas_internal_task_abort(hisi_hba, device, |
851 | HISI_SAS_INT_ABT_CMD, tag); | |
0efff300 JG |
852 | } |
853 | ||
854 | out: | |
855 | if (rc != TMF_RESP_FUNC_COMPLETE) | |
856 | dev_notice(dev, "abort task: rc=%d\n", rc); | |
857 | return rc; | |
858 | } | |
859 | ||
860 | static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun) | |
861 | { | |
862 | struct hisi_sas_tmf_task tmf_task; | |
863 | int rc = TMF_RESP_FUNC_FAILED; | |
864 | ||
865 | tmf_task.tmf = TMF_ABORT_TASK_SET; | |
866 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); | |
867 | ||
868 | return rc; | |
869 | } | |
870 | ||
871 | static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun) | |
872 | { | |
873 | int rc = TMF_RESP_FUNC_FAILED; | |
874 | struct hisi_sas_tmf_task tmf_task; | |
875 | ||
876 | tmf_task.tmf = TMF_CLEAR_ACA; | |
877 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); | |
878 | ||
879 | return rc; | |
880 | } | |
881 | ||
882 | static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device) | |
883 | { | |
884 | struct sas_phy *phy = sas_get_local_phy(device); | |
885 | int rc, reset_type = (device->dev_type == SAS_SATA_DEV || | |
886 | (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1; | |
887 | rc = sas_phy_reset(phy, reset_type); | |
888 | sas_put_local_phy(phy); | |
889 | msleep(2000); | |
890 | return rc; | |
891 | } | |
892 | ||
893 | static int hisi_sas_I_T_nexus_reset(struct domain_device *device) | |
894 | { | |
895 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
896 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); | |
897 | unsigned long flags; | |
898 | int rc = TMF_RESP_FUNC_FAILED; | |
899 | ||
900 | if (sas_dev->dev_status != HISI_SAS_DEV_EH) | |
901 | return TMF_RESP_FUNC_FAILED; | |
902 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; | |
903 | ||
904 | rc = hisi_sas_debug_I_T_nexus_reset(device); | |
905 | ||
906 | spin_lock_irqsave(&hisi_hba->lock, flags); | |
907 | hisi_sas_release_task(hisi_hba, device); | |
908 | spin_unlock_irqrestore(&hisi_hba->lock, flags); | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun) | |
914 | { | |
915 | struct hisi_sas_tmf_task tmf_task; | |
916 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
917 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); | |
918 | struct device *dev = &hisi_hba->pdev->dev; | |
919 | unsigned long flags; | |
920 | int rc = TMF_RESP_FUNC_FAILED; | |
921 | ||
922 | tmf_task.tmf = TMF_LU_RESET; | |
923 | sas_dev->dev_status = HISI_SAS_DEV_EH; | |
924 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); | |
925 | if (rc == TMF_RESP_FUNC_COMPLETE) { | |
926 | spin_lock_irqsave(&hisi_hba->lock, flags); | |
927 | hisi_sas_release_task(hisi_hba, device); | |
928 | spin_unlock_irqrestore(&hisi_hba->lock, flags); | |
929 | } | |
930 | ||
931 | /* If failed, fall-through I_T_Nexus reset */ | |
932 | dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n", | |
933 | sas_dev->device_id, rc); | |
934 | return rc; | |
935 | } | |
936 | ||
937 | static int hisi_sas_query_task(struct sas_task *task) | |
938 | { | |
939 | struct scsi_lun lun; | |
940 | struct hisi_sas_tmf_task tmf_task; | |
941 | int rc = TMF_RESP_FUNC_FAILED; | |
942 | ||
943 | if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { | |
944 | struct scsi_cmnd *cmnd = task->uldd_task; | |
945 | struct domain_device *device = task->dev; | |
946 | struct hisi_sas_slot *slot = task->lldd_task; | |
947 | u32 tag = slot->idx; | |
948 | ||
949 | int_to_scsilun(cmnd->device->lun, &lun); | |
950 | tmf_task.tmf = TMF_QUERY_TASK; | |
951 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); | |
952 | ||
953 | rc = hisi_sas_debug_issue_ssp_tmf(device, | |
954 | lun.scsi_lun, | |
955 | &tmf_task); | |
956 | switch (rc) { | |
957 | /* The task is still in Lun, release it then */ | |
958 | case TMF_RESP_FUNC_SUCC: | |
959 | /* The task is not in Lun or failed, reset the phy */ | |
960 | case TMF_RESP_FUNC_FAILED: | |
961 | case TMF_RESP_FUNC_COMPLETE: | |
962 | break; | |
963 | } | |
964 | } | |
965 | return rc; | |
966 | } | |
967 | ||
441c2740 JG |
968 | static int |
969 | hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id, | |
970 | struct sas_task *task, int abort_flag, | |
971 | int task_tag) | |
972 | { | |
973 | struct domain_device *device = task->dev; | |
974 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
975 | struct device *dev = &hisi_hba->pdev->dev; | |
976 | struct hisi_sas_port *port; | |
977 | struct hisi_sas_slot *slot; | |
978 | struct hisi_sas_cmd_hdr *cmd_hdr_base; | |
979 | int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; | |
980 | ||
981 | if (!device->port) | |
982 | return -1; | |
983 | ||
984 | port = device->port->lldd_port; | |
985 | ||
986 | /* simply get a slot and send abort command */ | |
987 | rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx); | |
988 | if (rc) | |
989 | goto err_out; | |
990 | rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue, | |
991 | &dlvry_queue_slot); | |
992 | if (rc) | |
993 | goto err_out_tag; | |
994 | ||
995 | slot = &hisi_hba->slot_info[slot_idx]; | |
996 | memset(slot, 0, sizeof(struct hisi_sas_slot)); | |
997 | ||
998 | slot->idx = slot_idx; | |
999 | slot->n_elem = n_elem; | |
1000 | slot->dlvry_queue = dlvry_queue; | |
1001 | slot->dlvry_queue_slot = dlvry_queue_slot; | |
1002 | cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; | |
1003 | slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; | |
1004 | slot->task = task; | |
1005 | slot->port = port; | |
1006 | task->lldd_task = slot; | |
1007 | ||
1008 | memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); | |
1009 | ||
1010 | rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id, | |
1011 | abort_flag, task_tag); | |
1012 | if (rc) | |
1013 | goto err_out_tag; | |
1014 | ||
1015 | /* Port structure is static for the HBA, so | |
1016 | * even if the port is deformed it is ok | |
1017 | * to reference. | |
1018 | */ | |
1019 | list_add_tail(&slot->entry, &port->list); | |
1020 | spin_lock(&task->task_state_lock); | |
1021 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; | |
1022 | spin_unlock(&task->task_state_lock); | |
1023 | ||
1024 | hisi_hba->slot_prep = slot; | |
1025 | ||
1026 | sas_dev->running_req++; | |
1027 | /* send abort command to our chip */ | |
1028 | hisi_hba->hw->start_delivery(hisi_hba); | |
1029 | ||
1030 | return 0; | |
1031 | ||
1032 | err_out_tag: | |
1033 | hisi_sas_slot_index_free(hisi_hba, slot_idx); | |
1034 | err_out: | |
1035 | dev_err(dev, "internal abort task prep: failed[%d]!\n", rc); | |
1036 | ||
1037 | return rc; | |
1038 | } | |
1039 | ||
1040 | /** | |
1041 | * hisi_sas_internal_task_abort -- execute an internal | |
1042 | * abort command for single IO command or a device | |
1043 | * @hisi_hba: host controller struct | |
1044 | * @device: domain device | |
1045 | * @abort_flag: mode of operation, device or single IO | |
1046 | * @tag: tag of IO to be aborted (only relevant to single | |
1047 | * IO mode) | |
1048 | */ | |
1049 | static int | |
1050 | hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba, | |
1051 | struct domain_device *device, | |
1052 | int abort_flag, int tag) | |
1053 | { | |
1054 | struct sas_task *task; | |
1055 | struct hisi_sas_device *sas_dev = device->lldd_dev; | |
1056 | struct device *dev = &hisi_hba->pdev->dev; | |
1057 | int res; | |
1058 | unsigned long flags; | |
1059 | ||
1060 | if (!hisi_hba->hw->prep_abort) | |
1061 | return -EOPNOTSUPP; | |
1062 | ||
1063 | task = sas_alloc_slow_task(GFP_KERNEL); | |
1064 | if (!task) | |
1065 | return -ENOMEM; | |
1066 | ||
1067 | task->dev = device; | |
1068 | task->task_proto = device->tproto; | |
1069 | task->task_done = hisi_sas_task_done; | |
1070 | task->slow_task->timer.data = (unsigned long)task; | |
1071 | task->slow_task->timer.function = hisi_sas_tmf_timedout; | |
1072 | task->slow_task->timer.expires = jiffies + 20*HZ; | |
1073 | add_timer(&task->slow_task->timer); | |
1074 | ||
1075 | /* Lock as we are alloc'ing a slot, which cannot be interrupted */ | |
1076 | spin_lock_irqsave(&hisi_hba->lock, flags); | |
1077 | res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id, | |
1078 | task, abort_flag, tag); | |
1079 | spin_unlock_irqrestore(&hisi_hba->lock, flags); | |
1080 | if (res) { | |
1081 | del_timer(&task->slow_task->timer); | |
1082 | dev_err(dev, "internal task abort: executing internal task failed: %d\n", | |
1083 | res); | |
1084 | goto exit; | |
1085 | } | |
1086 | wait_for_completion(&task->slow_task->completion); | |
1087 | res = TMF_RESP_FUNC_FAILED; | |
1088 | ||
1089 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
1090 | task->task_status.stat == TMF_RESP_FUNC_COMPLETE) { | |
1091 | res = TMF_RESP_FUNC_COMPLETE; | |
1092 | goto exit; | |
1093 | } | |
1094 | ||
1095 | /* TMF timed out, return direct. */ | |
1096 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | |
1097 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | |
1098 | dev_err(dev, "internal task abort: timeout.\n"); | |
1099 | if (task->lldd_task) { | |
1100 | struct hisi_sas_slot *slot = task->lldd_task; | |
1101 | ||
1102 | hisi_sas_slot_task_free(hisi_hba, task, slot); | |
1103 | } | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | exit: | |
1108 | dev_info(dev, "internal task abort: task to dev %016llx task=%p " | |
1109 | "resp: 0x%x sts 0x%x\n", | |
1110 | SAS_ADDR(device->sas_addr), | |
1111 | task, | |
1112 | task->task_status.resp, /* 0 is complete, -1 is undelivered */ | |
1113 | task->task_status.stat); | |
1114 | sas_free_task(task); | |
1115 | ||
1116 | return res; | |
1117 | } | |
1118 | ||
184a4635 JG |
1119 | static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy) |
1120 | { | |
1121 | hisi_sas_port_notify_formed(sas_phy); | |
1122 | } | |
1123 | ||
1124 | static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy) | |
1125 | { | |
1126 | hisi_sas_port_notify_deformed(sas_phy); | |
1127 | } | |
1128 | ||
1129 | static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy) | |
1130 | { | |
1131 | phy->phy_attached = 0; | |
1132 | phy->phy_type = 0; | |
1133 | phy->port = NULL; | |
1134 | } | |
1135 | ||
1136 | void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy) | |
1137 | { | |
1138 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; | |
1139 | struct asd_sas_phy *sas_phy = &phy->sas_phy; | |
1140 | struct sas_ha_struct *sas_ha = &hisi_hba->sha; | |
1141 | ||
1142 | if (rdy) { | |
1143 | /* Phy down but ready */ | |
1144 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); | |
1145 | hisi_sas_port_notify_formed(sas_phy); | |
1146 | } else { | |
1147 | struct hisi_sas_port *port = phy->port; | |
1148 | ||
1149 | /* Phy down and not ready */ | |
1150 | sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL); | |
1151 | sas_phy_disconnected(sas_phy); | |
1152 | ||
1153 | if (port) { | |
1154 | if (phy->phy_type & PORT_TYPE_SAS) { | |
1155 | int port_id = port->id; | |
1156 | ||
1157 | if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba, | |
1158 | port_id)) | |
1159 | port->port_attached = 0; | |
1160 | } else if (phy->phy_type & PORT_TYPE_SATA) | |
1161 | port->port_attached = 0; | |
1162 | } | |
1163 | hisi_sas_phy_disconnected(phy); | |
1164 | } | |
1165 | } | |
1166 | EXPORT_SYMBOL_GPL(hisi_sas_phy_down); | |
1167 | ||
e8899fad JG |
1168 | static struct scsi_transport_template *hisi_sas_stt; |
1169 | ||
7eb7869f JG |
1170 | static struct scsi_host_template hisi_sas_sht = { |
1171 | .module = THIS_MODULE, | |
1172 | .name = DRV_NAME, | |
1173 | .queuecommand = sas_queuecommand, | |
1174 | .target_alloc = sas_target_alloc, | |
31eec8a6 | 1175 | .slave_configure = hisi_sas_slave_configure, |
701f75ec JG |
1176 | .scan_finished = hisi_sas_scan_finished, |
1177 | .scan_start = hisi_sas_scan_start, | |
7eb7869f JG |
1178 | .change_queue_depth = sas_change_queue_depth, |
1179 | .bios_param = sas_bios_param, | |
1180 | .can_queue = 1, | |
1181 | .this_id = -1, | |
1182 | .sg_tablesize = SG_ALL, | |
1183 | .max_sectors = SCSI_DEFAULT_MAX_SECTORS, | |
1184 | .use_clustering = ENABLE_CLUSTERING, | |
1185 | .eh_device_reset_handler = sas_eh_device_reset_handler, | |
1186 | .eh_bus_reset_handler = sas_eh_bus_reset_handler, | |
1187 | .target_destroy = sas_target_destroy, | |
1188 | .ioctl = sas_ioctl, | |
1189 | }; | |
1190 | ||
e8899fad | 1191 | static struct sas_domain_function_template hisi_sas_transport_ops = { |
abda97c2 JG |
1192 | .lldd_dev_found = hisi_sas_dev_found, |
1193 | .lldd_dev_gone = hisi_sas_dev_gone, | |
42e7a693 | 1194 | .lldd_execute_task = hisi_sas_queue_command, |
e4189d53 | 1195 | .lldd_control_phy = hisi_sas_control_phy, |
0efff300 JG |
1196 | .lldd_abort_task = hisi_sas_abort_task, |
1197 | .lldd_abort_task_set = hisi_sas_abort_task_set, | |
1198 | .lldd_clear_aca = hisi_sas_clear_aca, | |
1199 | .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset, | |
1200 | .lldd_lu_reset = hisi_sas_lu_reset, | |
1201 | .lldd_query_task = hisi_sas_query_task, | |
184a4635 JG |
1202 | .lldd_port_formed = hisi_sas_port_formed, |
1203 | .lldd_port_deformed = hisi_sas_port_deformed, | |
e8899fad JG |
1204 | }; |
1205 | ||
6be6de18 JG |
1206 | static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost) |
1207 | { | |
6be6de18 JG |
1208 | struct platform_device *pdev = hisi_hba->pdev; |
1209 | struct device *dev = &pdev->dev; | |
a8d547bd | 1210 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
6be6de18 | 1211 | |
fa42d80d | 1212 | spin_lock_init(&hisi_hba->lock); |
976867e6 JG |
1213 | for (i = 0; i < hisi_hba->n_phy; i++) { |
1214 | hisi_sas_phy_init(hisi_hba, i); | |
1215 | hisi_hba->port[i].port_attached = 0; | |
1216 | hisi_hba->port[i].id = -1; | |
1217 | INIT_LIST_HEAD(&hisi_hba->port[i].list); | |
1218 | } | |
1219 | ||
af740dbe JG |
1220 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
1221 | hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED; | |
1222 | hisi_hba->devices[i].device_id = i; | |
1223 | hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL; | |
1224 | } | |
1225 | ||
6be6de18 | 1226 | for (i = 0; i < hisi_hba->queue_count; i++) { |
9101a079 | 1227 | struct hisi_sas_cq *cq = &hisi_hba->cq[i]; |
4fde02ad | 1228 | struct hisi_sas_dq *dq = &hisi_hba->dq[i]; |
9101a079 JG |
1229 | |
1230 | /* Completion queue structure */ | |
1231 | cq->id = i; | |
1232 | cq->hisi_hba = hisi_hba; | |
1233 | ||
4fde02ad JG |
1234 | /* Delivery queue structure */ |
1235 | dq->id = i; | |
1236 | dq->hisi_hba = hisi_hba; | |
1237 | ||
6be6de18 JG |
1238 | /* Delivery queue */ |
1239 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; | |
1240 | hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s, | |
1241 | &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL); | |
1242 | if (!hisi_hba->cmd_hdr[i]) | |
1243 | goto err_out; | |
1244 | memset(hisi_hba->cmd_hdr[i], 0, s); | |
1245 | ||
1246 | /* Completion queue */ | |
1247 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; | |
1248 | hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s, | |
1249 | &hisi_hba->complete_hdr_dma[i], GFP_KERNEL); | |
1250 | if (!hisi_hba->complete_hdr[i]) | |
1251 | goto err_out; | |
1252 | memset(hisi_hba->complete_hdr[i], 0, s); | |
1253 | } | |
1254 | ||
1255 | s = HISI_SAS_STATUS_BUF_SZ; | |
1256 | hisi_hba->status_buffer_pool = dma_pool_create("status_buffer", | |
1257 | dev, s, 16, 0); | |
1258 | if (!hisi_hba->status_buffer_pool) | |
1259 | goto err_out; | |
1260 | ||
1261 | s = HISI_SAS_COMMAND_TABLE_SZ; | |
1262 | hisi_hba->command_table_pool = dma_pool_create("command_table", | |
1263 | dev, s, 16, 0); | |
1264 | if (!hisi_hba->command_table_pool) | |
1265 | goto err_out; | |
1266 | ||
1267 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); | |
1268 | hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma, | |
1269 | GFP_KERNEL); | |
1270 | if (!hisi_hba->itct) | |
1271 | goto err_out; | |
1272 | ||
1273 | memset(hisi_hba->itct, 0, s); | |
1274 | ||
a8d547bd | 1275 | hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries, |
6be6de18 JG |
1276 | sizeof(struct hisi_sas_slot), |
1277 | GFP_KERNEL); | |
1278 | if (!hisi_hba->slot_info) | |
1279 | goto err_out; | |
1280 | ||
a8d547bd | 1281 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
6be6de18 JG |
1282 | hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma, |
1283 | GFP_KERNEL); | |
1284 | if (!hisi_hba->iost) | |
1285 | goto err_out; | |
1286 | ||
1287 | memset(hisi_hba->iost, 0, s); | |
1288 | ||
a8d547bd | 1289 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
6be6de18 JG |
1290 | hisi_hba->breakpoint = dma_alloc_coherent(dev, s, |
1291 | &hisi_hba->breakpoint_dma, GFP_KERNEL); | |
1292 | if (!hisi_hba->breakpoint) | |
1293 | goto err_out; | |
1294 | ||
1295 | memset(hisi_hba->breakpoint, 0, s); | |
1296 | ||
a8d547bd | 1297 | hisi_hba->slot_index_count = max_command_entries; |
433f5696 | 1298 | s = hisi_hba->slot_index_count / BITS_PER_BYTE; |
257efd1f JG |
1299 | hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL); |
1300 | if (!hisi_hba->slot_index_tags) | |
1301 | goto err_out; | |
1302 | ||
6be6de18 JG |
1303 | hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev, |
1304 | sizeof(struct hisi_sas_sge_page), 16, 0); | |
1305 | if (!hisi_hba->sge_page_pool) | |
1306 | goto err_out; | |
1307 | ||
1308 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; | |
1309 | hisi_hba->initial_fis = dma_alloc_coherent(dev, s, | |
1310 | &hisi_hba->initial_fis_dma, GFP_KERNEL); | |
1311 | if (!hisi_hba->initial_fis) | |
1312 | goto err_out; | |
1313 | memset(hisi_hba->initial_fis, 0, s); | |
1314 | ||
a8d547bd | 1315 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
6be6de18 JG |
1316 | hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s, |
1317 | &hisi_hba->sata_breakpoint_dma, GFP_KERNEL); | |
1318 | if (!hisi_hba->sata_breakpoint) | |
1319 | goto err_out; | |
1320 | memset(hisi_hba->sata_breakpoint, 0, s); | |
1321 | ||
257efd1f JG |
1322 | hisi_sas_slot_index_init(hisi_hba); |
1323 | ||
7e9080e1 JG |
1324 | hisi_hba->wq = create_singlethread_workqueue(dev_name(dev)); |
1325 | if (!hisi_hba->wq) { | |
1326 | dev_err(dev, "sas_alloc: failed to create workqueue\n"); | |
1327 | goto err_out; | |
1328 | } | |
1329 | ||
6be6de18 JG |
1330 | return 0; |
1331 | err_out: | |
1332 | return -ENOMEM; | |
1333 | } | |
1334 | ||
89d53322 JG |
1335 | static void hisi_sas_free(struct hisi_hba *hisi_hba) |
1336 | { | |
1337 | struct device *dev = &hisi_hba->pdev->dev; | |
a8d547bd | 1338 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
89d53322 JG |
1339 | |
1340 | for (i = 0; i < hisi_hba->queue_count; i++) { | |
1341 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; | |
1342 | if (hisi_hba->cmd_hdr[i]) | |
1343 | dma_free_coherent(dev, s, | |
1344 | hisi_hba->cmd_hdr[i], | |
1345 | hisi_hba->cmd_hdr_dma[i]); | |
1346 | ||
1347 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; | |
1348 | if (hisi_hba->complete_hdr[i]) | |
1349 | dma_free_coherent(dev, s, | |
1350 | hisi_hba->complete_hdr[i], | |
1351 | hisi_hba->complete_hdr_dma[i]); | |
1352 | } | |
1353 | ||
1354 | dma_pool_destroy(hisi_hba->status_buffer_pool); | |
1355 | dma_pool_destroy(hisi_hba->command_table_pool); | |
1356 | dma_pool_destroy(hisi_hba->sge_page_pool); | |
1357 | ||
1358 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); | |
1359 | if (hisi_hba->itct) | |
1360 | dma_free_coherent(dev, s, | |
1361 | hisi_hba->itct, hisi_hba->itct_dma); | |
1362 | ||
a8d547bd | 1363 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
89d53322 JG |
1364 | if (hisi_hba->iost) |
1365 | dma_free_coherent(dev, s, | |
1366 | hisi_hba->iost, hisi_hba->iost_dma); | |
1367 | ||
a8d547bd | 1368 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
89d53322 JG |
1369 | if (hisi_hba->breakpoint) |
1370 | dma_free_coherent(dev, s, | |
1371 | hisi_hba->breakpoint, | |
1372 | hisi_hba->breakpoint_dma); | |
1373 | ||
1374 | ||
1375 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; | |
1376 | if (hisi_hba->initial_fis) | |
1377 | dma_free_coherent(dev, s, | |
1378 | hisi_hba->initial_fis, | |
1379 | hisi_hba->initial_fis_dma); | |
1380 | ||
a8d547bd | 1381 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
89d53322 JG |
1382 | if (hisi_hba->sata_breakpoint) |
1383 | dma_free_coherent(dev, s, | |
1384 | hisi_hba->sata_breakpoint, | |
1385 | hisi_hba->sata_breakpoint_dma); | |
1386 | ||
7e9080e1 JG |
1387 | if (hisi_hba->wq) |
1388 | destroy_workqueue(hisi_hba->wq); | |
89d53322 | 1389 | } |
6be6de18 | 1390 | |
7eb7869f JG |
1391 | static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, |
1392 | const struct hisi_sas_hw *hw) | |
1393 | { | |
e26b2f40 | 1394 | struct resource *res; |
7eb7869f JG |
1395 | struct Scsi_Host *shost; |
1396 | struct hisi_hba *hisi_hba; | |
1397 | struct device *dev = &pdev->dev; | |
e26b2f40 | 1398 | struct device_node *np = pdev->dev.of_node; |
7eb7869f JG |
1399 | |
1400 | shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba)); | |
1401 | if (!shost) | |
1402 | goto err_out; | |
1403 | hisi_hba = shost_priv(shost); | |
1404 | ||
1405 | hisi_hba->hw = hw; | |
1406 | hisi_hba->pdev = pdev; | |
1407 | hisi_hba->shost = shost; | |
1408 | SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; | |
1409 | ||
fa42d80d JG |
1410 | init_timer(&hisi_hba->timer); |
1411 | ||
4d558c77 JG |
1412 | if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr, |
1413 | SAS_ADDR_SIZE)) | |
e26b2f40 | 1414 | goto err_out; |
e26b2f40 | 1415 | |
4d558c77 JG |
1416 | if (np) { |
1417 | hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np, | |
1418 | "hisilicon,sas-syscon"); | |
1419 | if (IS_ERR(hisi_hba->ctrl)) | |
1420 | goto err_out; | |
e26b2f40 | 1421 | |
4d558c77 JG |
1422 | if (device_property_read_u32(dev, "ctrl-reset-reg", |
1423 | &hisi_hba->ctrl_reset_reg)) | |
1424 | goto err_out; | |
e26b2f40 | 1425 | |
4d558c77 JG |
1426 | if (device_property_read_u32(dev, "ctrl-reset-sts-reg", |
1427 | &hisi_hba->ctrl_reset_sts_reg)) | |
1428 | goto err_out; | |
e26b2f40 | 1429 | |
4d558c77 JG |
1430 | if (device_property_read_u32(dev, "ctrl-clock-ena-reg", |
1431 | &hisi_hba->ctrl_clock_ena_reg)) | |
1432 | goto err_out; | |
1433 | } | |
1434 | ||
1435 | if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) | |
e26b2f40 JG |
1436 | goto err_out; |
1437 | ||
4d558c77 JG |
1438 | if (device_property_read_u32(dev, "queue-count", |
1439 | &hisi_hba->queue_count)) | |
e26b2f40 JG |
1440 | goto err_out; |
1441 | ||
e26b2f40 JG |
1442 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
1443 | hisi_hba->regs = devm_ioremap_resource(dev, res); | |
1444 | if (IS_ERR(hisi_hba->regs)) | |
1445 | goto err_out; | |
1446 | ||
89d53322 JG |
1447 | if (hisi_sas_alloc(hisi_hba, shost)) { |
1448 | hisi_sas_free(hisi_hba); | |
6be6de18 | 1449 | goto err_out; |
89d53322 | 1450 | } |
6be6de18 | 1451 | |
7eb7869f JG |
1452 | return shost; |
1453 | err_out: | |
1454 | dev_err(dev, "shost alloc failed\n"); | |
1455 | return NULL; | |
1456 | } | |
1457 | ||
5d74242e JG |
1458 | static void hisi_sas_init_add(struct hisi_hba *hisi_hba) |
1459 | { | |
1460 | int i; | |
1461 | ||
1462 | for (i = 0; i < hisi_hba->n_phy; i++) | |
1463 | memcpy(&hisi_hba->phy[i].dev_sas_addr, | |
1464 | hisi_hba->sas_addr, | |
1465 | SAS_ADDR_SIZE); | |
1466 | } | |
1467 | ||
7eb7869f JG |
1468 | int hisi_sas_probe(struct platform_device *pdev, |
1469 | const struct hisi_sas_hw *hw) | |
1470 | { | |
1471 | struct Scsi_Host *shost; | |
1472 | struct hisi_hba *hisi_hba; | |
1473 | struct device *dev = &pdev->dev; | |
1474 | struct asd_sas_phy **arr_phy; | |
1475 | struct asd_sas_port **arr_port; | |
1476 | struct sas_ha_struct *sha; | |
1477 | int rc, phy_nr, port_nr, i; | |
1478 | ||
1479 | shost = hisi_sas_shost_alloc(pdev, hw); | |
1480 | if (!shost) { | |
1481 | rc = -ENOMEM; | |
1482 | goto err_out_ha; | |
1483 | } | |
1484 | ||
1485 | sha = SHOST_TO_SAS_HA(shost); | |
1486 | hisi_hba = shost_priv(shost); | |
1487 | platform_set_drvdata(pdev, sha); | |
50cb916f JG |
1488 | |
1489 | if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) && | |
1490 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) { | |
1491 | dev_err(dev, "No usable DMA addressing method\n"); | |
1492 | rc = -EIO; | |
1493 | goto err_out_ha; | |
1494 | } | |
1495 | ||
7eb7869f JG |
1496 | phy_nr = port_nr = hisi_hba->n_phy; |
1497 | ||
1498 | arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); | |
1499 | arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); | |
1500 | if (!arr_phy || !arr_port) | |
1501 | return -ENOMEM; | |
1502 | ||
1503 | sha->sas_phy = arr_phy; | |
1504 | sha->sas_port = arr_port; | |
1505 | sha->core.shost = shost; | |
1506 | sha->lldd_ha = hisi_hba; | |
1507 | ||
1508 | shost->transportt = hisi_sas_stt; | |
1509 | shost->max_id = HISI_SAS_MAX_DEVICES; | |
1510 | shost->max_lun = ~0; | |
1511 | shost->max_channel = 1; | |
1512 | shost->max_cmd_len = 16; | |
1513 | shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT); | |
a8d547bd JG |
1514 | shost->can_queue = hisi_hba->hw->max_command_entries; |
1515 | shost->cmd_per_lun = hisi_hba->hw->max_command_entries; | |
7eb7869f JG |
1516 | |
1517 | sha->sas_ha_name = DRV_NAME; | |
1518 | sha->dev = &hisi_hba->pdev->dev; | |
1519 | sha->lldd_module = THIS_MODULE; | |
1520 | sha->sas_addr = &hisi_hba->sas_addr[0]; | |
1521 | sha->num_phys = hisi_hba->n_phy; | |
1522 | sha->core.shost = hisi_hba->shost; | |
1523 | ||
1524 | for (i = 0; i < hisi_hba->n_phy; i++) { | |
1525 | sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; | |
1526 | sha->sas_port[i] = &hisi_hba->port[i].sas_port; | |
1527 | } | |
1528 | ||
5d74242e JG |
1529 | hisi_sas_init_add(hisi_hba); |
1530 | ||
8ff1d571 JG |
1531 | rc = hisi_hba->hw->hw_init(hisi_hba); |
1532 | if (rc) | |
1533 | goto err_out_ha; | |
1534 | ||
7eb7869f JG |
1535 | rc = scsi_add_host(shost, &pdev->dev); |
1536 | if (rc) | |
1537 | goto err_out_ha; | |
1538 | ||
1539 | rc = sas_register_ha(sha); | |
1540 | if (rc) | |
1541 | goto err_out_register_ha; | |
1542 | ||
1543 | scsi_scan_host(shost); | |
1544 | ||
1545 | return 0; | |
1546 | ||
1547 | err_out_register_ha: | |
1548 | scsi_remove_host(shost); | |
1549 | err_out_ha: | |
1550 | kfree(shost); | |
1551 | return rc; | |
1552 | } | |
1553 | EXPORT_SYMBOL_GPL(hisi_sas_probe); | |
1554 | ||
89d53322 JG |
1555 | int hisi_sas_remove(struct platform_device *pdev) |
1556 | { | |
1557 | struct sas_ha_struct *sha = platform_get_drvdata(pdev); | |
1558 | struct hisi_hba *hisi_hba = sha->lldd_ha; | |
1559 | ||
1560 | scsi_remove_host(sha->core.shost); | |
1561 | sas_unregister_ha(sha); | |
1562 | sas_remove_host(sha->core.shost); | |
1563 | ||
1564 | hisi_sas_free(hisi_hba); | |
1565 | return 0; | |
1566 | } | |
1567 | EXPORT_SYMBOL_GPL(hisi_sas_remove); | |
1568 | ||
e8899fad JG |
1569 | static __init int hisi_sas_init(void) |
1570 | { | |
1571 | pr_info("hisi_sas: driver version %s\n", DRV_VERSION); | |
1572 | ||
1573 | hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); | |
1574 | if (!hisi_sas_stt) | |
1575 | return -ENOMEM; | |
1576 | ||
1577 | return 0; | |
1578 | } | |
1579 | ||
1580 | static __exit void hisi_sas_exit(void) | |
1581 | { | |
1582 | sas_release_transport(hisi_sas_stt); | |
1583 | } | |
1584 | ||
1585 | module_init(hisi_sas_init); | |
1586 | module_exit(hisi_sas_exit); | |
1587 | ||
1588 | MODULE_VERSION(DRV_VERSION); | |
1589 | MODULE_LICENSE("GPL"); | |
1590 | MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); | |
1591 | MODULE_DESCRIPTION("HISILICON SAS controller driver"); | |
1592 | MODULE_ALIAS("platform:" DRV_NAME); |