blob: 7c1fb75d11e6b38f9e929bc676095129f99eb689 [file] [log] [blame]
John Garrye8899fa2015-11-18 00:50:30 +08001/*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12#include "hisi_sas.h"
13#define DRV_NAME "hisi_sas"
14
John Garry42e7a692015-11-18 00:50:49 +080015#define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
John Garrycac9b2a2016-02-25 17:42:11 +080018static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
John Garry441c2742016-08-24 19:05:47 +080020static int
21hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
Xiang Chen7c594f02017-03-23 01:25:20 +080024static int hisi_sas_softreset_ata_disk(struct domain_device *device);
John Garrycac9b2a2016-02-25 17:42:11 +080025
John Garry42e7a692015-11-18 00:50:49 +080026static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
27{
28 return device->port->ha->lldd_ha;
29}
30
John Garry2e244f02017-03-23 01:25:17 +080031struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
32{
33 return container_of(sas_port, struct hisi_sas_port, sas_port);
34}
35EXPORT_SYMBOL_GPL(to_hisi_sas_port);
36
John Garry257efd12015-11-18 00:50:36 +080037static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
38{
39 void *bitmap = hisi_hba->slot_index_tags;
40
41 clear_bit(slot_idx, bitmap);
42}
43
John Garry42e7a692015-11-18 00:50:49 +080044static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
45{
46 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
47}
48
49static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
50{
51 void *bitmap = hisi_hba->slot_index_tags;
52
53 set_bit(slot_idx, bitmap);
54}
55
56static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
57{
58 unsigned int index;
59 void *bitmap = hisi_hba->slot_index_tags;
60
61 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
62 if (index >= hisi_hba->slot_index_count)
63 return -SAS_QUEUE_FULL;
64 hisi_sas_slot_index_set(hisi_hba, index);
65 *slot_idx = index;
66 return 0;
67}
68
John Garry257efd12015-11-18 00:50:36 +080069static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
70{
71 int i;
72
73 for (i = 0; i < hisi_hba->slot_index_count; ++i)
74 hisi_sas_slot_index_clear(hisi_hba, i);
75}
John Garry27a3f222015-11-18 00:50:50 +080076
77void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
78 struct hisi_sas_slot *slot)
79{
80 struct device *dev = &hisi_hba->pdev->dev;
Xiang Chen13c59902017-01-20 20:45:24 +080081 struct domain_device *device = task->dev;
82 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry27a3f222015-11-18 00:50:50 +080083
84 if (!slot->task)
85 return;
86
87 if (!sas_protocol_ata(task->task_proto))
88 if (slot->n_elem)
89 dma_unmap_sg(dev, task->scatter, slot->n_elem,
90 task->data_dir);
91
92 if (slot->command_table)
93 dma_pool_free(hisi_hba->command_table_pool,
94 slot->command_table, slot->command_table_dma);
95
96 if (slot->status_buffer)
97 dma_pool_free(hisi_hba->status_buffer_pool,
98 slot->status_buffer, slot->status_buffer_dma);
99
100 if (slot->sge_page)
101 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
102 slot->sge_page_dma);
103
104 list_del_init(&slot->entry);
105 task->lldd_task = NULL;
106 slot->task = NULL;
107 slot->port = NULL;
108 hisi_sas_slot_index_free(hisi_hba, slot->idx);
Xiang Chen13c59902017-01-20 20:45:24 +0800109 if (sas_dev)
110 atomic64_dec(&sas_dev->running_req);
John Garry59ba49f2016-09-06 23:36:14 +0800111 /* slot memory is fully zeroed when it is reused */
John Garry27a3f222015-11-18 00:50:50 +0800112}
113EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
114
John Garry66ee9992015-11-18 00:50:54 +0800115static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
116 struct hisi_sas_slot *slot)
117{
118 return hisi_hba->hw->prep_smp(hisi_hba, slot);
119}
120
John Garry42e7a692015-11-18 00:50:49 +0800121static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
122 struct hisi_sas_slot *slot, int is_tmf,
123 struct hisi_sas_tmf_task *tmf)
124{
125 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
126}
127
John Garry6f2ff1a2016-01-26 02:47:20 +0800128static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
129 struct hisi_sas_slot *slot)
130{
131 return hisi_hba->hw->prep_stp(hisi_hba, slot);
132}
133
John Garry441c2742016-08-24 19:05:47 +0800134static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
135 struct hisi_sas_slot *slot,
136 int device_id, int abort_flag, int tag_to_abort)
137{
138 return hisi_hba->hw->prep_abort(hisi_hba, slot,
139 device_id, abort_flag, tag_to_abort);
140}
141
John Garrycac9b2a2016-02-25 17:42:11 +0800142/*
143 * This function will issue an abort TMF regardless of whether the
144 * task is in the sdev or not. Then it will do the task complete
145 * cleanup and callbacks.
146 */
147static void hisi_sas_slot_abort(struct work_struct *work)
148{
149 struct hisi_sas_slot *abort_slot =
150 container_of(work, struct hisi_sas_slot, abort_slot);
151 struct sas_task *task = abort_slot->task;
152 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
153 struct scsi_cmnd *cmnd = task->uldd_task;
154 struct hisi_sas_tmf_task tmf_task;
John Garrycac9b2a2016-02-25 17:42:11 +0800155 struct scsi_lun lun;
156 struct device *dev = &hisi_hba->pdev->dev;
157 int tag = abort_slot->idx;
John Garryda7b66e2017-01-03 20:24:50 +0800158 unsigned long flags;
John Garrycac9b2a2016-02-25 17:42:11 +0800159
160 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
161 dev_err(dev, "cannot abort slot for non-ssp task\n");
162 goto out;
163 }
164
165 int_to_scsilun(cmnd->device->lun, &lun);
166 tmf_task.tmf = TMF_ABORT_TASK;
167 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
168
169 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
170out:
171 /* Do cleanup for this task */
John Garryda7b66e2017-01-03 20:24:50 +0800172 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800173 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
John Garryda7b66e2017-01-03 20:24:50 +0800174 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800175 if (task->task_done)
176 task->task_done(task);
John Garrycac9b2a2016-02-25 17:42:11 +0800177}
178
John Garry42e7a692015-11-18 00:50:49 +0800179static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
180 int is_tmf, struct hisi_sas_tmf_task *tmf,
181 int *pass)
182{
183 struct domain_device *device = task->dev;
184 struct hisi_sas_device *sas_dev = device->lldd_dev;
185 struct hisi_sas_port *port;
186 struct hisi_sas_slot *slot;
187 struct hisi_sas_cmd_hdr *cmd_hdr_base;
John Garry2e244f02017-03-23 01:25:17 +0800188 struct asd_sas_port *sas_port = device->port;
John Garry42e7a692015-11-18 00:50:49 +0800189 struct device *dev = &hisi_hba->pdev->dev;
190 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
191
John Garry2e244f02017-03-23 01:25:17 +0800192 if (!sas_port) {
John Garry42e7a692015-11-18 00:50:49 +0800193 struct task_status_struct *ts = &task->task_status;
194
195 ts->resp = SAS_TASK_UNDELIVERED;
196 ts->stat = SAS_PHY_DOWN;
197 /*
198 * libsas will use dev->port, should
199 * not call task_done for sata
200 */
201 if (device->dev_type != SAS_SATA_DEV)
202 task->task_done(task);
John Garryddabca22017-03-23 01:25:22 +0800203 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800204 }
205
206 if (DEV_IS_GONE(sas_dev)) {
207 if (sas_dev)
208 dev_info(dev, "task prep: device %llu not ready\n",
209 sas_dev->device_id);
210 else
211 dev_info(dev, "task prep: device %016llx not ready\n",
212 SAS_ADDR(device->sas_addr));
213
John Garryddabca22017-03-23 01:25:22 +0800214 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800215 }
John Garry2e244f02017-03-23 01:25:17 +0800216
217 port = to_hisi_sas_port(sas_port);
John Garry9859f242016-08-24 19:05:52 +0800218 if (port && !port->port_attached) {
John Garry09fe9ec2016-09-06 23:36:18 +0800219 dev_info(dev, "task prep: %s port%d not attach device\n",
220 (sas_protocol_ata(task->task_proto)) ?
221 "SATA/STP" : "SAS",
222 device->port->id);
John Garry42e7a692015-11-18 00:50:49 +0800223
John Garry09fe9ec2016-09-06 23:36:18 +0800224 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800225 }
226
227 if (!sas_protocol_ata(task->task_proto)) {
228 if (task->num_scatter) {
229 n_elem = dma_map_sg(dev, task->scatter,
230 task->num_scatter, task->data_dir);
231 if (!n_elem) {
232 rc = -ENOMEM;
233 goto prep_out;
234 }
235 }
236 } else
237 n_elem = task->num_scatter;
238
John Garry685b6d62016-04-15 21:36:36 +0800239 if (hisi_hba->hw->slot_index_alloc)
240 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
241 device);
242 else
243 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
John Garry42e7a692015-11-18 00:50:49 +0800244 if (rc)
245 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +0800246 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
247 &dlvry_queue, &dlvry_queue_slot);
John Garry42e7a692015-11-18 00:50:49 +0800248 if (rc)
249 goto err_out_tag;
250
251 slot = &hisi_hba->slot_info[slot_idx];
252 memset(slot, 0, sizeof(struct hisi_sas_slot));
253
254 slot->idx = slot_idx;
255 slot->n_elem = n_elem;
256 slot->dlvry_queue = dlvry_queue;
257 slot->dlvry_queue_slot = dlvry_queue_slot;
258 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
259 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
260 slot->task = task;
261 slot->port = port;
262 task->lldd_task = slot;
John Garrycac9b2a2016-02-25 17:42:11 +0800263 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
John Garry42e7a692015-11-18 00:50:49 +0800264
265 slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
266 GFP_ATOMIC,
267 &slot->status_buffer_dma);
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300268 if (!slot->status_buffer) {
269 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800270 goto err_out_slot_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300271 }
John Garry42e7a692015-11-18 00:50:49 +0800272 memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ);
273
274 slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool,
275 GFP_ATOMIC,
276 &slot->command_table_dma);
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300277 if (!slot->command_table) {
278 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800279 goto err_out_status_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300280 }
John Garry42e7a692015-11-18 00:50:49 +0800281 memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ);
282 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
283
284 switch (task->task_proto) {
John Garry66ee9992015-11-18 00:50:54 +0800285 case SAS_PROTOCOL_SMP:
286 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
287 break;
John Garry42e7a692015-11-18 00:50:49 +0800288 case SAS_PROTOCOL_SSP:
289 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
290 break;
291 case SAS_PROTOCOL_SATA:
292 case SAS_PROTOCOL_STP:
293 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
John Garry6f2ff1a2016-01-26 02:47:20 +0800294 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
295 break;
John Garry42e7a692015-11-18 00:50:49 +0800296 default:
297 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
298 task->task_proto);
299 rc = -EINVAL;
300 break;
301 }
302
303 if (rc) {
304 dev_err(dev, "task prep: rc = 0x%x\n", rc);
305 if (slot->sge_page)
306 goto err_out_sge;
307 goto err_out_command_table;
308 }
309
John Garry405314d2017-03-23 01:25:21 +0800310 list_add_tail(&slot->entry, &sas_dev->list);
John Garry42e7a692015-11-18 00:50:49 +0800311 spin_lock(&task->task_state_lock);
312 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
313 spin_unlock(&task->task_state_lock);
314
315 hisi_hba->slot_prep = slot;
316
John Garryf696cc32016-11-07 20:48:39 +0800317 atomic64_inc(&sas_dev->running_req);
John Garry42e7a692015-11-18 00:50:49 +0800318 ++(*pass);
319
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300320 return 0;
John Garry42e7a692015-11-18 00:50:49 +0800321
322err_out_sge:
323 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
324 slot->sge_page_dma);
325err_out_command_table:
326 dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
327 slot->command_table_dma);
328err_out_status_buf:
329 dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
330 slot->status_buffer_dma);
331err_out_slot_buf:
332 /* Nothing to be done */
333err_out_tag:
334 hisi_sas_slot_index_free(hisi_hba, slot_idx);
335err_out:
336 dev_err(dev, "task prep: failed[%d]!\n", rc);
337 if (!sas_protocol_ata(task->task_proto))
338 if (n_elem)
339 dma_unmap_sg(dev, task->scatter, n_elem,
340 task->data_dir);
341prep_out:
342 return rc;
343}
344
345static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
346 int is_tmf, struct hisi_sas_tmf_task *tmf)
347{
348 u32 rc;
349 u32 pass = 0;
350 unsigned long flags;
351 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
352 struct device *dev = &hisi_hba->pdev->dev;
353
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800354 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
355 return -EINVAL;
356
John Garry42e7a692015-11-18 00:50:49 +0800357 /* protect task_prep and start_delivery sequence */
358 spin_lock_irqsave(&hisi_hba->lock, flags);
359 rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass);
360 if (rc)
361 dev_err(dev, "task exec: failed[%d]!\n", rc);
362
363 if (likely(pass))
364 hisi_hba->hw->start_delivery(hisi_hba);
365 spin_unlock_irqrestore(&hisi_hba->lock, flags);
366
367 return rc;
368}
John Garry257efd12015-11-18 00:50:36 +0800369
John Garry66139922015-11-18 00:50:48 +0800370static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
371{
372 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
373 struct asd_sas_phy *sas_phy = &phy->sas_phy;
374 struct sas_ha_struct *sas_ha;
375
376 if (!phy->phy_attached)
377 return;
378
379 sas_ha = &hisi_hba->sha;
380 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
381
382 if (sas_phy->phy) {
383 struct sas_phy *sphy = sas_phy->phy;
384
385 sphy->negotiated_linkrate = sas_phy->linkrate;
John Garry66139922015-11-18 00:50:48 +0800386 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
Xiang Chen2ae75782016-11-07 20:48:40 +0800387 sphy->maximum_linkrate_hw =
388 hisi_hba->hw->phy_get_max_linkrate();
389 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
390 sphy->minimum_linkrate = phy->minimum_linkrate;
391
392 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
393 sphy->maximum_linkrate = phy->maximum_linkrate;
John Garry66139922015-11-18 00:50:48 +0800394 }
395
396 if (phy->phy_type & PORT_TYPE_SAS) {
397 struct sas_identify_frame *id;
398
399 id = (struct sas_identify_frame *)phy->frame_rcvd;
400 id->dev_type = phy->identify.device_type;
401 id->initiator_bits = SAS_PROTOCOL_ALL;
402 id->target_bits = phy->identify.target_port_protocols;
403 } else if (phy->phy_type & PORT_TYPE_SATA) {
404 /*Nothing*/
405 }
406
407 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
408 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
409}
410
John Garryabda97c2015-11-18 00:50:51 +0800411static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
412{
413 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
414 struct hisi_sas_device *sas_dev = NULL;
415 int i;
416
417 spin_lock(&hisi_hba->lock);
418 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
419 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
420 hisi_hba->devices[i].device_id = i;
421 sas_dev = &hisi_hba->devices[i];
422 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
423 sas_dev->dev_type = device->dev_type;
424 sas_dev->hisi_hba = hisi_hba;
425 sas_dev->sas_device = device;
John Garry405314d2017-03-23 01:25:21 +0800426 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
John Garryabda97c2015-11-18 00:50:51 +0800427 break;
428 }
429 }
430 spin_unlock(&hisi_hba->lock);
431
432 return sas_dev;
433}
434
435static int hisi_sas_dev_found(struct domain_device *device)
436{
437 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
438 struct domain_device *parent_dev = device->parent;
439 struct hisi_sas_device *sas_dev;
440 struct device *dev = &hisi_hba->pdev->dev;
441
John Garry685b6d62016-04-15 21:36:36 +0800442 if (hisi_hba->hw->alloc_dev)
443 sas_dev = hisi_hba->hw->alloc_dev(device);
444 else
445 sas_dev = hisi_sas_alloc_dev(device);
John Garryabda97c2015-11-18 00:50:51 +0800446 if (!sas_dev) {
447 dev_err(dev, "fail alloc dev: max support %d devices\n",
448 HISI_SAS_MAX_DEVICES);
449 return -EINVAL;
450 }
451
452 device->lldd_dev = sas_dev;
453 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
454
455 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
456 int phy_no;
457 u8 phy_num = parent_dev->ex_dev.num_phys;
458 struct ex_phy *phy;
459
460 for (phy_no = 0; phy_no < phy_num; phy_no++) {
461 phy = &parent_dev->ex_dev.ex_phy[phy_no];
462 if (SAS_ADDR(phy->attached_sas_addr) ==
463 SAS_ADDR(device->sas_addr)) {
464 sas_dev->attached_phy = phy_no;
465 break;
466 }
467 }
468
469 if (phy_no == phy_num) {
470 dev_info(dev, "dev found: no attached "
471 "dev:%016llx at ex:%016llx\n",
472 SAS_ADDR(device->sas_addr),
473 SAS_ADDR(parent_dev->sas_addr));
474 return -EINVAL;
475 }
476 }
477
478 return 0;
479}
480
John Garry31eec8a2016-02-25 17:42:14 +0800481static int hisi_sas_slave_configure(struct scsi_device *sdev)
482{
483 struct domain_device *dev = sdev_to_domain_dev(sdev);
484 int ret = sas_slave_configure(sdev);
485
486 if (ret)
487 return ret;
488 if (!dev_is_sata(dev))
489 sas_change_queue_depth(sdev, 64);
490
491 return 0;
492}
493
John Garry701f75e2015-11-18 00:50:55 +0800494static void hisi_sas_scan_start(struct Scsi_Host *shost)
495{
496 struct hisi_hba *hisi_hba = shost_priv(shost);
John Garry701f75e2015-11-18 00:50:55 +0800497
John Garry396b8042017-03-23 01:25:19 +0800498 hisi_hba->hw->phys_init(hisi_hba);
John Garry701f75e2015-11-18 00:50:55 +0800499}
500
501static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
502{
503 struct hisi_hba *hisi_hba = shost_priv(shost);
504 struct sas_ha_struct *sha = &hisi_hba->sha;
505
John Garry396b8042017-03-23 01:25:19 +0800506 /* Wait for PHY up interrupt to occur */
507 if (time < HZ)
John Garry701f75e2015-11-18 00:50:55 +0800508 return 0;
509
510 sas_drain_work(sha);
511 return 1;
512}
513
John Garry66139922015-11-18 00:50:48 +0800514static void hisi_sas_phyup_work(struct work_struct *work)
515{
516 struct hisi_sas_phy *phy =
517 container_of(work, struct hisi_sas_phy, phyup_ws);
518 struct hisi_hba *hisi_hba = phy->hisi_hba;
519 struct asd_sas_phy *sas_phy = &phy->sas_phy;
520 int phy_no = sas_phy->id;
521
522 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
523 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
524}
John Garry976867e2015-11-18 00:50:42 +0800525
526static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
527{
528 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
529 struct asd_sas_phy *sas_phy = &phy->sas_phy;
530
531 phy->hisi_hba = hisi_hba;
532 phy->port = NULL;
533 init_timer(&phy->timer);
534 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
535 sas_phy->class = SAS;
536 sas_phy->iproto = SAS_PROTOCOL_ALL;
537 sas_phy->tproto = 0;
538 sas_phy->type = PHY_TYPE_PHYSICAL;
539 sas_phy->role = PHY_ROLE_INITIATOR;
540 sas_phy->oob_mode = OOB_NOT_CONNECTED;
541 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
542 sas_phy->id = phy_no;
543 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
544 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
545 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
546 sas_phy->lldd_phy = phy;
John Garry66139922015-11-18 00:50:48 +0800547
548 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
John Garry976867e2015-11-18 00:50:42 +0800549}
550
John Garry184a4632015-11-18 00:50:52 +0800551static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
552{
553 struct sas_ha_struct *sas_ha = sas_phy->ha;
554 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
555 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
556 struct asd_sas_port *sas_port = sas_phy->port;
John Garry2e244f02017-03-23 01:25:17 +0800557 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
John Garry184a4632015-11-18 00:50:52 +0800558 unsigned long flags;
559
560 if (!sas_port)
561 return;
562
563 spin_lock_irqsave(&hisi_hba->lock, flags);
564 port->port_attached = 1;
565 port->id = phy->port_id;
566 phy->port = port;
567 sas_port->lldd_port = port;
568 spin_unlock_irqrestore(&hisi_hba->lock, flags);
569}
570
John Garry405314d2017-03-23 01:25:21 +0800571static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba,
572 struct sas_task *task,
573 struct hisi_sas_slot *slot)
John Garry184a4632015-11-18 00:50:52 +0800574{
John Garry405314d2017-03-23 01:25:21 +0800575 struct task_status_struct *ts;
576 unsigned long flags;
John Garry184a4632015-11-18 00:50:52 +0800577
John Garry405314d2017-03-23 01:25:21 +0800578 if (!task)
John Garry184a4632015-11-18 00:50:52 +0800579 return;
580
John Garry405314d2017-03-23 01:25:21 +0800581 ts = &task->task_status;
John Garry184a4632015-11-18 00:50:52 +0800582
John Garry405314d2017-03-23 01:25:21 +0800583 ts->resp = SAS_TASK_COMPLETE;
584 ts->stat = SAS_ABORTED_TASK;
585 spin_lock_irqsave(&task->task_state_lock, flags);
586 task->task_state_flags &=
587 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
588 task->task_state_flags |= SAS_TASK_STATE_DONE;
589 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry184a4632015-11-18 00:50:52 +0800590
John Garry405314d2017-03-23 01:25:21 +0800591 hisi_sas_slot_task_free(hisi_hba, task, slot);
John Garry184a4632015-11-18 00:50:52 +0800592}
593
John Garry405314d2017-03-23 01:25:21 +0800594/* hisi_hba.lock should be locked */
John Garry184a4632015-11-18 00:50:52 +0800595static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
596 struct domain_device *device)
597{
John Garry405314d2017-03-23 01:25:21 +0800598 struct hisi_sas_slot *slot, *slot2;
599 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry184a4632015-11-18 00:50:52 +0800600
John Garry405314d2017-03-23 01:25:21 +0800601 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
602 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
John Garry184a4632015-11-18 00:50:52 +0800603}
604
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800605static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
606{
John Garry405314d2017-03-23 01:25:21 +0800607 struct hisi_sas_device *sas_dev;
608 struct domain_device *device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800609 int i;
610
John Garry405314d2017-03-23 01:25:21 +0800611 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
612 sas_dev = &hisi_hba->devices[i];
613 device = sas_dev->sas_device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800614
John Garry405314d2017-03-23 01:25:21 +0800615 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
616 !device)
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800617 continue;
John Garry405314d2017-03-23 01:25:21 +0800618
619 hisi_sas_release_task(hisi_hba, device);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800620 }
621}
622
John Garryabda97c2015-11-18 00:50:51 +0800623static void hisi_sas_dev_gone(struct domain_device *device)
624{
625 struct hisi_sas_device *sas_dev = device->lldd_dev;
626 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
627 struct device *dev = &hisi_hba->pdev->dev;
628 u64 dev_id = sas_dev->device_id;
629
630 dev_info(dev, "found dev[%lld:%x] is gone\n",
631 sas_dev->device_id, sas_dev->dev_type);
632
John Garry40f27022016-08-24 19:05:48 +0800633 hisi_sas_internal_task_abort(hisi_hba, device,
634 HISI_SAS_INT_ABT_DEV, 0);
635
John Garryabda97c2015-11-18 00:50:51 +0800636 hisi_hba->hw->free_device(hisi_hba, sas_dev);
637 device->lldd_dev = NULL;
638 memset(sas_dev, 0, sizeof(*sas_dev));
639 sas_dev->device_id = dev_id;
640 sas_dev->dev_type = SAS_PHY_UNUSED;
641 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
642}
John Garry42e7a692015-11-18 00:50:49 +0800643
644static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
645{
646 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
647}
648
John Garrye4189d52015-11-18 00:50:57 +0800649static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
650 void *funcdata)
651{
652 struct sas_ha_struct *sas_ha = sas_phy->ha;
653 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
654 int phy_no = sas_phy->id;
655
656 switch (func) {
657 case PHY_FUNC_HARD_RESET:
658 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
659 break;
660
661 case PHY_FUNC_LINK_RESET:
662 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
663 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
664 break;
665
666 case PHY_FUNC_DISABLE:
667 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
668 break;
669
670 case PHY_FUNC_SET_LINK_RATE:
Xiang Chen2ae75782016-11-07 20:48:40 +0800671 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
672 break;
673
John Garrye4189d52015-11-18 00:50:57 +0800674 case PHY_FUNC_RELEASE_SPINUP_HOLD:
675 default:
676 return -EOPNOTSUPP;
677 }
678 return 0;
679}
John Garry184a4632015-11-18 00:50:52 +0800680
John Garry0efff302015-11-18 00:50:56 +0800681static void hisi_sas_task_done(struct sas_task *task)
682{
683 if (!del_timer(&task->slow_task->timer))
684 return;
685 complete(&task->slow_task->completion);
686}
687
688static void hisi_sas_tmf_timedout(unsigned long data)
689{
690 struct sas_task *task = (struct sas_task *)data;
691
692 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
693 complete(&task->slow_task->completion);
694}
695
696#define TASK_TIMEOUT 20
697#define TASK_RETRY 3
698static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
699 void *parameter, u32 para_len,
700 struct hisi_sas_tmf_task *tmf)
701{
702 struct hisi_sas_device *sas_dev = device->lldd_dev;
703 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
704 struct device *dev = &hisi_hba->pdev->dev;
705 struct sas_task *task;
706 int res, retry;
707
708 for (retry = 0; retry < TASK_RETRY; retry++) {
709 task = sas_alloc_slow_task(GFP_KERNEL);
710 if (!task)
711 return -ENOMEM;
712
713 task->dev = device;
714 task->task_proto = device->tproto;
715
Xiang Chen7c594f02017-03-23 01:25:20 +0800716 if (dev_is_sata(device)) {
717 task->ata_task.device_control_reg_update = 1;
718 memcpy(&task->ata_task.fis, parameter, para_len);
719 } else {
720 memcpy(&task->ssp_task, parameter, para_len);
721 }
John Garry0efff302015-11-18 00:50:56 +0800722 task->task_done = hisi_sas_task_done;
723
724 task->slow_task->timer.data = (unsigned long) task;
725 task->slow_task->timer.function = hisi_sas_tmf_timedout;
726 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
727 add_timer(&task->slow_task->timer);
728
729 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
730
731 if (res) {
732 del_timer(&task->slow_task->timer);
733 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
734 res);
735 goto ex_err;
736 }
737
738 wait_for_completion(&task->slow_task->completion);
739 res = TMF_RESP_FUNC_FAILED;
740 /* Even TMF timed out, return direct. */
741 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
742 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Xiang Chen7c594f02017-03-23 01:25:20 +0800743 dev_err(dev, "abort tmf: TMF task timeout\n");
John Garry0efff302015-11-18 00:50:56 +0800744 if (task->lldd_task) {
745 struct hisi_sas_slot *slot =
746 task->lldd_task;
747
748 hisi_sas_slot_task_free(hisi_hba,
749 task, slot);
750 }
751
752 goto ex_err;
753 }
754 }
755
756 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800757 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800758 res = TMF_RESP_FUNC_COMPLETE;
759 break;
760 }
761
762 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800763 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
764 res = TMF_RESP_FUNC_SUCC;
765 break;
766 }
767
768 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800769 task->task_status.stat == SAS_DATA_UNDERRUN) {
770 /* no error, but return the number of bytes of
771 * underrun
772 */
773 dev_warn(dev, "abort tmf: task to dev %016llx "
774 "resp: 0x%x sts 0x%x underrun\n",
775 SAS_ADDR(device->sas_addr),
776 task->task_status.resp,
777 task->task_status.stat);
778 res = task->task_status.residual;
779 break;
780 }
781
782 if (task->task_status.resp == SAS_TASK_COMPLETE &&
783 task->task_status.stat == SAS_DATA_OVERRUN) {
784 dev_warn(dev, "abort tmf: blocked task error\n");
785 res = -EMSGSIZE;
786 break;
787 }
788
789 dev_warn(dev, "abort tmf: task to dev "
790 "%016llx resp: 0x%x status 0x%x\n",
791 SAS_ADDR(device->sas_addr), task->task_status.resp,
792 task->task_status.stat);
793 sas_free_task(task);
794 task = NULL;
795 }
796ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800797 if (retry == TASK_RETRY)
798 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800799 sas_free_task(task);
800 return res;
801}
802
Xiang Chen7c594f02017-03-23 01:25:20 +0800803static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
804 bool reset, int pmp, u8 *fis)
805{
806 struct ata_taskfile tf;
807
808 ata_tf_init(dev, &tf);
809 if (reset)
810 tf.ctl |= ATA_SRST;
811 else
812 tf.ctl &= ~ATA_SRST;
813 tf.command = ATA_CMD_DEV_RESET;
814 ata_tf_to_fis(&tf, pmp, 0, fis);
815}
816
817static int hisi_sas_softreset_ata_disk(struct domain_device *device)
818{
819 u8 fis[20] = {0};
820 struct ata_port *ap = device->sata_dev.ap;
821 struct ata_link *link;
822 int rc = TMF_RESP_FUNC_FAILED;
823 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
824 struct device *dev = &hisi_hba->pdev->dev;
825 int s = sizeof(struct host_to_dev_fis);
826 unsigned long flags;
827
828 ata_for_each_link(link, ap, EDGE) {
829 int pmp = sata_srst_pmp(link);
830
831 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
832 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
833 if (rc != TMF_RESP_FUNC_COMPLETE)
834 break;
835 }
836
837 if (rc == TMF_RESP_FUNC_COMPLETE) {
838 ata_for_each_link(link, ap, EDGE) {
839 int pmp = sata_srst_pmp(link);
840
841 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
842 rc = hisi_sas_exec_internal_tmf_task(device, fis,
843 s, NULL);
844 if (rc != TMF_RESP_FUNC_COMPLETE)
845 dev_err(dev, "ata disk de-reset failed\n");
846 }
847 } else {
848 dev_err(dev, "ata disk reset failed\n");
849 }
850
851 if (rc == TMF_RESP_FUNC_COMPLETE) {
852 spin_lock_irqsave(&hisi_hba->lock, flags);
853 hisi_sas_release_task(hisi_hba, device);
854 spin_unlock_irqrestore(&hisi_hba->lock, flags);
855 }
856
857 return rc;
858}
859
John Garry0efff302015-11-18 00:50:56 +0800860static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
861 u8 *lun, struct hisi_sas_tmf_task *tmf)
862{
863 struct sas_ssp_task ssp_task;
864
865 if (!(device->tproto & SAS_PROTOCOL_SSP))
866 return TMF_RESP_FUNC_ESUPP;
867
868 memcpy(ssp_task.LUN, lun, 8);
869
870 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
871 sizeof(ssp_task), tmf);
872}
873
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800874static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
875{
876 int rc;
877
878 if (!hisi_hba->hw->soft_reset)
879 return -1;
880
881 if (!test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
882 struct device *dev = &hisi_hba->pdev->dev;
883 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
884 unsigned long flags;
885
886 dev_dbg(dev, "controller reset begins!\n");
887 scsi_block_requests(hisi_hba->shost);
888 rc = hisi_hba->hw->soft_reset(hisi_hba);
889 if (rc) {
890 dev_warn(dev, "controller reset failed (%d)\n", rc);
891 goto out;
892 }
893 spin_lock_irqsave(&hisi_hba->lock, flags);
894 hisi_sas_release_tasks(hisi_hba);
895 spin_unlock_irqrestore(&hisi_hba->lock, flags);
896
897 sas_ha->notify_ha_event(sas_ha, HAE_RESET);
898 dev_dbg(dev, "controller reset successful!\n");
899 } else
900 return -1;
901
902out:
903 scsi_unblock_requests(hisi_hba->shost);
904 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
905 return rc;
906}
907
John Garry0efff302015-11-18 00:50:56 +0800908static int hisi_sas_abort_task(struct sas_task *task)
909{
910 struct scsi_lun lun;
911 struct hisi_sas_tmf_task tmf_task;
912 struct domain_device *device = task->dev;
913 struct hisi_sas_device *sas_dev = device->lldd_dev;
914 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
915 struct device *dev = &hisi_hba->pdev->dev;
916 int rc = TMF_RESP_FUNC_FAILED;
917 unsigned long flags;
918
919 if (!sas_dev) {
920 dev_warn(dev, "Device has been removed\n");
921 return TMF_RESP_FUNC_FAILED;
922 }
923
924 spin_lock_irqsave(&task->task_state_lock, flags);
925 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
926 spin_unlock_irqrestore(&task->task_state_lock, flags);
927 rc = TMF_RESP_FUNC_COMPLETE;
928 goto out;
929 }
930
931 spin_unlock_irqrestore(&task->task_state_lock, flags);
932 sas_dev->dev_status = HISI_SAS_DEV_EH;
933 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
934 struct scsi_cmnd *cmnd = task->uldd_task;
935 struct hisi_sas_slot *slot = task->lldd_task;
936 u32 tag = slot->idx;
937
938 int_to_scsilun(cmnd->device->lun, &lun);
939 tmf_task.tmf = TMF_ABORT_TASK;
940 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
941
942 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
943 &tmf_task);
944
945 /* if successful, clear the task and callback forwards.*/
946 if (rc == TMF_RESP_FUNC_COMPLETE) {
947 if (task->lldd_task) {
948 struct hisi_sas_slot *slot;
949
950 slot = &hisi_hba->slot_info
951 [tmf_task.tag_of_task_to_be_managed];
952 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry405314d2017-03-23 01:25:21 +0800953 hisi_hba->hw->slot_complete(hisi_hba, slot);
John Garry0efff302015-11-18 00:50:56 +0800954 spin_unlock_irqrestore(&hisi_hba->lock, flags);
955 }
956 }
957
John Garrydc8a49c2016-08-24 19:05:49 +0800958 hisi_sas_internal_task_abort(hisi_hba, device,
959 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800960 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
961 task->task_proto & SAS_PROTOCOL_STP) {
962 if (task->dev->dev_type == SAS_SATA_DEV) {
John Garrydc8a49c2016-08-24 19:05:49 +0800963 hisi_sas_internal_task_abort(hisi_hba, device,
964 HISI_SAS_INT_ABT_DEV, 0);
Xiang Chen7c594f02017-03-23 01:25:20 +0800965 rc = hisi_sas_softreset_ata_disk(device);
John Garry0efff302015-11-18 00:50:56 +0800966 }
John Garrydc8a49c2016-08-24 19:05:49 +0800967 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
968 /* SMP */
969 struct hisi_sas_slot *slot = task->lldd_task;
970 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +0800971
John Garrydc8a49c2016-08-24 19:05:49 +0800972 hisi_sas_internal_task_abort(hisi_hba, device,
973 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800974 }
975
976out:
977 if (rc != TMF_RESP_FUNC_COMPLETE)
978 dev_notice(dev, "abort task: rc=%d\n", rc);
979 return rc;
980}
981
982static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
983{
984 struct hisi_sas_tmf_task tmf_task;
985 int rc = TMF_RESP_FUNC_FAILED;
986
987 tmf_task.tmf = TMF_ABORT_TASK_SET;
988 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
989
990 return rc;
991}
992
993static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
994{
995 int rc = TMF_RESP_FUNC_FAILED;
996 struct hisi_sas_tmf_task tmf_task;
997
998 tmf_task.tmf = TMF_CLEAR_ACA;
999 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1000
1001 return rc;
1002}
1003
1004static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1005{
1006 struct sas_phy *phy = sas_get_local_phy(device);
1007 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1008 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1009 rc = sas_phy_reset(phy, reset_type);
1010 sas_put_local_phy(phy);
1011 msleep(2000);
1012 return rc;
1013}
1014
1015static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1016{
1017 struct hisi_sas_device *sas_dev = device->lldd_dev;
1018 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1019 unsigned long flags;
1020 int rc = TMF_RESP_FUNC_FAILED;
1021
1022 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1023 return TMF_RESP_FUNC_FAILED;
1024 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1025
1026 rc = hisi_sas_debug_I_T_nexus_reset(device);
1027
1028 spin_lock_irqsave(&hisi_hba->lock, flags);
1029 hisi_sas_release_task(hisi_hba, device);
1030 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1031
1032 return 0;
1033}
1034
1035static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1036{
1037 struct hisi_sas_tmf_task tmf_task;
1038 struct hisi_sas_device *sas_dev = device->lldd_dev;
1039 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1040 struct device *dev = &hisi_hba->pdev->dev;
1041 unsigned long flags;
1042 int rc = TMF_RESP_FUNC_FAILED;
1043
1044 tmf_task.tmf = TMF_LU_RESET;
1045 sas_dev->dev_status = HISI_SAS_DEV_EH;
1046 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1047 if (rc == TMF_RESP_FUNC_COMPLETE) {
1048 spin_lock_irqsave(&hisi_hba->lock, flags);
1049 hisi_sas_release_task(hisi_hba, device);
1050 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1051 }
1052
1053 /* If failed, fall-through I_T_Nexus reset */
1054 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
1055 sas_dev->device_id, rc);
1056 return rc;
1057}
1058
1059static int hisi_sas_query_task(struct sas_task *task)
1060{
1061 struct scsi_lun lun;
1062 struct hisi_sas_tmf_task tmf_task;
1063 int rc = TMF_RESP_FUNC_FAILED;
1064
1065 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1066 struct scsi_cmnd *cmnd = task->uldd_task;
1067 struct domain_device *device = task->dev;
1068 struct hisi_sas_slot *slot = task->lldd_task;
1069 u32 tag = slot->idx;
1070
1071 int_to_scsilun(cmnd->device->lun, &lun);
1072 tmf_task.tmf = TMF_QUERY_TASK;
1073 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1074
1075 rc = hisi_sas_debug_issue_ssp_tmf(device,
1076 lun.scsi_lun,
1077 &tmf_task);
1078 switch (rc) {
1079 /* The task is still in Lun, release it then */
1080 case TMF_RESP_FUNC_SUCC:
1081 /* The task is not in Lun or failed, reset the phy */
1082 case TMF_RESP_FUNC_FAILED:
1083 case TMF_RESP_FUNC_COMPLETE:
1084 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001085 default:
1086 rc = TMF_RESP_FUNC_FAILED;
1087 break;
John Garry0efff302015-11-18 00:50:56 +08001088 }
1089 }
1090 return rc;
1091}
1092
John Garry441c2742016-08-24 19:05:47 +08001093static int
1094hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
1095 struct sas_task *task, int abort_flag,
1096 int task_tag)
1097{
1098 struct domain_device *device = task->dev;
1099 struct hisi_sas_device *sas_dev = device->lldd_dev;
1100 struct device *dev = &hisi_hba->pdev->dev;
1101 struct hisi_sas_port *port;
1102 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001103 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001104 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1105 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1106
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001107 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
1108 return -EINVAL;
1109
John Garry441c2742016-08-24 19:05:47 +08001110 if (!device->port)
1111 return -1;
1112
John Garry2e244f02017-03-23 01:25:17 +08001113 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001114
1115 /* simply get a slot and send abort command */
1116 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1117 if (rc)
1118 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +08001119 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
1120 &dlvry_queue, &dlvry_queue_slot);
John Garry441c2742016-08-24 19:05:47 +08001121 if (rc)
1122 goto err_out_tag;
1123
1124 slot = &hisi_hba->slot_info[slot_idx];
1125 memset(slot, 0, sizeof(struct hisi_sas_slot));
1126
1127 slot->idx = slot_idx;
1128 slot->n_elem = n_elem;
1129 slot->dlvry_queue = dlvry_queue;
1130 slot->dlvry_queue_slot = dlvry_queue_slot;
1131 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1132 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1133 slot->task = task;
1134 slot->port = port;
1135 task->lldd_task = slot;
1136
1137 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1138
1139 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1140 abort_flag, task_tag);
1141 if (rc)
1142 goto err_out_tag;
1143
John Garry405314d2017-03-23 01:25:21 +08001144
1145 list_add_tail(&slot->entry, &sas_dev->list);
John Garry441c2742016-08-24 19:05:47 +08001146 spin_lock(&task->task_state_lock);
1147 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1148 spin_unlock(&task->task_state_lock);
1149
1150 hisi_hba->slot_prep = slot;
1151
John Garryf696cc32016-11-07 20:48:39 +08001152 atomic64_inc(&sas_dev->running_req);
1153
John Garry441c2742016-08-24 19:05:47 +08001154 /* send abort command to our chip */
1155 hisi_hba->hw->start_delivery(hisi_hba);
1156
1157 return 0;
1158
1159err_out_tag:
1160 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1161err_out:
1162 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1163
1164 return rc;
1165}
1166
1167/**
1168 * hisi_sas_internal_task_abort -- execute an internal
1169 * abort command for single IO command or a device
1170 * @hisi_hba: host controller struct
1171 * @device: domain device
1172 * @abort_flag: mode of operation, device or single IO
1173 * @tag: tag of IO to be aborted (only relevant to single
1174 * IO mode)
1175 */
1176static int
1177hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1178 struct domain_device *device,
1179 int abort_flag, int tag)
1180{
1181 struct sas_task *task;
1182 struct hisi_sas_device *sas_dev = device->lldd_dev;
1183 struct device *dev = &hisi_hba->pdev->dev;
1184 int res;
1185 unsigned long flags;
1186
1187 if (!hisi_hba->hw->prep_abort)
1188 return -EOPNOTSUPP;
1189
1190 task = sas_alloc_slow_task(GFP_KERNEL);
1191 if (!task)
1192 return -ENOMEM;
1193
1194 task->dev = device;
1195 task->task_proto = device->tproto;
1196 task->task_done = hisi_sas_task_done;
1197 task->slow_task->timer.data = (unsigned long)task;
1198 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1199 task->slow_task->timer.expires = jiffies + 20*HZ;
1200 add_timer(&task->slow_task->timer);
1201
1202 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1203 spin_lock_irqsave(&hisi_hba->lock, flags);
1204 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1205 task, abort_flag, tag);
1206 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1207 if (res) {
1208 del_timer(&task->slow_task->timer);
1209 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1210 res);
1211 goto exit;
1212 }
1213 wait_for_completion(&task->slow_task->completion);
1214 res = TMF_RESP_FUNC_FAILED;
1215
1216 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1217 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1218 res = TMF_RESP_FUNC_COMPLETE;
1219 goto exit;
1220 }
1221
1222 /* TMF timed out, return direct. */
1223 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1224 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1225 dev_err(dev, "internal task abort: timeout.\n");
1226 if (task->lldd_task) {
1227 struct hisi_sas_slot *slot = task->lldd_task;
1228
1229 hisi_sas_slot_task_free(hisi_hba, task, slot);
1230 }
1231 }
1232 }
1233
1234exit:
John Garry297d7302017-01-20 20:45:22 +08001235 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001236 "resp: 0x%x sts 0x%x\n",
1237 SAS_ADDR(device->sas_addr),
1238 task,
1239 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1240 task->task_status.stat);
1241 sas_free_task(task);
1242
1243 return res;
1244}
1245
John Garry184a4632015-11-18 00:50:52 +08001246static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1247{
1248 hisi_sas_port_notify_formed(sas_phy);
1249}
1250
John Garry184a4632015-11-18 00:50:52 +08001251static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1252{
1253 phy->phy_attached = 0;
1254 phy->phy_type = 0;
1255 phy->port = NULL;
1256}
1257
1258void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1259{
1260 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1261 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1262 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1263
1264 if (rdy) {
1265 /* Phy down but ready */
1266 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1267 hisi_sas_port_notify_formed(sas_phy);
1268 } else {
1269 struct hisi_sas_port *port = phy->port;
1270
1271 /* Phy down and not ready */
1272 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1273 sas_phy_disconnected(sas_phy);
1274
1275 if (port) {
1276 if (phy->phy_type & PORT_TYPE_SAS) {
1277 int port_id = port->id;
1278
1279 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1280 port_id))
1281 port->port_attached = 0;
1282 } else if (phy->phy_type & PORT_TYPE_SATA)
1283 port->port_attached = 0;
1284 }
1285 hisi_sas_phy_disconnected(phy);
1286 }
1287}
1288EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1289
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001290void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1291 u32 state)
1292{
1293 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1294 int phy_no;
1295
1296 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1297 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1298 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1299 struct asd_sas_port *sas_port = sas_phy->port;
1300 struct domain_device *dev;
1301
1302 if (sas_phy->enabled) {
1303 /* Report PHY state change to libsas */
1304 if (state & (1 << phy_no))
1305 continue;
1306
1307 if (old_state & (1 << phy_no))
1308 /* PHY down but was up before */
1309 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1310 }
1311 if (!sas_port)
1312 continue;
1313 dev = sas_port->port_dev;
1314
1315 if (DEV_IS_EXPANDER(dev->dev_type))
1316 sas_ha->notify_phy_event(sas_phy, PORTE_BROADCAST_RCVD);
1317 }
1318}
1319EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology);
1320
John Garrye8899fa2015-11-18 00:50:30 +08001321static struct scsi_transport_template *hisi_sas_stt;
1322
John Garry7eb78692015-11-18 00:50:31 +08001323static struct scsi_host_template hisi_sas_sht = {
1324 .module = THIS_MODULE,
1325 .name = DRV_NAME,
1326 .queuecommand = sas_queuecommand,
1327 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001328 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001329 .scan_finished = hisi_sas_scan_finished,
1330 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001331 .change_queue_depth = sas_change_queue_depth,
1332 .bios_param = sas_bios_param,
1333 .can_queue = 1,
1334 .this_id = -1,
1335 .sg_tablesize = SG_ALL,
1336 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1337 .use_clustering = ENABLE_CLUSTERING,
1338 .eh_device_reset_handler = sas_eh_device_reset_handler,
1339 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1340 .target_destroy = sas_target_destroy,
1341 .ioctl = sas_ioctl,
1342};
1343
John Garrye8899fa2015-11-18 00:50:30 +08001344static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001345 .lldd_dev_found = hisi_sas_dev_found,
1346 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001347 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001348 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001349 .lldd_abort_task = hisi_sas_abort_task,
1350 .lldd_abort_task_set = hisi_sas_abort_task_set,
1351 .lldd_clear_aca = hisi_sas_clear_aca,
1352 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1353 .lldd_lu_reset = hisi_sas_lu_reset,
1354 .lldd_query_task = hisi_sas_query_task,
John Garry184a4632015-11-18 00:50:52 +08001355 .lldd_port_formed = hisi_sas_port_formed,
John Garrye8899fa2015-11-18 00:50:30 +08001356};
1357
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001358void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1359{
1360 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1361
1362 for (i = 0; i < hisi_hba->queue_count; i++) {
1363 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1364 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1365
1366 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1367 memset(hisi_hba->cmd_hdr[i], 0, s);
1368 dq->wr_point = 0;
1369
1370 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1371 memset(hisi_hba->complete_hdr[i], 0, s);
1372 cq->rd_point = 0;
1373 }
1374
1375 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1376 memset(hisi_hba->initial_fis, 0, s);
1377
1378 s = max_command_entries * sizeof(struct hisi_sas_iost);
1379 memset(hisi_hba->iost, 0, s);
1380
1381 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1382 memset(hisi_hba->breakpoint, 0, s);
1383
1384 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1385 memset(hisi_hba->sata_breakpoint, 0, s);
1386}
1387EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1388
John Garry6be6de12015-11-18 00:50:34 +08001389static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1390{
John Garry6be6de12015-11-18 00:50:34 +08001391 struct platform_device *pdev = hisi_hba->pdev;
1392 struct device *dev = &pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001393 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001394
John Garryfa42d802015-11-18 00:50:43 +08001395 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001396 for (i = 0; i < hisi_hba->n_phy; i++) {
1397 hisi_sas_phy_init(hisi_hba, i);
1398 hisi_hba->port[i].port_attached = 0;
1399 hisi_hba->port[i].id = -1;
John Garry976867e2015-11-18 00:50:42 +08001400 }
1401
John Garryaf740db2015-11-18 00:50:41 +08001402 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1403 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1404 hisi_hba->devices[i].device_id = i;
1405 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1406 }
1407
John Garry6be6de12015-11-18 00:50:34 +08001408 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001409 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001410 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001411
1412 /* Completion queue structure */
1413 cq->id = i;
1414 cq->hisi_hba = hisi_hba;
1415
John Garry4fde02a2016-09-06 23:36:12 +08001416 /* Delivery queue structure */
1417 dq->id = i;
1418 dq->hisi_hba = hisi_hba;
1419
John Garry6be6de12015-11-18 00:50:34 +08001420 /* Delivery queue */
1421 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1422 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1423 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1424 if (!hisi_hba->cmd_hdr[i])
1425 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001426
1427 /* Completion queue */
1428 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1429 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1430 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1431 if (!hisi_hba->complete_hdr[i])
1432 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001433 }
1434
1435 s = HISI_SAS_STATUS_BUF_SZ;
1436 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1437 dev, s, 16, 0);
1438 if (!hisi_hba->status_buffer_pool)
1439 goto err_out;
1440
1441 s = HISI_SAS_COMMAND_TABLE_SZ;
1442 hisi_hba->command_table_pool = dma_pool_create("command_table",
1443 dev, s, 16, 0);
1444 if (!hisi_hba->command_table_pool)
1445 goto err_out;
1446
1447 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1448 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1449 GFP_KERNEL);
1450 if (!hisi_hba->itct)
1451 goto err_out;
1452
1453 memset(hisi_hba->itct, 0, s);
1454
John Garrya8d547b2016-01-26 02:47:03 +08001455 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001456 sizeof(struct hisi_sas_slot),
1457 GFP_KERNEL);
1458 if (!hisi_hba->slot_info)
1459 goto err_out;
1460
John Garrya8d547b2016-01-26 02:47:03 +08001461 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001462 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1463 GFP_KERNEL);
1464 if (!hisi_hba->iost)
1465 goto err_out;
1466
John Garrya8d547b2016-01-26 02:47:03 +08001467 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001468 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1469 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1470 if (!hisi_hba->breakpoint)
1471 goto err_out;
1472
John Garrya8d547b2016-01-26 02:47:03 +08001473 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001474 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001475 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1476 if (!hisi_hba->slot_index_tags)
1477 goto err_out;
1478
John Garry6be6de12015-11-18 00:50:34 +08001479 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1480 sizeof(struct hisi_sas_sge_page), 16, 0);
1481 if (!hisi_hba->sge_page_pool)
1482 goto err_out;
1483
1484 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1485 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1486 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1487 if (!hisi_hba->initial_fis)
1488 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001489
John Garrya8d547b2016-01-26 02:47:03 +08001490 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry6be6de12015-11-18 00:50:34 +08001491 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1492 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1493 if (!hisi_hba->sata_breakpoint)
1494 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001495 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001496
John Garry257efd12015-11-18 00:50:36 +08001497 hisi_sas_slot_index_init(hisi_hba);
1498
John Garry7e9080e2015-11-18 00:50:40 +08001499 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1500 if (!hisi_hba->wq) {
1501 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1502 goto err_out;
1503 }
1504
John Garry6be6de12015-11-18 00:50:34 +08001505 return 0;
1506err_out:
1507 return -ENOMEM;
1508}
1509
John Garry89d53322015-11-18 00:50:35 +08001510static void hisi_sas_free(struct hisi_hba *hisi_hba)
1511{
1512 struct device *dev = &hisi_hba->pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001513 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001514
1515 for (i = 0; i < hisi_hba->queue_count; i++) {
1516 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1517 if (hisi_hba->cmd_hdr[i])
1518 dma_free_coherent(dev, s,
1519 hisi_hba->cmd_hdr[i],
1520 hisi_hba->cmd_hdr_dma[i]);
1521
1522 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1523 if (hisi_hba->complete_hdr[i])
1524 dma_free_coherent(dev, s,
1525 hisi_hba->complete_hdr[i],
1526 hisi_hba->complete_hdr_dma[i]);
1527 }
1528
1529 dma_pool_destroy(hisi_hba->status_buffer_pool);
1530 dma_pool_destroy(hisi_hba->command_table_pool);
1531 dma_pool_destroy(hisi_hba->sge_page_pool);
1532
1533 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1534 if (hisi_hba->itct)
1535 dma_free_coherent(dev, s,
1536 hisi_hba->itct, hisi_hba->itct_dma);
1537
John Garrya8d547b2016-01-26 02:47:03 +08001538 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001539 if (hisi_hba->iost)
1540 dma_free_coherent(dev, s,
1541 hisi_hba->iost, hisi_hba->iost_dma);
1542
John Garrya8d547b2016-01-26 02:47:03 +08001543 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001544 if (hisi_hba->breakpoint)
1545 dma_free_coherent(dev, s,
1546 hisi_hba->breakpoint,
1547 hisi_hba->breakpoint_dma);
1548
1549
1550 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1551 if (hisi_hba->initial_fis)
1552 dma_free_coherent(dev, s,
1553 hisi_hba->initial_fis,
1554 hisi_hba->initial_fis_dma);
1555
John Garrya8d547b2016-01-26 02:47:03 +08001556 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry89d53322015-11-18 00:50:35 +08001557 if (hisi_hba->sata_breakpoint)
1558 dma_free_coherent(dev, s,
1559 hisi_hba->sata_breakpoint,
1560 hisi_hba->sata_breakpoint_dma);
1561
John Garry7e9080e2015-11-18 00:50:40 +08001562 if (hisi_hba->wq)
1563 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001564}
John Garry6be6de12015-11-18 00:50:34 +08001565
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001566static void hisi_sas_rst_work_handler(struct work_struct *work)
1567{
1568 struct hisi_hba *hisi_hba =
1569 container_of(work, struct hisi_hba, rst_work);
1570
1571 hisi_sas_controller_reset(hisi_hba);
1572}
1573
John Garry7eb78692015-11-18 00:50:31 +08001574static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1575 const struct hisi_sas_hw *hw)
1576{
John Garrye26b2f42015-11-18 00:50:32 +08001577 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08001578 struct Scsi_Host *shost;
1579 struct hisi_hba *hisi_hba;
1580 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +08001581 struct device_node *np = pdev->dev.of_node;
John Garry3bc45af2016-10-04 19:11:11 +08001582 struct clk *refclk;
John Garry7eb78692015-11-18 00:50:31 +08001583
1584 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08001585 if (!shost) {
1586 dev_err(dev, "scsi host alloc failed\n");
1587 return NULL;
1588 }
John Garry7eb78692015-11-18 00:50:31 +08001589 hisi_hba = shost_priv(shost);
1590
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001591 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08001592 hisi_hba->hw = hw;
1593 hisi_hba->pdev = pdev;
1594 hisi_hba->shost = shost;
1595 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1596
John Garryfa42d802015-11-18 00:50:43 +08001597 init_timer(&hisi_hba->timer);
1598
John Garry4d558c72016-02-04 02:26:08 +08001599 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1600 SAS_ADDR_SIZE))
John Garrye26b2f42015-11-18 00:50:32 +08001601 goto err_out;
1602
John Garry4d558c72016-02-04 02:26:08 +08001603 if (np) {
1604 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1605 "hisilicon,sas-syscon");
1606 if (IS_ERR(hisi_hba->ctrl))
1607 goto err_out;
1608
1609 if (device_property_read_u32(dev, "ctrl-reset-reg",
1610 &hisi_hba->ctrl_reset_reg))
1611 goto err_out;
1612
1613 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1614 &hisi_hba->ctrl_reset_sts_reg))
1615 goto err_out;
1616
1617 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1618 &hisi_hba->ctrl_clock_ena_reg))
1619 goto err_out;
1620 }
1621
John Garry3bc45af2016-10-04 19:11:11 +08001622 refclk = devm_clk_get(&pdev->dev, NULL);
1623 if (IS_ERR(refclk))
John Garry87e287c2017-01-20 20:45:20 +08001624 dev_dbg(dev, "no ref clk property\n");
John Garry3bc45af2016-10-04 19:11:11 +08001625 else
1626 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1627
John Garry4d558c72016-02-04 02:26:08 +08001628 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
John Garrye26b2f42015-11-18 00:50:32 +08001629 goto err_out;
1630
John Garry4d558c72016-02-04 02:26:08 +08001631 if (device_property_read_u32(dev, "queue-count",
1632 &hisi_hba->queue_count))
John Garrye26b2f42015-11-18 00:50:32 +08001633 goto err_out;
1634
John Garrya6f2c7f2016-09-06 23:36:19 +08001635 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1636 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1637 dev_err(dev, "No usable DMA addressing method\n");
1638 goto err_out;
1639 }
1640
John Garrye26b2f42015-11-18 00:50:32 +08001641 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1642 hisi_hba->regs = devm_ioremap_resource(dev, res);
1643 if (IS_ERR(hisi_hba->regs))
1644 goto err_out;
1645
John Garry89d53322015-11-18 00:50:35 +08001646 if (hisi_sas_alloc(hisi_hba, shost)) {
1647 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001648 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08001649 }
John Garry6be6de12015-11-18 00:50:34 +08001650
John Garry7eb78692015-11-18 00:50:31 +08001651 return shost;
1652err_out:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001653 kfree(shost);
John Garry7eb78692015-11-18 00:50:31 +08001654 dev_err(dev, "shost alloc failed\n");
1655 return NULL;
1656}
1657
John Garry5d742422015-11-18 00:50:38 +08001658static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1659{
1660 int i;
1661
1662 for (i = 0; i < hisi_hba->n_phy; i++)
1663 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1664 hisi_hba->sas_addr,
1665 SAS_ADDR_SIZE);
1666}
1667
John Garry7eb78692015-11-18 00:50:31 +08001668int hisi_sas_probe(struct platform_device *pdev,
1669 const struct hisi_sas_hw *hw)
1670{
1671 struct Scsi_Host *shost;
1672 struct hisi_hba *hisi_hba;
1673 struct device *dev = &pdev->dev;
1674 struct asd_sas_phy **arr_phy;
1675 struct asd_sas_port **arr_port;
1676 struct sas_ha_struct *sha;
1677 int rc, phy_nr, port_nr, i;
1678
1679 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001680 if (!shost)
1681 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08001682
1683 sha = SHOST_TO_SAS_HA(shost);
1684 hisi_hba = shost_priv(shost);
1685 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08001686
John Garry7eb78692015-11-18 00:50:31 +08001687 phy_nr = port_nr = hisi_hba->n_phy;
1688
1689 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1690 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001691 if (!arr_phy || !arr_port) {
1692 rc = -ENOMEM;
1693 goto err_out_ha;
1694 }
John Garry7eb78692015-11-18 00:50:31 +08001695
1696 sha->sas_phy = arr_phy;
1697 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08001698 sha->lldd_ha = hisi_hba;
1699
1700 shost->transportt = hisi_sas_stt;
1701 shost->max_id = HISI_SAS_MAX_DEVICES;
1702 shost->max_lun = ~0;
1703 shost->max_channel = 1;
1704 shost->max_cmd_len = 16;
1705 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08001706 shost->can_queue = hisi_hba->hw->max_command_entries;
1707 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08001708
1709 sha->sas_ha_name = DRV_NAME;
1710 sha->dev = &hisi_hba->pdev->dev;
1711 sha->lldd_module = THIS_MODULE;
1712 sha->sas_addr = &hisi_hba->sas_addr[0];
1713 sha->num_phys = hisi_hba->n_phy;
1714 sha->core.shost = hisi_hba->shost;
1715
1716 for (i = 0; i < hisi_hba->n_phy; i++) {
1717 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1718 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1719 }
1720
John Garry5d742422015-11-18 00:50:38 +08001721 hisi_sas_init_add(hisi_hba);
1722
John Garry7eb78692015-11-18 00:50:31 +08001723 rc = scsi_add_host(shost, &pdev->dev);
1724 if (rc)
1725 goto err_out_ha;
1726
1727 rc = sas_register_ha(sha);
1728 if (rc)
1729 goto err_out_register_ha;
1730
Xiang Chen0757f042017-01-20 20:45:23 +08001731 rc = hisi_hba->hw->hw_init(hisi_hba);
1732 if (rc)
1733 goto err_out_register_ha;
1734
John Garry7eb78692015-11-18 00:50:31 +08001735 scsi_scan_host(shost);
1736
1737 return 0;
1738
1739err_out_register_ha:
1740 scsi_remove_host(shost);
1741err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001742 hisi_sas_free(hisi_hba);
John Garry7eb78692015-11-18 00:50:31 +08001743 kfree(shost);
1744 return rc;
1745}
1746EXPORT_SYMBOL_GPL(hisi_sas_probe);
1747
John Garry89d53322015-11-18 00:50:35 +08001748int hisi_sas_remove(struct platform_device *pdev)
1749{
1750 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1751 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08001752 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08001753
1754 scsi_remove_host(sha->core.shost);
1755 sas_unregister_ha(sha);
1756 sas_remove_host(sha->core.shost);
1757
1758 hisi_sas_free(hisi_hba);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001759 kfree(shost);
John Garry89d53322015-11-18 00:50:35 +08001760 return 0;
1761}
1762EXPORT_SYMBOL_GPL(hisi_sas_remove);
1763
John Garrye8899fa2015-11-18 00:50:30 +08001764static __init int hisi_sas_init(void)
1765{
1766 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1767
1768 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1769 if (!hisi_sas_stt)
1770 return -ENOMEM;
1771
1772 return 0;
1773}
1774
1775static __exit void hisi_sas_exit(void)
1776{
1777 sas_release_transport(hisi_sas_stt);
1778}
1779
1780module_init(hisi_sas_init);
1781module_exit(hisi_sas_exit);
1782
1783MODULE_VERSION(DRV_VERSION);
1784MODULE_LICENSE("GPL");
1785MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1786MODULE_DESCRIPTION("HISILICON SAS controller driver");
1787MODULE_ALIAS("platform:" DRV_NAME);