blob: 36d4e5a6d2c996f21620a1dd04c634f0eb08d6c6 [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:
John Garryb4c67a62017-03-23 01:25:23 +0800662 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
663 msleep(100);
John Garrye4189d52015-11-18 00:50:57 +0800664 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
John Garrye4189d52015-11-18 00:50:57 +0800665 break;
666
667 case PHY_FUNC_DISABLE:
668 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
669 break;
670
671 case PHY_FUNC_SET_LINK_RATE:
Xiang Chen2ae75782016-11-07 20:48:40 +0800672 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
673 break;
674
John Garrye4189d52015-11-18 00:50:57 +0800675 case PHY_FUNC_RELEASE_SPINUP_HOLD:
676 default:
677 return -EOPNOTSUPP;
678 }
679 return 0;
680}
John Garry184a4632015-11-18 00:50:52 +0800681
John Garry0efff302015-11-18 00:50:56 +0800682static void hisi_sas_task_done(struct sas_task *task)
683{
684 if (!del_timer(&task->slow_task->timer))
685 return;
686 complete(&task->slow_task->completion);
687}
688
689static void hisi_sas_tmf_timedout(unsigned long data)
690{
691 struct sas_task *task = (struct sas_task *)data;
692
693 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
694 complete(&task->slow_task->completion);
695}
696
697#define TASK_TIMEOUT 20
698#define TASK_RETRY 3
699static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
700 void *parameter, u32 para_len,
701 struct hisi_sas_tmf_task *tmf)
702{
703 struct hisi_sas_device *sas_dev = device->lldd_dev;
704 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
705 struct device *dev = &hisi_hba->pdev->dev;
706 struct sas_task *task;
707 int res, retry;
708
709 for (retry = 0; retry < TASK_RETRY; retry++) {
710 task = sas_alloc_slow_task(GFP_KERNEL);
711 if (!task)
712 return -ENOMEM;
713
714 task->dev = device;
715 task->task_proto = device->tproto;
716
Xiang Chen7c594f02017-03-23 01:25:20 +0800717 if (dev_is_sata(device)) {
718 task->ata_task.device_control_reg_update = 1;
719 memcpy(&task->ata_task.fis, parameter, para_len);
720 } else {
721 memcpy(&task->ssp_task, parameter, para_len);
722 }
John Garry0efff302015-11-18 00:50:56 +0800723 task->task_done = hisi_sas_task_done;
724
725 task->slow_task->timer.data = (unsigned long) task;
726 task->slow_task->timer.function = hisi_sas_tmf_timedout;
727 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
728 add_timer(&task->slow_task->timer);
729
730 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
731
732 if (res) {
733 del_timer(&task->slow_task->timer);
734 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
735 res);
736 goto ex_err;
737 }
738
739 wait_for_completion(&task->slow_task->completion);
740 res = TMF_RESP_FUNC_FAILED;
741 /* Even TMF timed out, return direct. */
742 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
743 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Xiang Chen7c594f02017-03-23 01:25:20 +0800744 dev_err(dev, "abort tmf: TMF task timeout\n");
John Garry0efff302015-11-18 00:50:56 +0800745 if (task->lldd_task) {
746 struct hisi_sas_slot *slot =
747 task->lldd_task;
748
749 hisi_sas_slot_task_free(hisi_hba,
750 task, slot);
751 }
752
753 goto ex_err;
754 }
755 }
756
757 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800758 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800759 res = TMF_RESP_FUNC_COMPLETE;
760 break;
761 }
762
763 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800764 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
765 res = TMF_RESP_FUNC_SUCC;
766 break;
767 }
768
769 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800770 task->task_status.stat == SAS_DATA_UNDERRUN) {
771 /* no error, but return the number of bytes of
772 * underrun
773 */
774 dev_warn(dev, "abort tmf: task to dev %016llx "
775 "resp: 0x%x sts 0x%x underrun\n",
776 SAS_ADDR(device->sas_addr),
777 task->task_status.resp,
778 task->task_status.stat);
779 res = task->task_status.residual;
780 break;
781 }
782
783 if (task->task_status.resp == SAS_TASK_COMPLETE &&
784 task->task_status.stat == SAS_DATA_OVERRUN) {
785 dev_warn(dev, "abort tmf: blocked task error\n");
786 res = -EMSGSIZE;
787 break;
788 }
789
790 dev_warn(dev, "abort tmf: task to dev "
791 "%016llx resp: 0x%x status 0x%x\n",
792 SAS_ADDR(device->sas_addr), task->task_status.resp,
793 task->task_status.stat);
794 sas_free_task(task);
795 task = NULL;
796 }
797ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800798 if (retry == TASK_RETRY)
799 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800800 sas_free_task(task);
801 return res;
802}
803
Xiang Chen7c594f02017-03-23 01:25:20 +0800804static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
805 bool reset, int pmp, u8 *fis)
806{
807 struct ata_taskfile tf;
808
809 ata_tf_init(dev, &tf);
810 if (reset)
811 tf.ctl |= ATA_SRST;
812 else
813 tf.ctl &= ~ATA_SRST;
814 tf.command = ATA_CMD_DEV_RESET;
815 ata_tf_to_fis(&tf, pmp, 0, fis);
816}
817
818static int hisi_sas_softreset_ata_disk(struct domain_device *device)
819{
820 u8 fis[20] = {0};
821 struct ata_port *ap = device->sata_dev.ap;
822 struct ata_link *link;
823 int rc = TMF_RESP_FUNC_FAILED;
824 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
825 struct device *dev = &hisi_hba->pdev->dev;
826 int s = sizeof(struct host_to_dev_fis);
827 unsigned long flags;
828
829 ata_for_each_link(link, ap, EDGE) {
830 int pmp = sata_srst_pmp(link);
831
832 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
833 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
834 if (rc != TMF_RESP_FUNC_COMPLETE)
835 break;
836 }
837
838 if (rc == TMF_RESP_FUNC_COMPLETE) {
839 ata_for_each_link(link, ap, EDGE) {
840 int pmp = sata_srst_pmp(link);
841
842 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
843 rc = hisi_sas_exec_internal_tmf_task(device, fis,
844 s, NULL);
845 if (rc != TMF_RESP_FUNC_COMPLETE)
846 dev_err(dev, "ata disk de-reset failed\n");
847 }
848 } else {
849 dev_err(dev, "ata disk reset failed\n");
850 }
851
852 if (rc == TMF_RESP_FUNC_COMPLETE) {
853 spin_lock_irqsave(&hisi_hba->lock, flags);
854 hisi_sas_release_task(hisi_hba, device);
855 spin_unlock_irqrestore(&hisi_hba->lock, flags);
856 }
857
858 return rc;
859}
860
John Garry0efff302015-11-18 00:50:56 +0800861static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
862 u8 *lun, struct hisi_sas_tmf_task *tmf)
863{
864 struct sas_ssp_task ssp_task;
865
866 if (!(device->tproto & SAS_PROTOCOL_SSP))
867 return TMF_RESP_FUNC_ESUPP;
868
869 memcpy(ssp_task.LUN, lun, 8);
870
871 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
872 sizeof(ssp_task), tmf);
873}
874
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800875static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
876{
877 int rc;
878
879 if (!hisi_hba->hw->soft_reset)
880 return -1;
881
882 if (!test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
883 struct device *dev = &hisi_hba->pdev->dev;
884 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
885 unsigned long flags;
886
887 dev_dbg(dev, "controller reset begins!\n");
888 scsi_block_requests(hisi_hba->shost);
889 rc = hisi_hba->hw->soft_reset(hisi_hba);
890 if (rc) {
891 dev_warn(dev, "controller reset failed (%d)\n", rc);
892 goto out;
893 }
894 spin_lock_irqsave(&hisi_hba->lock, flags);
895 hisi_sas_release_tasks(hisi_hba);
896 spin_unlock_irqrestore(&hisi_hba->lock, flags);
897
898 sas_ha->notify_ha_event(sas_ha, HAE_RESET);
899 dev_dbg(dev, "controller reset successful!\n");
900 } else
901 return -1;
902
903out:
904 scsi_unblock_requests(hisi_hba->shost);
905 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
906 return rc;
907}
908
John Garry0efff302015-11-18 00:50:56 +0800909static int hisi_sas_abort_task(struct sas_task *task)
910{
911 struct scsi_lun lun;
912 struct hisi_sas_tmf_task tmf_task;
913 struct domain_device *device = task->dev;
914 struct hisi_sas_device *sas_dev = device->lldd_dev;
915 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
916 struct device *dev = &hisi_hba->pdev->dev;
917 int rc = TMF_RESP_FUNC_FAILED;
918 unsigned long flags;
919
920 if (!sas_dev) {
921 dev_warn(dev, "Device has been removed\n");
922 return TMF_RESP_FUNC_FAILED;
923 }
924
925 spin_lock_irqsave(&task->task_state_lock, flags);
926 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
927 spin_unlock_irqrestore(&task->task_state_lock, flags);
928 rc = TMF_RESP_FUNC_COMPLETE;
929 goto out;
930 }
931
932 spin_unlock_irqrestore(&task->task_state_lock, flags);
933 sas_dev->dev_status = HISI_SAS_DEV_EH;
934 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
935 struct scsi_cmnd *cmnd = task->uldd_task;
936 struct hisi_sas_slot *slot = task->lldd_task;
937 u32 tag = slot->idx;
John Garryc35279f2017-03-23 01:25:25 +0800938 int rc2;
John Garry0efff302015-11-18 00:50:56 +0800939
940 int_to_scsilun(cmnd->device->lun, &lun);
941 tmf_task.tmf = TMF_ABORT_TASK;
942 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
943
944 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
945 &tmf_task);
946
John Garryc35279f2017-03-23 01:25:25 +0800947 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
948 HISI_SAS_INT_ABT_CMD, tag);
949 /*
950 * If the TMF finds that the IO is not in the device and also
951 * the internal abort does not succeed, then it is safe to
952 * free the slot.
953 * Note: if the internal abort succeeds then the slot
954 * will have already been completed
955 */
956 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
John Garry0efff302015-11-18 00:50:56 +0800957 if (task->lldd_task) {
John Garry0efff302015-11-18 00:50:56 +0800958 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garryc35279f2017-03-23 01:25:25 +0800959 hisi_sas_do_release_task(hisi_hba, task, slot);
John Garry0efff302015-11-18 00:50:56 +0800960 spin_unlock_irqrestore(&hisi_hba->lock, flags);
961 }
962 }
John Garry0efff302015-11-18 00:50:56 +0800963 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
964 task->task_proto & SAS_PROTOCOL_STP) {
965 if (task->dev->dev_type == SAS_SATA_DEV) {
John Garrydc8a49c2016-08-24 19:05:49 +0800966 hisi_sas_internal_task_abort(hisi_hba, device,
967 HISI_SAS_INT_ABT_DEV, 0);
Xiang Chen7c594f02017-03-23 01:25:20 +0800968 rc = hisi_sas_softreset_ata_disk(device);
John Garry0efff302015-11-18 00:50:56 +0800969 }
John Garrydc8a49c2016-08-24 19:05:49 +0800970 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
971 /* SMP */
972 struct hisi_sas_slot *slot = task->lldd_task;
973 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +0800974
John Garrydc8a49c2016-08-24 19:05:49 +0800975 hisi_sas_internal_task_abort(hisi_hba, device,
976 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800977 }
978
979out:
980 if (rc != TMF_RESP_FUNC_COMPLETE)
981 dev_notice(dev, "abort task: rc=%d\n", rc);
982 return rc;
983}
984
985static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
986{
987 struct hisi_sas_tmf_task tmf_task;
988 int rc = TMF_RESP_FUNC_FAILED;
989
990 tmf_task.tmf = TMF_ABORT_TASK_SET;
991 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
992
993 return rc;
994}
995
996static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
997{
998 int rc = TMF_RESP_FUNC_FAILED;
999 struct hisi_sas_tmf_task tmf_task;
1000
1001 tmf_task.tmf = TMF_CLEAR_ACA;
1002 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1003
1004 return rc;
1005}
1006
1007static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1008{
1009 struct sas_phy *phy = sas_get_local_phy(device);
1010 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1011 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1012 rc = sas_phy_reset(phy, reset_type);
1013 sas_put_local_phy(phy);
1014 msleep(2000);
1015 return rc;
1016}
1017
1018static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1019{
1020 struct hisi_sas_device *sas_dev = device->lldd_dev;
1021 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1022 unsigned long flags;
1023 int rc = TMF_RESP_FUNC_FAILED;
1024
1025 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1026 return TMF_RESP_FUNC_FAILED;
1027 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1028
1029 rc = hisi_sas_debug_I_T_nexus_reset(device);
1030
1031 spin_lock_irqsave(&hisi_hba->lock, flags);
1032 hisi_sas_release_task(hisi_hba, device);
1033 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1034
1035 return 0;
1036}
1037
1038static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1039{
1040 struct hisi_sas_tmf_task tmf_task;
1041 struct hisi_sas_device *sas_dev = device->lldd_dev;
1042 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1043 struct device *dev = &hisi_hba->pdev->dev;
1044 unsigned long flags;
1045 int rc = TMF_RESP_FUNC_FAILED;
1046
1047 tmf_task.tmf = TMF_LU_RESET;
1048 sas_dev->dev_status = HISI_SAS_DEV_EH;
1049 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1050 if (rc == TMF_RESP_FUNC_COMPLETE) {
1051 spin_lock_irqsave(&hisi_hba->lock, flags);
1052 hisi_sas_release_task(hisi_hba, device);
1053 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1054 }
1055
1056 /* If failed, fall-through I_T_Nexus reset */
1057 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
1058 sas_dev->device_id, rc);
1059 return rc;
1060}
1061
1062static int hisi_sas_query_task(struct sas_task *task)
1063{
1064 struct scsi_lun lun;
1065 struct hisi_sas_tmf_task tmf_task;
1066 int rc = TMF_RESP_FUNC_FAILED;
1067
1068 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1069 struct scsi_cmnd *cmnd = task->uldd_task;
1070 struct domain_device *device = task->dev;
1071 struct hisi_sas_slot *slot = task->lldd_task;
1072 u32 tag = slot->idx;
1073
1074 int_to_scsilun(cmnd->device->lun, &lun);
1075 tmf_task.tmf = TMF_QUERY_TASK;
1076 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1077
1078 rc = hisi_sas_debug_issue_ssp_tmf(device,
1079 lun.scsi_lun,
1080 &tmf_task);
1081 switch (rc) {
1082 /* The task is still in Lun, release it then */
1083 case TMF_RESP_FUNC_SUCC:
1084 /* The task is not in Lun or failed, reset the phy */
1085 case TMF_RESP_FUNC_FAILED:
1086 case TMF_RESP_FUNC_COMPLETE:
1087 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001088 default:
1089 rc = TMF_RESP_FUNC_FAILED;
1090 break;
John Garry0efff302015-11-18 00:50:56 +08001091 }
1092 }
1093 return rc;
1094}
1095
John Garry441c2742016-08-24 19:05:47 +08001096static int
1097hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
1098 struct sas_task *task, int abort_flag,
1099 int task_tag)
1100{
1101 struct domain_device *device = task->dev;
1102 struct hisi_sas_device *sas_dev = device->lldd_dev;
1103 struct device *dev = &hisi_hba->pdev->dev;
1104 struct hisi_sas_port *port;
1105 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001106 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001107 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1108 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1109
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001110 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
1111 return -EINVAL;
1112
John Garry441c2742016-08-24 19:05:47 +08001113 if (!device->port)
1114 return -1;
1115
John Garry2e244f02017-03-23 01:25:17 +08001116 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001117
1118 /* simply get a slot and send abort command */
1119 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1120 if (rc)
1121 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +08001122 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
1123 &dlvry_queue, &dlvry_queue_slot);
John Garry441c2742016-08-24 19:05:47 +08001124 if (rc)
1125 goto err_out_tag;
1126
1127 slot = &hisi_hba->slot_info[slot_idx];
1128 memset(slot, 0, sizeof(struct hisi_sas_slot));
1129
1130 slot->idx = slot_idx;
1131 slot->n_elem = n_elem;
1132 slot->dlvry_queue = dlvry_queue;
1133 slot->dlvry_queue_slot = dlvry_queue_slot;
1134 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1135 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1136 slot->task = task;
1137 slot->port = port;
1138 task->lldd_task = slot;
1139
1140 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1141
1142 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1143 abort_flag, task_tag);
1144 if (rc)
1145 goto err_out_tag;
1146
John Garry405314d2017-03-23 01:25:21 +08001147
1148 list_add_tail(&slot->entry, &sas_dev->list);
John Garry441c2742016-08-24 19:05:47 +08001149 spin_lock(&task->task_state_lock);
1150 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1151 spin_unlock(&task->task_state_lock);
1152
1153 hisi_hba->slot_prep = slot;
1154
John Garryf696cc32016-11-07 20:48:39 +08001155 atomic64_inc(&sas_dev->running_req);
1156
John Garry441c2742016-08-24 19:05:47 +08001157 /* send abort command to our chip */
1158 hisi_hba->hw->start_delivery(hisi_hba);
1159
1160 return 0;
1161
1162err_out_tag:
1163 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1164err_out:
1165 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1166
1167 return rc;
1168}
1169
1170/**
1171 * hisi_sas_internal_task_abort -- execute an internal
1172 * abort command for single IO command or a device
1173 * @hisi_hba: host controller struct
1174 * @device: domain device
1175 * @abort_flag: mode of operation, device or single IO
1176 * @tag: tag of IO to be aborted (only relevant to single
1177 * IO mode)
1178 */
1179static int
1180hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1181 struct domain_device *device,
1182 int abort_flag, int tag)
1183{
1184 struct sas_task *task;
1185 struct hisi_sas_device *sas_dev = device->lldd_dev;
1186 struct device *dev = &hisi_hba->pdev->dev;
1187 int res;
1188 unsigned long flags;
1189
1190 if (!hisi_hba->hw->prep_abort)
1191 return -EOPNOTSUPP;
1192
1193 task = sas_alloc_slow_task(GFP_KERNEL);
1194 if (!task)
1195 return -ENOMEM;
1196
1197 task->dev = device;
1198 task->task_proto = device->tproto;
1199 task->task_done = hisi_sas_task_done;
1200 task->slow_task->timer.data = (unsigned long)task;
1201 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1202 task->slow_task->timer.expires = jiffies + 20*HZ;
1203 add_timer(&task->slow_task->timer);
1204
1205 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1206 spin_lock_irqsave(&hisi_hba->lock, flags);
1207 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1208 task, abort_flag, tag);
1209 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1210 if (res) {
1211 del_timer(&task->slow_task->timer);
1212 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1213 res);
1214 goto exit;
1215 }
1216 wait_for_completion(&task->slow_task->completion);
1217 res = TMF_RESP_FUNC_FAILED;
1218
1219 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1220 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1221 res = TMF_RESP_FUNC_COMPLETE;
1222 goto exit;
1223 }
1224
John Garryc35279f2017-03-23 01:25:25 +08001225 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1226 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1227 res = TMF_RESP_FUNC_SUCC;
1228 goto exit;
1229 }
1230
John Garry441c2742016-08-24 19:05:47 +08001231 /* TMF timed out, return direct. */
1232 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1233 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1234 dev_err(dev, "internal task abort: timeout.\n");
1235 if (task->lldd_task) {
1236 struct hisi_sas_slot *slot = task->lldd_task;
1237
1238 hisi_sas_slot_task_free(hisi_hba, task, slot);
1239 }
1240 }
1241 }
1242
1243exit:
John Garry297d7302017-01-20 20:45:22 +08001244 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001245 "resp: 0x%x sts 0x%x\n",
1246 SAS_ADDR(device->sas_addr),
1247 task,
1248 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1249 task->task_status.stat);
1250 sas_free_task(task);
1251
1252 return res;
1253}
1254
John Garry184a4632015-11-18 00:50:52 +08001255static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1256{
1257 hisi_sas_port_notify_formed(sas_phy);
1258}
1259
John Garry184a4632015-11-18 00:50:52 +08001260static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1261{
1262 phy->phy_attached = 0;
1263 phy->phy_type = 0;
1264 phy->port = NULL;
1265}
1266
1267void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1268{
1269 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1270 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1271 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1272
1273 if (rdy) {
1274 /* Phy down but ready */
1275 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1276 hisi_sas_port_notify_formed(sas_phy);
1277 } else {
1278 struct hisi_sas_port *port = phy->port;
1279
1280 /* Phy down and not ready */
1281 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1282 sas_phy_disconnected(sas_phy);
1283
1284 if (port) {
1285 if (phy->phy_type & PORT_TYPE_SAS) {
1286 int port_id = port->id;
1287
1288 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1289 port_id))
1290 port->port_attached = 0;
1291 } else if (phy->phy_type & PORT_TYPE_SATA)
1292 port->port_attached = 0;
1293 }
1294 hisi_sas_phy_disconnected(phy);
1295 }
1296}
1297EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1298
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001299void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1300 u32 state)
1301{
1302 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1303 int phy_no;
1304
1305 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1306 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1307 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1308 struct asd_sas_port *sas_port = sas_phy->port;
1309 struct domain_device *dev;
1310
1311 if (sas_phy->enabled) {
1312 /* Report PHY state change to libsas */
1313 if (state & (1 << phy_no))
1314 continue;
1315
1316 if (old_state & (1 << phy_no))
1317 /* PHY down but was up before */
1318 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1319 }
1320 if (!sas_port)
1321 continue;
1322 dev = sas_port->port_dev;
1323
1324 if (DEV_IS_EXPANDER(dev->dev_type))
1325 sas_ha->notify_phy_event(sas_phy, PORTE_BROADCAST_RCVD);
1326 }
1327}
1328EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology);
1329
John Garrye8899fa2015-11-18 00:50:30 +08001330static struct scsi_transport_template *hisi_sas_stt;
1331
John Garry7eb78692015-11-18 00:50:31 +08001332static struct scsi_host_template hisi_sas_sht = {
1333 .module = THIS_MODULE,
1334 .name = DRV_NAME,
1335 .queuecommand = sas_queuecommand,
1336 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001337 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001338 .scan_finished = hisi_sas_scan_finished,
1339 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001340 .change_queue_depth = sas_change_queue_depth,
1341 .bios_param = sas_bios_param,
1342 .can_queue = 1,
1343 .this_id = -1,
1344 .sg_tablesize = SG_ALL,
1345 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1346 .use_clustering = ENABLE_CLUSTERING,
1347 .eh_device_reset_handler = sas_eh_device_reset_handler,
1348 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1349 .target_destroy = sas_target_destroy,
1350 .ioctl = sas_ioctl,
1351};
1352
John Garrye8899fa2015-11-18 00:50:30 +08001353static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001354 .lldd_dev_found = hisi_sas_dev_found,
1355 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001356 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001357 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001358 .lldd_abort_task = hisi_sas_abort_task,
1359 .lldd_abort_task_set = hisi_sas_abort_task_set,
1360 .lldd_clear_aca = hisi_sas_clear_aca,
1361 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1362 .lldd_lu_reset = hisi_sas_lu_reset,
1363 .lldd_query_task = hisi_sas_query_task,
John Garry184a4632015-11-18 00:50:52 +08001364 .lldd_port_formed = hisi_sas_port_formed,
John Garrye8899fa2015-11-18 00:50:30 +08001365};
1366
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001367void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1368{
1369 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1370
1371 for (i = 0; i < hisi_hba->queue_count; i++) {
1372 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1373 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1374
1375 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1376 memset(hisi_hba->cmd_hdr[i], 0, s);
1377 dq->wr_point = 0;
1378
1379 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1380 memset(hisi_hba->complete_hdr[i], 0, s);
1381 cq->rd_point = 0;
1382 }
1383
1384 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1385 memset(hisi_hba->initial_fis, 0, s);
1386
1387 s = max_command_entries * sizeof(struct hisi_sas_iost);
1388 memset(hisi_hba->iost, 0, s);
1389
1390 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1391 memset(hisi_hba->breakpoint, 0, s);
1392
1393 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1394 memset(hisi_hba->sata_breakpoint, 0, s);
1395}
1396EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1397
John Garry6be6de12015-11-18 00:50:34 +08001398static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1399{
John Garry6be6de12015-11-18 00:50:34 +08001400 struct platform_device *pdev = hisi_hba->pdev;
1401 struct device *dev = &pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001402 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001403
John Garryfa42d802015-11-18 00:50:43 +08001404 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001405 for (i = 0; i < hisi_hba->n_phy; i++) {
1406 hisi_sas_phy_init(hisi_hba, i);
1407 hisi_hba->port[i].port_attached = 0;
1408 hisi_hba->port[i].id = -1;
John Garry976867e2015-11-18 00:50:42 +08001409 }
1410
John Garryaf740db2015-11-18 00:50:41 +08001411 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1412 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1413 hisi_hba->devices[i].device_id = i;
1414 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1415 }
1416
John Garry6be6de12015-11-18 00:50:34 +08001417 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001418 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001419 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001420
1421 /* Completion queue structure */
1422 cq->id = i;
1423 cq->hisi_hba = hisi_hba;
1424
John Garry4fde02a2016-09-06 23:36:12 +08001425 /* Delivery queue structure */
1426 dq->id = i;
1427 dq->hisi_hba = hisi_hba;
1428
John Garry6be6de12015-11-18 00:50:34 +08001429 /* Delivery queue */
1430 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1431 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1432 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1433 if (!hisi_hba->cmd_hdr[i])
1434 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001435
1436 /* Completion queue */
1437 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1438 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1439 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1440 if (!hisi_hba->complete_hdr[i])
1441 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001442 }
1443
1444 s = HISI_SAS_STATUS_BUF_SZ;
1445 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1446 dev, s, 16, 0);
1447 if (!hisi_hba->status_buffer_pool)
1448 goto err_out;
1449
1450 s = HISI_SAS_COMMAND_TABLE_SZ;
1451 hisi_hba->command_table_pool = dma_pool_create("command_table",
1452 dev, s, 16, 0);
1453 if (!hisi_hba->command_table_pool)
1454 goto err_out;
1455
1456 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1457 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1458 GFP_KERNEL);
1459 if (!hisi_hba->itct)
1460 goto err_out;
1461
1462 memset(hisi_hba->itct, 0, s);
1463
John Garrya8d547b2016-01-26 02:47:03 +08001464 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001465 sizeof(struct hisi_sas_slot),
1466 GFP_KERNEL);
1467 if (!hisi_hba->slot_info)
1468 goto err_out;
1469
John Garrya8d547b2016-01-26 02:47:03 +08001470 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001471 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1472 GFP_KERNEL);
1473 if (!hisi_hba->iost)
1474 goto err_out;
1475
John Garrya8d547b2016-01-26 02:47:03 +08001476 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001477 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1478 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1479 if (!hisi_hba->breakpoint)
1480 goto err_out;
1481
John Garrya8d547b2016-01-26 02:47:03 +08001482 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001483 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001484 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1485 if (!hisi_hba->slot_index_tags)
1486 goto err_out;
1487
John Garry6be6de12015-11-18 00:50:34 +08001488 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1489 sizeof(struct hisi_sas_sge_page), 16, 0);
1490 if (!hisi_hba->sge_page_pool)
1491 goto err_out;
1492
1493 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1494 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1495 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1496 if (!hisi_hba->initial_fis)
1497 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001498
John Garrya8d547b2016-01-26 02:47:03 +08001499 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry6be6de12015-11-18 00:50:34 +08001500 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1501 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1502 if (!hisi_hba->sata_breakpoint)
1503 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001504 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001505
John Garry257efd12015-11-18 00:50:36 +08001506 hisi_sas_slot_index_init(hisi_hba);
1507
John Garry7e9080e2015-11-18 00:50:40 +08001508 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1509 if (!hisi_hba->wq) {
1510 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1511 goto err_out;
1512 }
1513
John Garry6be6de12015-11-18 00:50:34 +08001514 return 0;
1515err_out:
1516 return -ENOMEM;
1517}
1518
John Garry89d53322015-11-18 00:50:35 +08001519static void hisi_sas_free(struct hisi_hba *hisi_hba)
1520{
1521 struct device *dev = &hisi_hba->pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001522 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001523
1524 for (i = 0; i < hisi_hba->queue_count; i++) {
1525 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1526 if (hisi_hba->cmd_hdr[i])
1527 dma_free_coherent(dev, s,
1528 hisi_hba->cmd_hdr[i],
1529 hisi_hba->cmd_hdr_dma[i]);
1530
1531 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1532 if (hisi_hba->complete_hdr[i])
1533 dma_free_coherent(dev, s,
1534 hisi_hba->complete_hdr[i],
1535 hisi_hba->complete_hdr_dma[i]);
1536 }
1537
1538 dma_pool_destroy(hisi_hba->status_buffer_pool);
1539 dma_pool_destroy(hisi_hba->command_table_pool);
1540 dma_pool_destroy(hisi_hba->sge_page_pool);
1541
1542 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1543 if (hisi_hba->itct)
1544 dma_free_coherent(dev, s,
1545 hisi_hba->itct, hisi_hba->itct_dma);
1546
John Garrya8d547b2016-01-26 02:47:03 +08001547 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001548 if (hisi_hba->iost)
1549 dma_free_coherent(dev, s,
1550 hisi_hba->iost, hisi_hba->iost_dma);
1551
John Garrya8d547b2016-01-26 02:47:03 +08001552 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001553 if (hisi_hba->breakpoint)
1554 dma_free_coherent(dev, s,
1555 hisi_hba->breakpoint,
1556 hisi_hba->breakpoint_dma);
1557
1558
1559 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1560 if (hisi_hba->initial_fis)
1561 dma_free_coherent(dev, s,
1562 hisi_hba->initial_fis,
1563 hisi_hba->initial_fis_dma);
1564
John Garrya8d547b2016-01-26 02:47:03 +08001565 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry89d53322015-11-18 00:50:35 +08001566 if (hisi_hba->sata_breakpoint)
1567 dma_free_coherent(dev, s,
1568 hisi_hba->sata_breakpoint,
1569 hisi_hba->sata_breakpoint_dma);
1570
John Garry7e9080e2015-11-18 00:50:40 +08001571 if (hisi_hba->wq)
1572 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001573}
John Garry6be6de12015-11-18 00:50:34 +08001574
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001575static void hisi_sas_rst_work_handler(struct work_struct *work)
1576{
1577 struct hisi_hba *hisi_hba =
1578 container_of(work, struct hisi_hba, rst_work);
1579
1580 hisi_sas_controller_reset(hisi_hba);
1581}
1582
John Garry7eb78692015-11-18 00:50:31 +08001583static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1584 const struct hisi_sas_hw *hw)
1585{
John Garrye26b2f42015-11-18 00:50:32 +08001586 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08001587 struct Scsi_Host *shost;
1588 struct hisi_hba *hisi_hba;
1589 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +08001590 struct device_node *np = pdev->dev.of_node;
John Garry3bc45af2016-10-04 19:11:11 +08001591 struct clk *refclk;
John Garry7eb78692015-11-18 00:50:31 +08001592
1593 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08001594 if (!shost) {
1595 dev_err(dev, "scsi host alloc failed\n");
1596 return NULL;
1597 }
John Garry7eb78692015-11-18 00:50:31 +08001598 hisi_hba = shost_priv(shost);
1599
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001600 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08001601 hisi_hba->hw = hw;
1602 hisi_hba->pdev = pdev;
1603 hisi_hba->shost = shost;
1604 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1605
John Garryfa42d802015-11-18 00:50:43 +08001606 init_timer(&hisi_hba->timer);
1607
John Garry4d558c72016-02-04 02:26:08 +08001608 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1609 SAS_ADDR_SIZE))
John Garrye26b2f42015-11-18 00:50:32 +08001610 goto err_out;
1611
John Garry4d558c72016-02-04 02:26:08 +08001612 if (np) {
1613 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1614 "hisilicon,sas-syscon");
1615 if (IS_ERR(hisi_hba->ctrl))
1616 goto err_out;
1617
1618 if (device_property_read_u32(dev, "ctrl-reset-reg",
1619 &hisi_hba->ctrl_reset_reg))
1620 goto err_out;
1621
1622 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1623 &hisi_hba->ctrl_reset_sts_reg))
1624 goto err_out;
1625
1626 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1627 &hisi_hba->ctrl_clock_ena_reg))
1628 goto err_out;
1629 }
1630
John Garry3bc45af2016-10-04 19:11:11 +08001631 refclk = devm_clk_get(&pdev->dev, NULL);
1632 if (IS_ERR(refclk))
John Garry87e287c2017-01-20 20:45:20 +08001633 dev_dbg(dev, "no ref clk property\n");
John Garry3bc45af2016-10-04 19:11:11 +08001634 else
1635 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1636
John Garry4d558c72016-02-04 02:26:08 +08001637 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
John Garrye26b2f42015-11-18 00:50:32 +08001638 goto err_out;
1639
John Garry4d558c72016-02-04 02:26:08 +08001640 if (device_property_read_u32(dev, "queue-count",
1641 &hisi_hba->queue_count))
John Garrye26b2f42015-11-18 00:50:32 +08001642 goto err_out;
1643
John Garrya6f2c7f2016-09-06 23:36:19 +08001644 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1645 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1646 dev_err(dev, "No usable DMA addressing method\n");
1647 goto err_out;
1648 }
1649
John Garrye26b2f42015-11-18 00:50:32 +08001650 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1651 hisi_hba->regs = devm_ioremap_resource(dev, res);
1652 if (IS_ERR(hisi_hba->regs))
1653 goto err_out;
1654
John Garry89d53322015-11-18 00:50:35 +08001655 if (hisi_sas_alloc(hisi_hba, shost)) {
1656 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001657 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08001658 }
John Garry6be6de12015-11-18 00:50:34 +08001659
John Garry7eb78692015-11-18 00:50:31 +08001660 return shost;
1661err_out:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001662 kfree(shost);
John Garry7eb78692015-11-18 00:50:31 +08001663 dev_err(dev, "shost alloc failed\n");
1664 return NULL;
1665}
1666
John Garry5d742422015-11-18 00:50:38 +08001667static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1668{
1669 int i;
1670
1671 for (i = 0; i < hisi_hba->n_phy; i++)
1672 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1673 hisi_hba->sas_addr,
1674 SAS_ADDR_SIZE);
1675}
1676
John Garry7eb78692015-11-18 00:50:31 +08001677int hisi_sas_probe(struct platform_device *pdev,
1678 const struct hisi_sas_hw *hw)
1679{
1680 struct Scsi_Host *shost;
1681 struct hisi_hba *hisi_hba;
1682 struct device *dev = &pdev->dev;
1683 struct asd_sas_phy **arr_phy;
1684 struct asd_sas_port **arr_port;
1685 struct sas_ha_struct *sha;
1686 int rc, phy_nr, port_nr, i;
1687
1688 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001689 if (!shost)
1690 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08001691
1692 sha = SHOST_TO_SAS_HA(shost);
1693 hisi_hba = shost_priv(shost);
1694 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08001695
John Garry7eb78692015-11-18 00:50:31 +08001696 phy_nr = port_nr = hisi_hba->n_phy;
1697
1698 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1699 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001700 if (!arr_phy || !arr_port) {
1701 rc = -ENOMEM;
1702 goto err_out_ha;
1703 }
John Garry7eb78692015-11-18 00:50:31 +08001704
1705 sha->sas_phy = arr_phy;
1706 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08001707 sha->lldd_ha = hisi_hba;
1708
1709 shost->transportt = hisi_sas_stt;
1710 shost->max_id = HISI_SAS_MAX_DEVICES;
1711 shost->max_lun = ~0;
1712 shost->max_channel = 1;
1713 shost->max_cmd_len = 16;
1714 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08001715 shost->can_queue = hisi_hba->hw->max_command_entries;
1716 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08001717
1718 sha->sas_ha_name = DRV_NAME;
1719 sha->dev = &hisi_hba->pdev->dev;
1720 sha->lldd_module = THIS_MODULE;
1721 sha->sas_addr = &hisi_hba->sas_addr[0];
1722 sha->num_phys = hisi_hba->n_phy;
1723 sha->core.shost = hisi_hba->shost;
1724
1725 for (i = 0; i < hisi_hba->n_phy; i++) {
1726 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1727 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1728 }
1729
John Garry5d742422015-11-18 00:50:38 +08001730 hisi_sas_init_add(hisi_hba);
1731
John Garry7eb78692015-11-18 00:50:31 +08001732 rc = scsi_add_host(shost, &pdev->dev);
1733 if (rc)
1734 goto err_out_ha;
1735
1736 rc = sas_register_ha(sha);
1737 if (rc)
1738 goto err_out_register_ha;
1739
Xiang Chen0757f042017-01-20 20:45:23 +08001740 rc = hisi_hba->hw->hw_init(hisi_hba);
1741 if (rc)
1742 goto err_out_register_ha;
1743
John Garry7eb78692015-11-18 00:50:31 +08001744 scsi_scan_host(shost);
1745
1746 return 0;
1747
1748err_out_register_ha:
1749 scsi_remove_host(shost);
1750err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001751 hisi_sas_free(hisi_hba);
John Garry7eb78692015-11-18 00:50:31 +08001752 kfree(shost);
1753 return rc;
1754}
1755EXPORT_SYMBOL_GPL(hisi_sas_probe);
1756
John Garry89d53322015-11-18 00:50:35 +08001757int hisi_sas_remove(struct platform_device *pdev)
1758{
1759 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1760 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08001761 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08001762
1763 scsi_remove_host(sha->core.shost);
1764 sas_unregister_ha(sha);
1765 sas_remove_host(sha->core.shost);
1766
1767 hisi_sas_free(hisi_hba);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001768 kfree(shost);
John Garry89d53322015-11-18 00:50:35 +08001769 return 0;
1770}
1771EXPORT_SYMBOL_GPL(hisi_sas_remove);
1772
John Garrye8899fa2015-11-18 00:50:30 +08001773static __init int hisi_sas_init(void)
1774{
1775 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1776
1777 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1778 if (!hisi_sas_stt)
1779 return -ENOMEM;
1780
1781 return 0;
1782}
1783
1784static __exit void hisi_sas_exit(void)
1785{
1786 sas_release_transport(hisi_sas_stt);
1787}
1788
1789module_init(hisi_sas_init);
1790module_exit(hisi_sas_exit);
1791
1792MODULE_VERSION(DRV_VERSION);
1793MODULE_LICENSE("GPL");
1794MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1795MODULE_DESCRIPTION("HISILICON SAS controller driver");
1796MODULE_ALIAS("platform:" DRV_NAME);