blob: 326dc816907356e50237cb41f32698542e919e81 [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
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080026u8 hisi_sas_get_ata_protocol(u8 cmd, int direction)
27{
28 switch (cmd) {
29 case ATA_CMD_FPDMA_WRITE:
30 case ATA_CMD_FPDMA_READ:
31 case ATA_CMD_FPDMA_RECV:
32 case ATA_CMD_FPDMA_SEND:
33 case ATA_CMD_NCQ_NON_DATA:
34 return HISI_SAS_SATA_PROTOCOL_FPDMA;
35
36 case ATA_CMD_DOWNLOAD_MICRO:
37 case ATA_CMD_ID_ATA:
38 case ATA_CMD_PMP_READ:
39 case ATA_CMD_READ_LOG_EXT:
40 case ATA_CMD_PIO_READ:
41 case ATA_CMD_PIO_READ_EXT:
42 case ATA_CMD_PMP_WRITE:
43 case ATA_CMD_WRITE_LOG_EXT:
44 case ATA_CMD_PIO_WRITE:
45 case ATA_CMD_PIO_WRITE_EXT:
46 return HISI_SAS_SATA_PROTOCOL_PIO;
47
48 case ATA_CMD_DSM:
49 case ATA_CMD_DOWNLOAD_MICRO_DMA:
50 case ATA_CMD_PMP_READ_DMA:
51 case ATA_CMD_PMP_WRITE_DMA:
52 case ATA_CMD_READ:
53 case ATA_CMD_READ_EXT:
54 case ATA_CMD_READ_LOG_DMA_EXT:
55 case ATA_CMD_READ_STREAM_DMA_EXT:
56 case ATA_CMD_TRUSTED_RCV_DMA:
57 case ATA_CMD_TRUSTED_SND_DMA:
58 case ATA_CMD_WRITE:
59 case ATA_CMD_WRITE_EXT:
60 case ATA_CMD_WRITE_FUA_EXT:
61 case ATA_CMD_WRITE_QUEUED:
62 case ATA_CMD_WRITE_LOG_DMA_EXT:
63 case ATA_CMD_WRITE_STREAM_DMA_EXT:
Xiaofei Tanc3fe8a2b2017-08-11 00:09:34 +080064 case ATA_CMD_ZAC_MGMT_IN:
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080065 return HISI_SAS_SATA_PROTOCOL_DMA;
66
67 case ATA_CMD_CHK_POWER:
68 case ATA_CMD_DEV_RESET:
69 case ATA_CMD_EDD:
70 case ATA_CMD_FLUSH:
71 case ATA_CMD_FLUSH_EXT:
72 case ATA_CMD_VERIFY:
73 case ATA_CMD_VERIFY_EXT:
74 case ATA_CMD_SET_FEATURES:
75 case ATA_CMD_STANDBY:
76 case ATA_CMD_STANDBYNOW1:
Xiaofei Tanc3fe8a2b2017-08-11 00:09:34 +080077 case ATA_CMD_ZAC_MGMT_OUT:
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080078 return HISI_SAS_SATA_PROTOCOL_NONDATA;
79 default:
80 if (direction == DMA_NONE)
81 return HISI_SAS_SATA_PROTOCOL_NONDATA;
82 return HISI_SAS_SATA_PROTOCOL_PIO;
83 }
84}
85EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
86
Xiang Chen75904072017-06-14 23:33:15 +080087void hisi_sas_sata_done(struct sas_task *task,
88 struct hisi_sas_slot *slot)
89{
90 struct task_status_struct *ts = &task->task_status;
91 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
Xiaofei Tanf557e322017-06-29 21:02:14 +080092 struct hisi_sas_status_buffer *status_buf =
93 hisi_sas_status_buf_addr_mem(slot);
94 u8 *iu = &status_buf->iu[0];
95 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
Xiang Chen75904072017-06-14 23:33:15 +080096
97 resp->frame_len = sizeof(struct dev_to_host_fis);
98 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
99
100 ts->buf_valid_size = sizeof(*resp);
101}
102EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
103
Xiang Chen318913c2017-06-14 23:33:16 +0800104int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
105{
106 struct ata_queued_cmd *qc = task->uldd_task;
107
108 if (qc) {
109 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
110 qc->tf.command == ATA_CMD_FPDMA_READ) {
111 *tag = qc->tag;
112 return 1;
113 }
114 }
115 return 0;
116}
117EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
118
John Garry42e7a692015-11-18 00:50:49 +0800119static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
120{
121 return device->port->ha->lldd_ha;
122}
123
John Garry2e244f02017-03-23 01:25:17 +0800124struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
125{
126 return container_of(sas_port, struct hisi_sas_port, sas_port);
127}
128EXPORT_SYMBOL_GPL(to_hisi_sas_port);
129
Xiang Chena25d0d32017-08-11 00:09:40 +0800130void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
131{
132 int phy_no;
133
134 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
135 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
136}
137EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
138
John Garry257efd12015-11-18 00:50:36 +0800139static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
140{
141 void *bitmap = hisi_hba->slot_index_tags;
142
143 clear_bit(slot_idx, bitmap);
144}
145
John Garry42e7a692015-11-18 00:50:49 +0800146static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
147{
148 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
149}
150
151static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
152{
153 void *bitmap = hisi_hba->slot_index_tags;
154
155 set_bit(slot_idx, bitmap);
156}
157
158static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
159{
160 unsigned int index;
161 void *bitmap = hisi_hba->slot_index_tags;
162
163 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
164 if (index >= hisi_hba->slot_index_count)
165 return -SAS_QUEUE_FULL;
166 hisi_sas_slot_index_set(hisi_hba, index);
167 *slot_idx = index;
168 return 0;
169}
170
John Garry257efd12015-11-18 00:50:36 +0800171static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
172{
173 int i;
174
175 for (i = 0; i < hisi_hba->slot_index_count; ++i)
176 hisi_sas_slot_index_clear(hisi_hba, i);
177}
John Garry27a3f222015-11-18 00:50:50 +0800178
179void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
180 struct hisi_sas_slot *slot)
181{
John Garry27a3f222015-11-18 00:50:50 +0800182
John Garryd3c4dd42017-04-10 21:22:00 +0800183 if (task) {
John Garry11b75242017-06-14 23:33:17 +0800184 struct device *dev = hisi_hba->dev;
John Garryd3c4dd42017-04-10 21:22:00 +0800185 struct domain_device *device = task->dev;
186 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry27a3f222015-11-18 00:50:50 +0800187
Xiaofei Tan6ba0fbc2017-10-24 23:51:38 +0800188 if (!task->lldd_task)
189 return;
190
191 task->lldd_task = NULL;
192
John Garryd3c4dd42017-04-10 21:22:00 +0800193 if (!sas_protocol_ata(task->task_proto))
194 if (slot->n_elem)
Xiang Chendc1e4732017-12-09 01:16:33 +0800195 dma_unmap_sg(dev, task->scatter,
196 task->num_scatter,
John Garryd3c4dd42017-04-10 21:22:00 +0800197 task->data_dir);
198
John Garryd3c4dd42017-04-10 21:22:00 +0800199 if (sas_dev)
200 atomic64_dec(&sas_dev->running_req);
201 }
John Garry27a3f222015-11-18 00:50:50 +0800202
Xiaofei Tanf557e322017-06-29 21:02:14 +0800203 if (slot->buf)
204 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
John Garry27a3f222015-11-18 00:50:50 +0800205
John Garry27a3f222015-11-18 00:50:50 +0800206 list_del_init(&slot->entry);
Xiaofei Tan6ba0fbc2017-10-24 23:51:38 +0800207 slot->buf = NULL;
John Garry27a3f222015-11-18 00:50:50 +0800208 slot->task = NULL;
209 slot->port = NULL;
210 hisi_sas_slot_index_free(hisi_hba, slot->idx);
John Garryd3c4dd42017-04-10 21:22:00 +0800211
John Garry59ba49f2016-09-06 23:36:14 +0800212 /* slot memory is fully zeroed when it is reused */
John Garry27a3f222015-11-18 00:50:50 +0800213}
214EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
215
John Garry66ee9992015-11-18 00:50:54 +0800216static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
217 struct hisi_sas_slot *slot)
218{
219 return hisi_hba->hw->prep_smp(hisi_hba, slot);
220}
221
John Garry42e7a692015-11-18 00:50:49 +0800222static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
223 struct hisi_sas_slot *slot, int is_tmf,
224 struct hisi_sas_tmf_task *tmf)
225{
226 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
227}
228
John Garry6f2ff1a2016-01-26 02:47:20 +0800229static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
230 struct hisi_sas_slot *slot)
231{
232 return hisi_hba->hw->prep_stp(hisi_hba, slot);
233}
234
John Garry441c2742016-08-24 19:05:47 +0800235static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
236 struct hisi_sas_slot *slot,
237 int device_id, int abort_flag, int tag_to_abort)
238{
239 return hisi_hba->hw->prep_abort(hisi_hba, slot,
240 device_id, abort_flag, tag_to_abort);
241}
242
John Garrycac9b2a2016-02-25 17:42:11 +0800243/*
244 * This function will issue an abort TMF regardless of whether the
245 * task is in the sdev or not. Then it will do the task complete
246 * cleanup and callbacks.
247 */
248static void hisi_sas_slot_abort(struct work_struct *work)
249{
250 struct hisi_sas_slot *abort_slot =
251 container_of(work, struct hisi_sas_slot, abort_slot);
252 struct sas_task *task = abort_slot->task;
253 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
254 struct scsi_cmnd *cmnd = task->uldd_task;
255 struct hisi_sas_tmf_task tmf_task;
John Garrycac9b2a2016-02-25 17:42:11 +0800256 struct scsi_lun lun;
John Garry11b75242017-06-14 23:33:17 +0800257 struct device *dev = hisi_hba->dev;
John Garrycac9b2a2016-02-25 17:42:11 +0800258 int tag = abort_slot->idx;
John Garryda7b66e2017-01-03 20:24:50 +0800259 unsigned long flags;
John Garrycac9b2a2016-02-25 17:42:11 +0800260
261 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
262 dev_err(dev, "cannot abort slot for non-ssp task\n");
263 goto out;
264 }
265
266 int_to_scsilun(cmnd->device->lun, &lun);
267 tmf_task.tmf = TMF_ABORT_TASK;
268 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
269
270 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
271out:
272 /* Do cleanup for this task */
John Garryda7b66e2017-01-03 20:24:50 +0800273 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800274 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
John Garryda7b66e2017-01-03 20:24:50 +0800275 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800276 if (task->task_done)
277 task->task_done(task);
John Garrycac9b2a2016-02-25 17:42:11 +0800278}
279
Xiang Chenb1a49412017-06-14 23:33:13 +0800280static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
281 *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
282 int *pass)
John Garry42e7a692015-11-18 00:50:49 +0800283{
Xiang Chenb1a49412017-06-14 23:33:13 +0800284 struct hisi_hba *hisi_hba = dq->hisi_hba;
John Garry42e7a692015-11-18 00:50:49 +0800285 struct domain_device *device = task->dev;
286 struct hisi_sas_device *sas_dev = device->lldd_dev;
287 struct hisi_sas_port *port;
288 struct hisi_sas_slot *slot;
289 struct hisi_sas_cmd_hdr *cmd_hdr_base;
John Garry2e244f02017-03-23 01:25:17 +0800290 struct asd_sas_port *sas_port = device->port;
John Garry11b75242017-06-14 23:33:17 +0800291 struct device *dev = hisi_hba->dev;
John Garry42e7a692015-11-18 00:50:49 +0800292 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
John Garry54c9dd22017-03-23 01:25:29 +0800293 unsigned long flags;
John Garry42e7a692015-11-18 00:50:49 +0800294
John Garry2e244f02017-03-23 01:25:17 +0800295 if (!sas_port) {
John Garry42e7a692015-11-18 00:50:49 +0800296 struct task_status_struct *ts = &task->task_status;
297
298 ts->resp = SAS_TASK_UNDELIVERED;
299 ts->stat = SAS_PHY_DOWN;
300 /*
301 * libsas will use dev->port, should
302 * not call task_done for sata
303 */
304 if (device->dev_type != SAS_SATA_DEV)
305 task->task_done(task);
John Garryddabca22017-03-23 01:25:22 +0800306 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800307 }
308
309 if (DEV_IS_GONE(sas_dev)) {
310 if (sas_dev)
John Garryad604832017-06-14 23:33:12 +0800311 dev_info(dev, "task prep: device %d not ready\n",
John Garry42e7a692015-11-18 00:50:49 +0800312 sas_dev->device_id);
313 else
314 dev_info(dev, "task prep: device %016llx not ready\n",
315 SAS_ADDR(device->sas_addr));
316
John Garryddabca22017-03-23 01:25:22 +0800317 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800318 }
John Garry2e244f02017-03-23 01:25:17 +0800319
320 port = to_hisi_sas_port(sas_port);
John Garry9859f242016-08-24 19:05:52 +0800321 if (port && !port->port_attached) {
John Garry09fe9ec2016-09-06 23:36:18 +0800322 dev_info(dev, "task prep: %s port%d not attach device\n",
Xiang Chen6073b772017-03-23 01:25:38 +0800323 (dev_is_sata(device)) ?
John Garry09fe9ec2016-09-06 23:36:18 +0800324 "SATA/STP" : "SAS",
325 device->port->id);
John Garry42e7a692015-11-18 00:50:49 +0800326
John Garry09fe9ec2016-09-06 23:36:18 +0800327 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800328 }
329
330 if (!sas_protocol_ata(task->task_proto)) {
331 if (task->num_scatter) {
332 n_elem = dma_map_sg(dev, task->scatter,
333 task->num_scatter, task->data_dir);
334 if (!n_elem) {
335 rc = -ENOMEM;
336 goto prep_out;
337 }
338 }
339 } else
340 n_elem = task->num_scatter;
341
Xiang Chenb1a49412017-06-14 23:33:13 +0800342 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry685b6d62016-04-15 21:36:36 +0800343 if (hisi_hba->hw->slot_index_alloc)
344 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
345 device);
346 else
347 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +0800348 if (rc) {
349 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800350 goto err_out;
Xiang Chenb1a49412017-06-14 23:33:13 +0800351 }
352 spin_unlock_irqrestore(&hisi_hba->lock, flags);
353
354 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
John Garry42e7a692015-11-18 00:50:49 +0800355 if (rc)
356 goto err_out_tag;
357
Xiang Chenb1a49412017-06-14 23:33:13 +0800358 dlvry_queue = dq->id;
359 dlvry_queue_slot = dq->wr_point;
John Garry42e7a692015-11-18 00:50:49 +0800360 slot = &hisi_hba->slot_info[slot_idx];
361 memset(slot, 0, sizeof(struct hisi_sas_slot));
362
363 slot->idx = slot_idx;
364 slot->n_elem = n_elem;
365 slot->dlvry_queue = dlvry_queue;
366 slot->dlvry_queue_slot = dlvry_queue_slot;
367 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
368 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
369 slot->task = task;
370 slot->port = port;
371 task->lldd_task = slot;
John Garrycac9b2a2016-02-25 17:42:11 +0800372 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
John Garry42e7a692015-11-18 00:50:49 +0800373
Xiaofei Tanf557e322017-06-29 21:02:14 +0800374 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
375 GFP_ATOMIC, &slot->buf_dma);
376 if (!slot->buf) {
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300377 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800378 goto err_out_slot_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300379 }
John Garry42e7a692015-11-18 00:50:49 +0800380 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
Xiaofei Tanf557e322017-06-29 21:02:14 +0800381 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
382 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
John Garry42e7a692015-11-18 00:50:49 +0800383
384 switch (task->task_proto) {
John Garry66ee9992015-11-18 00:50:54 +0800385 case SAS_PROTOCOL_SMP:
386 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
387 break;
John Garry42e7a692015-11-18 00:50:49 +0800388 case SAS_PROTOCOL_SSP:
389 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
390 break;
391 case SAS_PROTOCOL_SATA:
392 case SAS_PROTOCOL_STP:
393 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
John Garry6f2ff1a2016-01-26 02:47:20 +0800394 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
395 break;
John Garry42e7a692015-11-18 00:50:49 +0800396 default:
397 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
398 task->task_proto);
399 rc = -EINVAL;
400 break;
401 }
402
403 if (rc) {
404 dev_err(dev, "task prep: rc = 0x%x\n", rc);
Xiaofei Tanf557e322017-06-29 21:02:14 +0800405 goto err_out_buf;
John Garry42e7a692015-11-18 00:50:49 +0800406 }
407
Xiang Chen9feaf902017-10-24 23:51:34 +0800408 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry405314d2017-03-23 01:25:21 +0800409 list_add_tail(&slot->entry, &sas_dev->list);
Xiang Chen9feaf902017-10-24 23:51:34 +0800410 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry54c9dd22017-03-23 01:25:29 +0800411 spin_lock_irqsave(&task->task_state_lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800412 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
John Garry54c9dd22017-03-23 01:25:29 +0800413 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800414
Xiang Chenb1a49412017-06-14 23:33:13 +0800415 dq->slot_prep = slot;
John Garry42e7a692015-11-18 00:50:49 +0800416
John Garryf696cc32016-11-07 20:48:39 +0800417 atomic64_inc(&sas_dev->running_req);
John Garry42e7a692015-11-18 00:50:49 +0800418 ++(*pass);
419
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300420 return 0;
John Garry42e7a692015-11-18 00:50:49 +0800421
Xiaofei Tanf557e322017-06-29 21:02:14 +0800422err_out_buf:
423 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
424 slot->buf_dma);
John Garry42e7a692015-11-18 00:50:49 +0800425err_out_slot_buf:
426 /* Nothing to be done */
427err_out_tag:
Xiang Chenb1a49412017-06-14 23:33:13 +0800428 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800429 hisi_sas_slot_index_free(hisi_hba, slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +0800430 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800431err_out:
432 dev_err(dev, "task prep: failed[%d]!\n", rc);
433 if (!sas_protocol_ata(task->task_proto))
434 if (n_elem)
Xiang Chendc1e4732017-12-09 01:16:33 +0800435 dma_unmap_sg(dev, task->scatter,
436 task->num_scatter,
John Garry42e7a692015-11-18 00:50:49 +0800437 task->data_dir);
438prep_out:
439 return rc;
440}
441
442static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
443 int is_tmf, struct hisi_sas_tmf_task *tmf)
444{
445 u32 rc;
446 u32 pass = 0;
447 unsigned long flags;
448 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
John Garry11b75242017-06-14 23:33:17 +0800449 struct device *dev = hisi_hba->dev;
Xiang Chenb1a49412017-06-14 23:33:13 +0800450 struct domain_device *device = task->dev;
451 struct hisi_sas_device *sas_dev = device->lldd_dev;
452 struct hisi_sas_dq *dq = sas_dev->dq;
John Garry42e7a692015-11-18 00:50:49 +0800453
Xiaofei Tan917d3bd2017-08-11 00:09:26 +0800454 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800455 return -EINVAL;
456
John Garry42e7a692015-11-18 00:50:49 +0800457 /* protect task_prep and start_delivery sequence */
Xiang Chenb1a49412017-06-14 23:33:13 +0800458 spin_lock_irqsave(&dq->lock, flags);
459 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
John Garry42e7a692015-11-18 00:50:49 +0800460 if (rc)
461 dev_err(dev, "task exec: failed[%d]!\n", rc);
462
463 if (likely(pass))
Xiang Chenb1a49412017-06-14 23:33:13 +0800464 hisi_hba->hw->start_delivery(dq);
465 spin_unlock_irqrestore(&dq->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800466
467 return rc;
468}
John Garry257efd12015-11-18 00:50:36 +0800469
John Garry66139922015-11-18 00:50:48 +0800470static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
471{
472 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
473 struct asd_sas_phy *sas_phy = &phy->sas_phy;
474 struct sas_ha_struct *sas_ha;
475
476 if (!phy->phy_attached)
477 return;
478
479 sas_ha = &hisi_hba->sha;
480 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
481
482 if (sas_phy->phy) {
483 struct sas_phy *sphy = sas_phy->phy;
484
485 sphy->negotiated_linkrate = sas_phy->linkrate;
John Garry66139922015-11-18 00:50:48 +0800486 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
Xiang Chen2ae75782016-11-07 20:48:40 +0800487 sphy->maximum_linkrate_hw =
488 hisi_hba->hw->phy_get_max_linkrate();
489 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
490 sphy->minimum_linkrate = phy->minimum_linkrate;
491
492 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
493 sphy->maximum_linkrate = phy->maximum_linkrate;
John Garry66139922015-11-18 00:50:48 +0800494 }
495
496 if (phy->phy_type & PORT_TYPE_SAS) {
497 struct sas_identify_frame *id;
498
499 id = (struct sas_identify_frame *)phy->frame_rcvd;
500 id->dev_type = phy->identify.device_type;
501 id->initiator_bits = SAS_PROTOCOL_ALL;
502 id->target_bits = phy->identify.target_port_protocols;
503 } else if (phy->phy_type & PORT_TYPE_SATA) {
504 /*Nothing*/
505 }
506
507 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
508 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
509}
510
John Garryabda97c2015-11-18 00:50:51 +0800511static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
512{
513 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
514 struct hisi_sas_device *sas_dev = NULL;
Xiaofei Tan302e0902017-10-24 23:51:33 +0800515 unsigned long flags;
John Garryabda97c2015-11-18 00:50:51 +0800516 int i;
517
Xiaofei Tan302e0902017-10-24 23:51:33 +0800518 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garryabda97c2015-11-18 00:50:51 +0800519 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
520 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
Xiang Chenb1a49412017-06-14 23:33:13 +0800521 int queue = i % hisi_hba->queue_count;
522 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
523
John Garryabda97c2015-11-18 00:50:51 +0800524 hisi_hba->devices[i].device_id = i;
525 sas_dev = &hisi_hba->devices[i];
526 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
527 sas_dev->dev_type = device->dev_type;
528 sas_dev->hisi_hba = hisi_hba;
529 sas_dev->sas_device = device;
Xiang Chenb1a49412017-06-14 23:33:13 +0800530 sas_dev->dq = dq;
John Garry405314d2017-03-23 01:25:21 +0800531 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
John Garryabda97c2015-11-18 00:50:51 +0800532 break;
533 }
534 }
Xiaofei Tan302e0902017-10-24 23:51:33 +0800535 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garryabda97c2015-11-18 00:50:51 +0800536
537 return sas_dev;
538}
539
540static int hisi_sas_dev_found(struct domain_device *device)
541{
542 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
543 struct domain_device *parent_dev = device->parent;
544 struct hisi_sas_device *sas_dev;
John Garry11b75242017-06-14 23:33:17 +0800545 struct device *dev = hisi_hba->dev;
John Garryabda97c2015-11-18 00:50:51 +0800546
John Garry685b6d62016-04-15 21:36:36 +0800547 if (hisi_hba->hw->alloc_dev)
548 sas_dev = hisi_hba->hw->alloc_dev(device);
549 else
550 sas_dev = hisi_sas_alloc_dev(device);
John Garryabda97c2015-11-18 00:50:51 +0800551 if (!sas_dev) {
552 dev_err(dev, "fail alloc dev: max support %d devices\n",
553 HISI_SAS_MAX_DEVICES);
554 return -EINVAL;
555 }
556
557 device->lldd_dev = sas_dev;
558 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
559
560 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
561 int phy_no;
562 u8 phy_num = parent_dev->ex_dev.num_phys;
563 struct ex_phy *phy;
564
565 for (phy_no = 0; phy_no < phy_num; phy_no++) {
566 phy = &parent_dev->ex_dev.ex_phy[phy_no];
567 if (SAS_ADDR(phy->attached_sas_addr) ==
568 SAS_ADDR(device->sas_addr)) {
569 sas_dev->attached_phy = phy_no;
570 break;
571 }
572 }
573
574 if (phy_no == phy_num) {
575 dev_info(dev, "dev found: no attached "
576 "dev:%016llx at ex:%016llx\n",
577 SAS_ADDR(device->sas_addr),
578 SAS_ADDR(parent_dev->sas_addr));
579 return -EINVAL;
580 }
581 }
582
Xiang Chenf1c88212017-12-09 01:16:41 +0800583 dev_info(dev, "dev[%d:%x] found\n",
584 sas_dev->device_id, sas_dev->dev_type);
585
John Garryabda97c2015-11-18 00:50:51 +0800586 return 0;
587}
588
John Garry31eec8a2016-02-25 17:42:14 +0800589static int hisi_sas_slave_configure(struct scsi_device *sdev)
590{
591 struct domain_device *dev = sdev_to_domain_dev(sdev);
592 int ret = sas_slave_configure(sdev);
593
594 if (ret)
595 return ret;
596 if (!dev_is_sata(dev))
597 sas_change_queue_depth(sdev, 64);
598
599 return 0;
600}
601
John Garry701f75e2015-11-18 00:50:55 +0800602static void hisi_sas_scan_start(struct Scsi_Host *shost)
603{
604 struct hisi_hba *hisi_hba = shost_priv(shost);
John Garry701f75e2015-11-18 00:50:55 +0800605
John Garry396b8042017-03-23 01:25:19 +0800606 hisi_hba->hw->phys_init(hisi_hba);
John Garry701f75e2015-11-18 00:50:55 +0800607}
608
609static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
610{
611 struct hisi_hba *hisi_hba = shost_priv(shost);
612 struct sas_ha_struct *sha = &hisi_hba->sha;
613
John Garry396b8042017-03-23 01:25:19 +0800614 /* Wait for PHY up interrupt to occur */
615 if (time < HZ)
John Garry701f75e2015-11-18 00:50:55 +0800616 return 0;
617
618 sas_drain_work(sha);
619 return 1;
620}
621
John Garry66139922015-11-18 00:50:48 +0800622static void hisi_sas_phyup_work(struct work_struct *work)
623{
624 struct hisi_sas_phy *phy =
Xiaofei Tane537b622017-12-09 01:16:44 +0800625 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
John Garry66139922015-11-18 00:50:48 +0800626 struct hisi_hba *hisi_hba = phy->hisi_hba;
627 struct asd_sas_phy *sas_phy = &phy->sas_phy;
628 int phy_no = sas_phy->id;
629
630 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
631 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
632}
John Garry976867e2015-11-18 00:50:42 +0800633
Xiaofei Tane537b622017-12-09 01:16:44 +0800634static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
635 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
636};
637
638bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
639 enum hisi_sas_phy_event event)
640{
641 struct hisi_hba *hisi_hba = phy->hisi_hba;
642
643 if (WARN_ON(event >= HISI_PHYES_NUM))
644 return false;
645
646 return queue_work(hisi_hba->wq, &phy->works[event]);
647}
648EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
649
John Garry976867e2015-11-18 00:50:42 +0800650static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
651{
652 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
653 struct asd_sas_phy *sas_phy = &phy->sas_phy;
Xiaofei Tane537b622017-12-09 01:16:44 +0800654 int i;
John Garry976867e2015-11-18 00:50:42 +0800655
656 phy->hisi_hba = hisi_hba;
657 phy->port = NULL;
John Garry976867e2015-11-18 00:50:42 +0800658 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
659 sas_phy->class = SAS;
660 sas_phy->iproto = SAS_PROTOCOL_ALL;
661 sas_phy->tproto = 0;
662 sas_phy->type = PHY_TYPE_PHYSICAL;
663 sas_phy->role = PHY_ROLE_INITIATOR;
664 sas_phy->oob_mode = OOB_NOT_CONNECTED;
665 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
666 sas_phy->id = phy_no;
667 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
668 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
669 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
670 sas_phy->lldd_phy = phy;
John Garry66139922015-11-18 00:50:48 +0800671
Xiaofei Tane537b622017-12-09 01:16:44 +0800672 for (i = 0; i < HISI_PHYES_NUM; i++)
673 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
John Garry976867e2015-11-18 00:50:42 +0800674}
675
John Garry184a4632015-11-18 00:50:52 +0800676static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
677{
678 struct sas_ha_struct *sas_ha = sas_phy->ha;
679 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
680 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
681 struct asd_sas_port *sas_port = sas_phy->port;
John Garry2e244f02017-03-23 01:25:17 +0800682 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
John Garry184a4632015-11-18 00:50:52 +0800683 unsigned long flags;
684
685 if (!sas_port)
686 return;
687
688 spin_lock_irqsave(&hisi_hba->lock, flags);
689 port->port_attached = 1;
690 port->id = phy->port_id;
691 phy->port = port;
692 sas_port->lldd_port = port;
693 spin_unlock_irqrestore(&hisi_hba->lock, flags);
694}
695
John Garryd3c4dd42017-04-10 21:22:00 +0800696static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
John Garry405314d2017-03-23 01:25:21 +0800697 struct hisi_sas_slot *slot)
John Garry184a4632015-11-18 00:50:52 +0800698{
John Garryd3c4dd42017-04-10 21:22:00 +0800699 if (task) {
700 unsigned long flags;
701 struct task_status_struct *ts;
John Garry184a4632015-11-18 00:50:52 +0800702
John Garryd3c4dd42017-04-10 21:22:00 +0800703 ts = &task->task_status;
John Garry184a4632015-11-18 00:50:52 +0800704
John Garryd3c4dd42017-04-10 21:22:00 +0800705 ts->resp = SAS_TASK_COMPLETE;
706 ts->stat = SAS_ABORTED_TASK;
707 spin_lock_irqsave(&task->task_state_lock, flags);
708 task->task_state_flags &=
709 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
710 task->task_state_flags |= SAS_TASK_STATE_DONE;
711 spin_unlock_irqrestore(&task->task_state_lock, flags);
712 }
John Garry184a4632015-11-18 00:50:52 +0800713
John Garry405314d2017-03-23 01:25:21 +0800714 hisi_sas_slot_task_free(hisi_hba, task, slot);
John Garry184a4632015-11-18 00:50:52 +0800715}
716
John Garry405314d2017-03-23 01:25:21 +0800717/* hisi_hba.lock should be locked */
John Garry184a4632015-11-18 00:50:52 +0800718static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
719 struct domain_device *device)
720{
John Garry405314d2017-03-23 01:25:21 +0800721 struct hisi_sas_slot *slot, *slot2;
722 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry184a4632015-11-18 00:50:52 +0800723
John Garry405314d2017-03-23 01:25:21 +0800724 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
725 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
John Garry184a4632015-11-18 00:50:52 +0800726}
727
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800728static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
729{
John Garry405314d2017-03-23 01:25:21 +0800730 struct hisi_sas_device *sas_dev;
731 struct domain_device *device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800732 int i;
733
John Garry405314d2017-03-23 01:25:21 +0800734 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
735 sas_dev = &hisi_hba->devices[i];
736 device = sas_dev->sas_device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800737
John Garry405314d2017-03-23 01:25:21 +0800738 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
739 !device)
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800740 continue;
John Garry405314d2017-03-23 01:25:21 +0800741
742 hisi_sas_release_task(hisi_hba, device);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800743 }
744}
745
Xiang Chend30ff262017-06-14 23:33:32 +0800746static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
747 struct domain_device *device)
748{
749 if (hisi_hba->hw->dereg_device)
750 hisi_hba->hw->dereg_device(hisi_hba, device);
751}
752
John Garryabda97c2015-11-18 00:50:51 +0800753static void hisi_sas_dev_gone(struct domain_device *device)
754{
755 struct hisi_sas_device *sas_dev = device->lldd_dev;
756 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
John Garry11b75242017-06-14 23:33:17 +0800757 struct device *dev = hisi_hba->dev;
John Garryabda97c2015-11-18 00:50:51 +0800758
Xiang Chenf1c88212017-12-09 01:16:41 +0800759 dev_info(dev, "dev[%d:%x] is gone\n",
John Garryabda97c2015-11-18 00:50:51 +0800760 sas_dev->device_id, sas_dev->dev_type);
761
Xiang Chenf8e45ec2017-12-09 01:16:37 +0800762 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
763 hisi_sas_internal_task_abort(hisi_hba, device,
John Garry40f27022016-08-24 19:05:48 +0800764 HISI_SAS_INT_ABT_DEV, 0);
765
Xiang Chenf8e45ec2017-12-09 01:16:37 +0800766 hisi_sas_dereg_device(hisi_hba, device);
Xiang Chend30ff262017-06-14 23:33:32 +0800767
Xiang Chenf8e45ec2017-12-09 01:16:37 +0800768 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
769 device->lldd_dev = NULL;
770 memset(sas_dev, 0, sizeof(*sas_dev));
771 }
772
Xiaofei Tan02581412017-12-09 01:16:34 +0800773 if (hisi_hba->hw->free_device)
774 hisi_hba->hw->free_device(sas_dev);
John Garryabda97c2015-11-18 00:50:51 +0800775 sas_dev->dev_type = SAS_PHY_UNUSED;
John Garryabda97c2015-11-18 00:50:51 +0800776}
John Garry42e7a692015-11-18 00:50:49 +0800777
778static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
779{
780 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
781}
782
John Garrye4189d52015-11-18 00:50:57 +0800783static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
784 void *funcdata)
785{
786 struct sas_ha_struct *sas_ha = sas_phy->ha;
787 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
788 int phy_no = sas_phy->id;
789
790 switch (func) {
791 case PHY_FUNC_HARD_RESET:
792 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
793 break;
794
795 case PHY_FUNC_LINK_RESET:
John Garryb4c67a62017-03-23 01:25:23 +0800796 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
797 msleep(100);
Xiang Chen1eb8eea2017-10-24 23:51:36 +0800798 hisi_hba->hw->phy_start(hisi_hba, phy_no);
John Garrye4189d52015-11-18 00:50:57 +0800799 break;
800
801 case PHY_FUNC_DISABLE:
802 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
803 break;
804
805 case PHY_FUNC_SET_LINK_RATE:
Xiang Chen2ae75782016-11-07 20:48:40 +0800806 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
807 break;
Xiaofei Tanc52108c2017-08-11 00:09:29 +0800808 case PHY_FUNC_GET_EVENTS:
809 if (hisi_hba->hw->get_events) {
810 hisi_hba->hw->get_events(hisi_hba, phy_no);
811 break;
812 }
813 /* fallthru */
John Garrye4189d52015-11-18 00:50:57 +0800814 case PHY_FUNC_RELEASE_SPINUP_HOLD:
815 default:
816 return -EOPNOTSUPP;
817 }
818 return 0;
819}
John Garry184a4632015-11-18 00:50:52 +0800820
John Garry0efff302015-11-18 00:50:56 +0800821static void hisi_sas_task_done(struct sas_task *task)
822{
823 if (!del_timer(&task->slow_task->timer))
824 return;
825 complete(&task->slow_task->completion);
826}
827
Kees Cook77570ee2017-08-22 16:05:14 -0700828static void hisi_sas_tmf_timedout(struct timer_list *t)
John Garry0efff302015-11-18 00:50:56 +0800829{
Kees Cook77570ee2017-08-22 16:05:14 -0700830 struct sas_task_slow *slow = from_timer(slow, t, timer);
831 struct sas_task *task = slow->task;
Xiang Chenf64a6982017-06-14 23:33:11 +0800832 unsigned long flags;
John Garry0efff302015-11-18 00:50:56 +0800833
Xiang Chenf64a6982017-06-14 23:33:11 +0800834 spin_lock_irqsave(&task->task_state_lock, flags);
835 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
836 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
837 spin_unlock_irqrestore(&task->task_state_lock, flags);
838
John Garry0efff302015-11-18 00:50:56 +0800839 complete(&task->slow_task->completion);
840}
841
842#define TASK_TIMEOUT 20
843#define TASK_RETRY 3
844static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
845 void *parameter, u32 para_len,
846 struct hisi_sas_tmf_task *tmf)
847{
848 struct hisi_sas_device *sas_dev = device->lldd_dev;
849 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
John Garry11b75242017-06-14 23:33:17 +0800850 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +0800851 struct sas_task *task;
852 int res, retry;
853
854 for (retry = 0; retry < TASK_RETRY; retry++) {
855 task = sas_alloc_slow_task(GFP_KERNEL);
856 if (!task)
857 return -ENOMEM;
858
859 task->dev = device;
860 task->task_proto = device->tproto;
861
Xiang Chen7c594f02017-03-23 01:25:20 +0800862 if (dev_is_sata(device)) {
863 task->ata_task.device_control_reg_update = 1;
864 memcpy(&task->ata_task.fis, parameter, para_len);
865 } else {
866 memcpy(&task->ssp_task, parameter, para_len);
867 }
John Garry0efff302015-11-18 00:50:56 +0800868 task->task_done = hisi_sas_task_done;
869
Kees Cook841b86f2017-10-23 09:40:42 +0200870 task->slow_task->timer.function = hisi_sas_tmf_timedout;
John Garry0efff302015-11-18 00:50:56 +0800871 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
872 add_timer(&task->slow_task->timer);
873
874 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
875
876 if (res) {
877 del_timer(&task->slow_task->timer);
878 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
879 res);
880 goto ex_err;
881 }
882
883 wait_for_completion(&task->slow_task->completion);
884 res = TMF_RESP_FUNC_FAILED;
885 /* Even TMF timed out, return direct. */
886 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
887 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
John Garryd3c4dd42017-04-10 21:22:00 +0800888 struct hisi_sas_slot *slot = task->lldd_task;
889
Xiang Chenf1c88212017-12-09 01:16:41 +0800890 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
John Garryd3c4dd42017-04-10 21:22:00 +0800891 if (slot)
892 slot->task = NULL;
893
John Garry0efff302015-11-18 00:50:56 +0800894 goto ex_err;
Xiang Chenf1c88212017-12-09 01:16:41 +0800895 } else
896 dev_err(dev, "abort tmf: TMF task timeout\n");
John Garry0efff302015-11-18 00:50:56 +0800897 }
898
899 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800900 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800901 res = TMF_RESP_FUNC_COMPLETE;
902 break;
903 }
904
905 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800906 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
907 res = TMF_RESP_FUNC_SUCC;
908 break;
909 }
910
911 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800912 task->task_status.stat == SAS_DATA_UNDERRUN) {
913 /* no error, but return the number of bytes of
914 * underrun
915 */
916 dev_warn(dev, "abort tmf: task to dev %016llx "
917 "resp: 0x%x sts 0x%x underrun\n",
918 SAS_ADDR(device->sas_addr),
919 task->task_status.resp,
920 task->task_status.stat);
921 res = task->task_status.residual;
922 break;
923 }
924
925 if (task->task_status.resp == SAS_TASK_COMPLETE &&
926 task->task_status.stat == SAS_DATA_OVERRUN) {
927 dev_warn(dev, "abort tmf: blocked task error\n");
928 res = -EMSGSIZE;
929 break;
930 }
931
932 dev_warn(dev, "abort tmf: task to dev "
933 "%016llx resp: 0x%x status 0x%x\n",
934 SAS_ADDR(device->sas_addr), task->task_status.resp,
935 task->task_status.stat);
936 sas_free_task(task);
937 task = NULL;
938 }
939ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800940 if (retry == TASK_RETRY)
941 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800942 sas_free_task(task);
943 return res;
944}
945
Xiang Chen7c594f02017-03-23 01:25:20 +0800946static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
947 bool reset, int pmp, u8 *fis)
948{
949 struct ata_taskfile tf;
950
951 ata_tf_init(dev, &tf);
952 if (reset)
953 tf.ctl |= ATA_SRST;
954 else
955 tf.ctl &= ~ATA_SRST;
956 tf.command = ATA_CMD_DEV_RESET;
957 ata_tf_to_fis(&tf, pmp, 0, fis);
958}
959
960static int hisi_sas_softreset_ata_disk(struct domain_device *device)
961{
962 u8 fis[20] = {0};
963 struct ata_port *ap = device->sata_dev.ap;
964 struct ata_link *link;
965 int rc = TMF_RESP_FUNC_FAILED;
966 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
John Garry11b75242017-06-14 23:33:17 +0800967 struct device *dev = hisi_hba->dev;
Xiang Chen7c594f02017-03-23 01:25:20 +0800968 int s = sizeof(struct host_to_dev_fis);
969 unsigned long flags;
970
971 ata_for_each_link(link, ap, EDGE) {
972 int pmp = sata_srst_pmp(link);
973
974 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
975 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
976 if (rc != TMF_RESP_FUNC_COMPLETE)
977 break;
978 }
979
980 if (rc == TMF_RESP_FUNC_COMPLETE) {
981 ata_for_each_link(link, ap, EDGE) {
982 int pmp = sata_srst_pmp(link);
983
984 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
985 rc = hisi_sas_exec_internal_tmf_task(device, fis,
986 s, NULL);
987 if (rc != TMF_RESP_FUNC_COMPLETE)
988 dev_err(dev, "ata disk de-reset failed\n");
989 }
990 } else {
991 dev_err(dev, "ata disk reset failed\n");
992 }
993
994 if (rc == TMF_RESP_FUNC_COMPLETE) {
995 spin_lock_irqsave(&hisi_hba->lock, flags);
996 hisi_sas_release_task(hisi_hba, device);
997 spin_unlock_irqrestore(&hisi_hba->lock, flags);
998 }
999
1000 return rc;
1001}
1002
John Garry0efff302015-11-18 00:50:56 +08001003static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1004 u8 *lun, struct hisi_sas_tmf_task *tmf)
1005{
1006 struct sas_ssp_task ssp_task;
1007
1008 if (!(device->tproto & SAS_PROTOCOL_SSP))
1009 return TMF_RESP_FUNC_ESUPP;
1010
1011 memcpy(ssp_task.LUN, lun, 8);
1012
1013 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1014 sizeof(ssp_task), tmf);
1015}
1016
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001017static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001018{
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001019 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001020 int i;
1021
1022 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001023 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1024 struct domain_device *device = sas_dev->sas_device;
1025 struct asd_sas_port *sas_port;
1026 struct hisi_sas_port *port;
1027 struct hisi_sas_phy *phy = NULL;
1028 struct asd_sas_phy *sas_phy;
1029
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001030 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001031 || !device || !device->port)
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001032 continue;
1033
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001034 sas_port = device->port;
1035 port = to_hisi_sas_port(sas_port);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001036
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001037 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1038 if (state & BIT(sas_phy->id)) {
1039 phy = sas_phy->lldd_phy;
1040 break;
1041 }
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001042
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001043 if (phy) {
1044 port->id = phy->port_id;
1045
1046 /* Update linkrate of directly attached device. */
1047 if (!device->parent)
1048 device->linkrate = phy->sas_phy.linkrate;
1049
1050 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1051 } else
1052 port->id = 0xff;
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001053 }
1054}
1055
1056static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1057 u32 state)
1058{
1059 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1060 struct asd_sas_port *_sas_port = NULL;
1061 int phy_no;
1062
1063 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1064 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1065 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1066 struct asd_sas_port *sas_port = sas_phy->port;
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001067 bool do_port_check = !!(_sas_port != sas_port);
1068
1069 if (!sas_phy->phy->enabled)
1070 continue;
1071
1072 /* Report PHY state change to libsas */
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001073 if (state & BIT(phy_no)) {
1074 if (do_port_check && sas_port && sas_port->port_dev) {
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001075 struct domain_device *dev = sas_port->port_dev;
1076
1077 _sas_port = sas_port;
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001078
1079 if (DEV_IS_EXPANDER(dev->dev_type))
1080 sas_ha->notify_port_event(sas_phy,
1081 PORTE_BROADCAST_RCVD);
1082 }
1083 } else if (old_state & (1 << phy_no))
1084 /* PHY down but was up before */
1085 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1086
1087 }
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001088}
1089
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001090static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1091{
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001092 struct device *dev = hisi_hba->dev;
1093 struct Scsi_Host *shost = hisi_hba->shost;
1094 u32 old_state, state;
1095 unsigned long flags;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001096 int rc;
1097
1098 if (!hisi_hba->hw->soft_reset)
1099 return -1;
1100
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001101 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001102 return -1;
1103
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001104 dev_info(dev, "controller resetting...\n");
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001105 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1106
1107 scsi_block_requests(shost);
1108 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1109 rc = hisi_hba->hw->soft_reset(hisi_hba);
1110 if (rc) {
1111 dev_warn(dev, "controller reset failed (%d)\n", rc);
1112 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001113 scsi_unblock_requests(shost);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001114 goto out;
1115 }
1116 spin_lock_irqsave(&hisi_hba->lock, flags);
1117 hisi_sas_release_tasks(hisi_hba);
1118 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1119
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001120 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1121
1122 /* Init and wait for PHYs to come up and all libsas event finished. */
1123 hisi_hba->hw->phys_init(hisi_hba);
1124 msleep(1000);
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001125 hisi_sas_refresh_port_id(hisi_hba);
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001126 scsi_unblock_requests(shost);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001127
1128 state = hisi_hba->hw->get_phys_state(hisi_hba);
1129 hisi_sas_rescan_topology(hisi_hba, old_state, state);
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001130 dev_info(dev, "controller reset complete\n");
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001131
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001132out:
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001133 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001134
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001135 return rc;
1136}
1137
John Garry0efff302015-11-18 00:50:56 +08001138static int hisi_sas_abort_task(struct sas_task *task)
1139{
1140 struct scsi_lun lun;
1141 struct hisi_sas_tmf_task tmf_task;
1142 struct domain_device *device = task->dev;
1143 struct hisi_sas_device *sas_dev = device->lldd_dev;
1144 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
John Garry11b75242017-06-14 23:33:17 +08001145 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +08001146 int rc = TMF_RESP_FUNC_FAILED;
1147 unsigned long flags;
1148
1149 if (!sas_dev) {
1150 dev_warn(dev, "Device has been removed\n");
1151 return TMF_RESP_FUNC_FAILED;
1152 }
1153
John Garry0efff302015-11-18 00:50:56 +08001154 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
John Garry0efff302015-11-18 00:50:56 +08001155 rc = TMF_RESP_FUNC_COMPLETE;
1156 goto out;
1157 }
1158
John Garry0efff302015-11-18 00:50:56 +08001159 sas_dev->dev_status = HISI_SAS_DEV_EH;
1160 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1161 struct scsi_cmnd *cmnd = task->uldd_task;
1162 struct hisi_sas_slot *slot = task->lldd_task;
1163 u32 tag = slot->idx;
John Garryc35279f2017-03-23 01:25:25 +08001164 int rc2;
John Garry0efff302015-11-18 00:50:56 +08001165
1166 int_to_scsilun(cmnd->device->lun, &lun);
1167 tmf_task.tmf = TMF_ABORT_TASK;
1168 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1169
1170 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1171 &tmf_task);
1172
John Garryc35279f2017-03-23 01:25:25 +08001173 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1174 HISI_SAS_INT_ABT_CMD, tag);
1175 /*
1176 * If the TMF finds that the IO is not in the device and also
1177 * the internal abort does not succeed, then it is safe to
1178 * free the slot.
1179 * Note: if the internal abort succeeds then the slot
1180 * will have already been completed
1181 */
1182 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
John Garry0efff302015-11-18 00:50:56 +08001183 if (task->lldd_task) {
John Garry0efff302015-11-18 00:50:56 +08001184 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garryc35279f2017-03-23 01:25:25 +08001185 hisi_sas_do_release_task(hisi_hba, task, slot);
John Garry0efff302015-11-18 00:50:56 +08001186 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1187 }
1188 }
John Garry0efff302015-11-18 00:50:56 +08001189 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1190 task->task_proto & SAS_PROTOCOL_STP) {
1191 if (task->dev->dev_type == SAS_SATA_DEV) {
John Garrydc8a49c2016-08-24 19:05:49 +08001192 hisi_sas_internal_task_abort(hisi_hba, device,
1193 HISI_SAS_INT_ABT_DEV, 0);
Xiang Chend30ff262017-06-14 23:33:32 +08001194 hisi_sas_dereg_device(hisi_hba, device);
Xiang Chen7c594f02017-03-23 01:25:20 +08001195 rc = hisi_sas_softreset_ata_disk(device);
John Garry0efff302015-11-18 00:50:56 +08001196 }
Gustavo A. R. Silvaeb045e02017-05-22 13:00:29 -05001197 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
John Garrydc8a49c2016-08-24 19:05:49 +08001198 /* SMP */
1199 struct hisi_sas_slot *slot = task->lldd_task;
1200 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +08001201
Xiang Chenccbfe5a2017-03-23 01:25:36 +08001202 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1203 HISI_SAS_INT_ABT_CMD, tag);
Xiaofei Tan378c2332017-10-24 23:51:37 +08001204 if (rc == TMF_RESP_FUNC_FAILED && task->lldd_task) {
Xiang Chenccbfe5a2017-03-23 01:25:36 +08001205 spin_lock_irqsave(&hisi_hba->lock, flags);
1206 hisi_sas_do_release_task(hisi_hba, task, slot);
1207 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1208 }
John Garry0efff302015-11-18 00:50:56 +08001209 }
1210
1211out:
1212 if (rc != TMF_RESP_FUNC_COMPLETE)
1213 dev_notice(dev, "abort task: rc=%d\n", rc);
1214 return rc;
1215}
1216
1217static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1218{
1219 struct hisi_sas_tmf_task tmf_task;
1220 int rc = TMF_RESP_FUNC_FAILED;
1221
1222 tmf_task.tmf = TMF_ABORT_TASK_SET;
1223 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1224
1225 return rc;
1226}
1227
1228static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1229{
1230 int rc = TMF_RESP_FUNC_FAILED;
1231 struct hisi_sas_tmf_task tmf_task;
1232
1233 tmf_task.tmf = TMF_CLEAR_ACA;
1234 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1235
1236 return rc;
1237}
1238
1239static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1240{
1241 struct sas_phy *phy = sas_get_local_phy(device);
1242 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1243 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1244 rc = sas_phy_reset(phy, reset_type);
1245 sas_put_local_phy(phy);
1246 msleep(2000);
1247 return rc;
1248}
1249
1250static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1251{
1252 struct hisi_sas_device *sas_dev = device->lldd_dev;
1253 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1254 unsigned long flags;
1255 int rc = TMF_RESP_FUNC_FAILED;
1256
1257 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1258 return TMF_RESP_FUNC_FAILED;
1259 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1260
Xiang Chend30ff262017-06-14 23:33:32 +08001261 hisi_sas_internal_task_abort(hisi_hba, device,
1262 HISI_SAS_INT_ABT_DEV, 0);
1263 hisi_sas_dereg_device(hisi_hba, device);
1264
John Garry0efff302015-11-18 00:50:56 +08001265 rc = hisi_sas_debug_I_T_nexus_reset(device);
1266
Xiang Chen61312432017-03-23 01:25:28 +08001267 if (rc == TMF_RESP_FUNC_COMPLETE) {
1268 spin_lock_irqsave(&hisi_hba->lock, flags);
1269 hisi_sas_release_task(hisi_hba, device);
1270 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1271 }
1272 return rc;
John Garry0efff302015-11-18 00:50:56 +08001273}
1274
1275static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1276{
John Garry0efff302015-11-18 00:50:56 +08001277 struct hisi_sas_device *sas_dev = device->lldd_dev;
1278 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
John Garry11b75242017-06-14 23:33:17 +08001279 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +08001280 unsigned long flags;
1281 int rc = TMF_RESP_FUNC_FAILED;
1282
John Garry0efff302015-11-18 00:50:56 +08001283 sas_dev->dev_status = HISI_SAS_DEV_EH;
John Garry055945d2017-03-23 01:25:26 +08001284 if (dev_is_sata(device)) {
1285 struct sas_phy *phy;
John Garry0efff302015-11-18 00:50:56 +08001286
John Garry055945d2017-03-23 01:25:26 +08001287 /* Clear internal IO and then hardreset */
1288 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1289 HISI_SAS_INT_ABT_DEV, 0);
1290 if (rc == TMF_RESP_FUNC_FAILED)
1291 goto out;
Xiang Chend30ff262017-06-14 23:33:32 +08001292 hisi_sas_dereg_device(hisi_hba, device);
John Garry055945d2017-03-23 01:25:26 +08001293
1294 phy = sas_get_local_phy(device);
1295
1296 rc = sas_phy_reset(phy, 1);
1297
1298 if (rc == 0) {
1299 spin_lock_irqsave(&hisi_hba->lock, flags);
1300 hisi_sas_release_task(hisi_hba, device);
1301 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1302 }
1303 sas_put_local_phy(phy);
1304 } else {
1305 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1306
1307 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1308 if (rc == TMF_RESP_FUNC_COMPLETE) {
1309 spin_lock_irqsave(&hisi_hba->lock, flags);
1310 hisi_sas_release_task(hisi_hba, device);
1311 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1312 }
1313 }
1314out:
John Garry14d3f392017-03-23 01:25:37 +08001315 if (rc != TMF_RESP_FUNC_COMPLETE)
John Garryad604832017-06-14 23:33:12 +08001316 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
John Garry14d3f392017-03-23 01:25:37 +08001317 sas_dev->device_id, rc);
John Garry0efff302015-11-18 00:50:56 +08001318 return rc;
1319}
1320
John Garry8b05ad62017-03-23 01:25:35 +08001321static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1322{
1323 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
Xiaofei Tane402acd2017-12-09 01:16:38 +08001324 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
John Garry8b05ad62017-03-23 01:25:35 +08001325
Xiaofei Tane402acd2017-12-09 01:16:38 +08001326 queue_work(hisi_hba->wq, &r.work);
1327 wait_for_completion(r.completion);
1328 if (r.done)
1329 return TMF_RESP_FUNC_COMPLETE;
1330
1331 return TMF_RESP_FUNC_FAILED;
John Garry8b05ad62017-03-23 01:25:35 +08001332}
1333
John Garry0efff302015-11-18 00:50:56 +08001334static int hisi_sas_query_task(struct sas_task *task)
1335{
1336 struct scsi_lun lun;
1337 struct hisi_sas_tmf_task tmf_task;
1338 int rc = TMF_RESP_FUNC_FAILED;
1339
1340 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1341 struct scsi_cmnd *cmnd = task->uldd_task;
1342 struct domain_device *device = task->dev;
1343 struct hisi_sas_slot *slot = task->lldd_task;
1344 u32 tag = slot->idx;
1345
1346 int_to_scsilun(cmnd->device->lun, &lun);
1347 tmf_task.tmf = TMF_QUERY_TASK;
1348 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1349
1350 rc = hisi_sas_debug_issue_ssp_tmf(device,
1351 lun.scsi_lun,
1352 &tmf_task);
1353 switch (rc) {
1354 /* The task is still in Lun, release it then */
1355 case TMF_RESP_FUNC_SUCC:
1356 /* The task is not in Lun or failed, reset the phy */
1357 case TMF_RESP_FUNC_FAILED:
1358 case TMF_RESP_FUNC_COMPLETE:
1359 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001360 default:
1361 rc = TMF_RESP_FUNC_FAILED;
1362 break;
John Garry0efff302015-11-18 00:50:56 +08001363 }
1364 }
1365 return rc;
1366}
1367
John Garry441c2742016-08-24 19:05:47 +08001368static int
John Garryad604832017-06-14 23:33:12 +08001369hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
John Garry441c2742016-08-24 19:05:47 +08001370 struct sas_task *task, int abort_flag,
1371 int task_tag)
1372{
1373 struct domain_device *device = task->dev;
1374 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry11b75242017-06-14 23:33:17 +08001375 struct device *dev = hisi_hba->dev;
John Garry441c2742016-08-24 19:05:47 +08001376 struct hisi_sas_port *port;
1377 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001378 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001379 struct hisi_sas_cmd_hdr *cmd_hdr_base;
Xiang Chenb1a49412017-06-14 23:33:13 +08001380 struct hisi_sas_dq *dq = sas_dev->dq;
John Garry441c2742016-08-24 19:05:47 +08001381 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
Xiang Chenb1a49412017-06-14 23:33:13 +08001382 unsigned long flags, flags_dq;
John Garry441c2742016-08-24 19:05:47 +08001383
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001384 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001385 return -EINVAL;
1386
John Garry441c2742016-08-24 19:05:47 +08001387 if (!device->port)
1388 return -1;
1389
John Garry2e244f02017-03-23 01:25:17 +08001390 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001391
1392 /* simply get a slot and send abort command */
Xiang Chenb1a49412017-06-14 23:33:13 +08001393 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001394 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +08001395 if (rc) {
1396 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001397 goto err_out;
Xiang Chenb1a49412017-06-14 23:33:13 +08001398 }
1399 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1400
1401 spin_lock_irqsave(&dq->lock, flags_dq);
1402 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
John Garry441c2742016-08-24 19:05:47 +08001403 if (rc)
1404 goto err_out_tag;
1405
Xiang Chenb1a49412017-06-14 23:33:13 +08001406 dlvry_queue = dq->id;
1407 dlvry_queue_slot = dq->wr_point;
1408
John Garry441c2742016-08-24 19:05:47 +08001409 slot = &hisi_hba->slot_info[slot_idx];
1410 memset(slot, 0, sizeof(struct hisi_sas_slot));
1411
1412 slot->idx = slot_idx;
1413 slot->n_elem = n_elem;
1414 slot->dlvry_queue = dlvry_queue;
1415 slot->dlvry_queue_slot = dlvry_queue_slot;
1416 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1417 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1418 slot->task = task;
1419 slot->port = port;
1420 task->lldd_task = slot;
1421
Xiang Chen031da092017-08-11 00:09:35 +08001422 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1423 GFP_ATOMIC, &slot->buf_dma);
1424 if (!slot->buf) {
1425 rc = -ENOMEM;
1426 goto err_out_tag;
1427 }
1428
John Garry441c2742016-08-24 19:05:47 +08001429 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
Xiang Chen031da092017-08-11 00:09:35 +08001430 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1431 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
John Garry441c2742016-08-24 19:05:47 +08001432
1433 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1434 abort_flag, task_tag);
1435 if (rc)
Xiang Chen031da092017-08-11 00:09:35 +08001436 goto err_out_buf;
John Garry441c2742016-08-24 19:05:47 +08001437
Xiang Chen9feaf902017-10-24 23:51:34 +08001438 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry405314d2017-03-23 01:25:21 +08001439 list_add_tail(&slot->entry, &sas_dev->list);
Xiang Chen9feaf902017-10-24 23:51:34 +08001440 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry54c9dd22017-03-23 01:25:29 +08001441 spin_lock_irqsave(&task->task_state_lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001442 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
John Garry54c9dd22017-03-23 01:25:29 +08001443 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001444
Xiang Chenb1a49412017-06-14 23:33:13 +08001445 dq->slot_prep = slot;
John Garry441c2742016-08-24 19:05:47 +08001446
John Garryf696cc32016-11-07 20:48:39 +08001447 atomic64_inc(&sas_dev->running_req);
1448
Xiang Chenb1a49412017-06-14 23:33:13 +08001449 /* send abort command to the chip */
1450 hisi_hba->hw->start_delivery(dq);
1451 spin_unlock_irqrestore(&dq->lock, flags_dq);
John Garry441c2742016-08-24 19:05:47 +08001452
1453 return 0;
1454
Xiang Chen031da092017-08-11 00:09:35 +08001455err_out_buf:
1456 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1457 slot->buf_dma);
John Garry441c2742016-08-24 19:05:47 +08001458err_out_tag:
Xiang Chenb1a49412017-06-14 23:33:13 +08001459 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001460 hisi_sas_slot_index_free(hisi_hba, slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +08001461 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1462 spin_unlock_irqrestore(&dq->lock, flags_dq);
John Garry441c2742016-08-24 19:05:47 +08001463err_out:
1464 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1465
1466 return rc;
1467}
1468
1469/**
1470 * hisi_sas_internal_task_abort -- execute an internal
1471 * abort command for single IO command or a device
1472 * @hisi_hba: host controller struct
1473 * @device: domain device
1474 * @abort_flag: mode of operation, device or single IO
1475 * @tag: tag of IO to be aborted (only relevant to single
1476 * IO mode)
1477 */
1478static int
1479hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1480 struct domain_device *device,
1481 int abort_flag, int tag)
1482{
1483 struct sas_task *task;
1484 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry11b75242017-06-14 23:33:17 +08001485 struct device *dev = hisi_hba->dev;
John Garry441c2742016-08-24 19:05:47 +08001486 int res;
John Garry441c2742016-08-24 19:05:47 +08001487
1488 if (!hisi_hba->hw->prep_abort)
1489 return -EOPNOTSUPP;
1490
1491 task = sas_alloc_slow_task(GFP_KERNEL);
1492 if (!task)
1493 return -ENOMEM;
1494
1495 task->dev = device;
1496 task->task_proto = device->tproto;
1497 task->task_done = hisi_sas_task_done;
Kees Cook841b86f2017-10-23 09:40:42 +02001498 task->slow_task->timer.function = hisi_sas_tmf_timedout;
John Garry0844a3f2017-04-10 21:21:59 +08001499 task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
John Garry441c2742016-08-24 19:05:47 +08001500 add_timer(&task->slow_task->timer);
1501
John Garry441c2742016-08-24 19:05:47 +08001502 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1503 task, abort_flag, tag);
John Garry441c2742016-08-24 19:05:47 +08001504 if (res) {
1505 del_timer(&task->slow_task->timer);
1506 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1507 res);
1508 goto exit;
1509 }
1510 wait_for_completion(&task->slow_task->completion);
1511 res = TMF_RESP_FUNC_FAILED;
1512
Xiang Chenf64a6982017-06-14 23:33:11 +08001513 /* Internal abort timed out */
1514 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1515 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1516 struct hisi_sas_slot *slot = task->lldd_task;
1517
1518 if (slot)
1519 slot->task = NULL;
Xiang Chenf1c88212017-12-09 01:16:41 +08001520 dev_err(dev, "internal task abort: timeout and not done.\n");
Xiang Chenf692a672017-10-24 23:51:32 +08001521 goto exit;
Xiang Chenf1c88212017-12-09 01:16:41 +08001522 } else
1523 dev_err(dev, "internal task abort: timeout.\n");
Xiang Chenf64a6982017-06-14 23:33:11 +08001524 }
1525
John Garry441c2742016-08-24 19:05:47 +08001526 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1527 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1528 res = TMF_RESP_FUNC_COMPLETE;
1529 goto exit;
1530 }
1531
John Garryc35279f2017-03-23 01:25:25 +08001532 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1533 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1534 res = TMF_RESP_FUNC_SUCC;
1535 goto exit;
1536 }
1537
John Garry441c2742016-08-24 19:05:47 +08001538exit:
John Garry297d7302017-01-20 20:45:22 +08001539 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001540 "resp: 0x%x sts 0x%x\n",
1541 SAS_ADDR(device->sas_addr),
1542 task,
1543 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1544 task->task_status.stat);
1545 sas_free_task(task);
1546
1547 return res;
1548}
1549
John Garry184a4632015-11-18 00:50:52 +08001550static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1551{
1552 hisi_sas_port_notify_formed(sas_phy);
1553}
1554
John Garry184a4632015-11-18 00:50:52 +08001555static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1556{
1557 phy->phy_attached = 0;
1558 phy->phy_type = 0;
1559 phy->port = NULL;
1560}
1561
1562void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1563{
1564 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1565 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1566 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1567
1568 if (rdy) {
1569 /* Phy down but ready */
1570 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1571 hisi_sas_port_notify_formed(sas_phy);
1572 } else {
1573 struct hisi_sas_port *port = phy->port;
1574
1575 /* Phy down and not ready */
1576 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1577 sas_phy_disconnected(sas_phy);
1578
1579 if (port) {
1580 if (phy->phy_type & PORT_TYPE_SAS) {
1581 int port_id = port->id;
1582
1583 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1584 port_id))
1585 port->port_attached = 0;
1586 } else if (phy->phy_type & PORT_TYPE_SATA)
1587 port->port_attached = 0;
1588 }
1589 hisi_sas_phy_disconnected(phy);
1590 }
1591}
1592EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1593
Xiaofei Tan571295f2017-10-24 23:51:47 +08001594void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1595{
1596 int i;
1597
1598 for (i = 0; i < hisi_hba->queue_count; i++) {
1599 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1600
1601 tasklet_kill(&cq->tasklet);
1602 }
1603}
1604EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001605
John Garrye21fe3a2017-06-14 23:33:20 +08001606struct scsi_transport_template *hisi_sas_stt;
1607EXPORT_SYMBOL_GPL(hisi_sas_stt);
John Garrye8899fa2015-11-18 00:50:30 +08001608
John Garrye21fe3a2017-06-14 23:33:20 +08001609static struct scsi_host_template _hisi_sas_sht = {
John Garry7eb78692015-11-18 00:50:31 +08001610 .module = THIS_MODULE,
1611 .name = DRV_NAME,
1612 .queuecommand = sas_queuecommand,
1613 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001614 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001615 .scan_finished = hisi_sas_scan_finished,
1616 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001617 .change_queue_depth = sas_change_queue_depth,
1618 .bios_param = sas_bios_param,
1619 .can_queue = 1,
1620 .this_id = -1,
1621 .sg_tablesize = SG_ALL,
1622 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1623 .use_clustering = ENABLE_CLUSTERING,
1624 .eh_device_reset_handler = sas_eh_device_reset_handler,
Hannes Reineckecc199e72017-08-25 13:57:02 +02001625 .eh_target_reset_handler = sas_eh_target_reset_handler,
John Garry7eb78692015-11-18 00:50:31 +08001626 .target_destroy = sas_target_destroy,
1627 .ioctl = sas_ioctl,
1628};
John Garrye21fe3a2017-06-14 23:33:20 +08001629struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1630EXPORT_SYMBOL_GPL(hisi_sas_sht);
John Garry7eb78692015-11-18 00:50:31 +08001631
John Garrye8899fa2015-11-18 00:50:30 +08001632static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001633 .lldd_dev_found = hisi_sas_dev_found,
1634 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001635 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001636 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001637 .lldd_abort_task = hisi_sas_abort_task,
1638 .lldd_abort_task_set = hisi_sas_abort_task_set,
1639 .lldd_clear_aca = hisi_sas_clear_aca,
1640 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1641 .lldd_lu_reset = hisi_sas_lu_reset,
1642 .lldd_query_task = hisi_sas_query_task,
John Garry8b05ad62017-03-23 01:25:35 +08001643 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
John Garry184a4632015-11-18 00:50:52 +08001644 .lldd_port_formed = hisi_sas_port_formed,
John Garrye8899fa2015-11-18 00:50:30 +08001645};
1646
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001647void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1648{
1649 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1650
1651 for (i = 0; i < hisi_hba->queue_count; i++) {
1652 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1653 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1654
1655 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1656 memset(hisi_hba->cmd_hdr[i], 0, s);
1657 dq->wr_point = 0;
1658
1659 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1660 memset(hisi_hba->complete_hdr[i], 0, s);
1661 cq->rd_point = 0;
1662 }
1663
1664 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1665 memset(hisi_hba->initial_fis, 0, s);
1666
1667 s = max_command_entries * sizeof(struct hisi_sas_iost);
1668 memset(hisi_hba->iost, 0, s);
1669
1670 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1671 memset(hisi_hba->breakpoint, 0, s);
1672
Xiang Chen3297ded2017-10-24 23:51:35 +08001673 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001674 memset(hisi_hba->sata_breakpoint, 0, s);
1675}
1676EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1677
John Garrye21fe3a2017-06-14 23:33:20 +08001678int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
John Garry6be6de12015-11-18 00:50:34 +08001679{
John Garry11b75242017-06-14 23:33:17 +08001680 struct device *dev = hisi_hba->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001681 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001682
John Garryfa42d802015-11-18 00:50:43 +08001683 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001684 for (i = 0; i < hisi_hba->n_phy; i++) {
1685 hisi_sas_phy_init(hisi_hba, i);
1686 hisi_hba->port[i].port_attached = 0;
1687 hisi_hba->port[i].id = -1;
John Garry976867e2015-11-18 00:50:42 +08001688 }
1689
John Garryaf740db2015-11-18 00:50:41 +08001690 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1691 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1692 hisi_hba->devices[i].device_id = i;
1693 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1694 }
1695
John Garry6be6de12015-11-18 00:50:34 +08001696 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001697 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001698 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001699
1700 /* Completion queue structure */
1701 cq->id = i;
1702 cq->hisi_hba = hisi_hba;
1703
John Garry4fde02a2016-09-06 23:36:12 +08001704 /* Delivery queue structure */
Xiang Chen39bade02017-12-09 01:16:32 +08001705 spin_lock_init(&dq->lock);
John Garry4fde02a2016-09-06 23:36:12 +08001706 dq->id = i;
1707 dq->hisi_hba = hisi_hba;
1708
John Garry6be6de12015-11-18 00:50:34 +08001709 /* Delivery queue */
1710 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1711 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1712 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1713 if (!hisi_hba->cmd_hdr[i])
1714 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001715
1716 /* Completion queue */
1717 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1718 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1719 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1720 if (!hisi_hba->complete_hdr[i])
1721 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001722 }
1723
Xiaofei Tanf557e322017-06-29 21:02:14 +08001724 s = sizeof(struct hisi_sas_slot_buf_table);
1725 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1726 if (!hisi_hba->buffer_pool)
John Garry6be6de12015-11-18 00:50:34 +08001727 goto err_out;
1728
1729 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1730 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1731 GFP_KERNEL);
1732 if (!hisi_hba->itct)
1733 goto err_out;
1734
1735 memset(hisi_hba->itct, 0, s);
1736
John Garrya8d547b2016-01-26 02:47:03 +08001737 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001738 sizeof(struct hisi_sas_slot),
1739 GFP_KERNEL);
1740 if (!hisi_hba->slot_info)
1741 goto err_out;
1742
John Garrya8d547b2016-01-26 02:47:03 +08001743 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001744 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1745 GFP_KERNEL);
1746 if (!hisi_hba->iost)
1747 goto err_out;
1748
John Garrya8d547b2016-01-26 02:47:03 +08001749 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001750 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1751 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1752 if (!hisi_hba->breakpoint)
1753 goto err_out;
1754
John Garrya8d547b2016-01-26 02:47:03 +08001755 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001756 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001757 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1758 if (!hisi_hba->slot_index_tags)
1759 goto err_out;
1760
John Garry6be6de12015-11-18 00:50:34 +08001761 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1762 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1763 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1764 if (!hisi_hba->initial_fis)
1765 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001766
Xiang Chen3297ded2017-10-24 23:51:35 +08001767 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001768 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1769 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1770 if (!hisi_hba->sata_breakpoint)
1771 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001772 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001773
John Garry257efd12015-11-18 00:50:36 +08001774 hisi_sas_slot_index_init(hisi_hba);
1775
John Garry7e9080e2015-11-18 00:50:40 +08001776 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1777 if (!hisi_hba->wq) {
1778 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1779 goto err_out;
1780 }
1781
John Garry6be6de12015-11-18 00:50:34 +08001782 return 0;
1783err_out:
1784 return -ENOMEM;
1785}
John Garrye21fe3a2017-06-14 23:33:20 +08001786EXPORT_SYMBOL_GPL(hisi_sas_alloc);
John Garry6be6de12015-11-18 00:50:34 +08001787
John Garrye21fe3a2017-06-14 23:33:20 +08001788void hisi_sas_free(struct hisi_hba *hisi_hba)
John Garry89d53322015-11-18 00:50:35 +08001789{
John Garry11b75242017-06-14 23:33:17 +08001790 struct device *dev = hisi_hba->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001791 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001792
1793 for (i = 0; i < hisi_hba->queue_count; i++) {
1794 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1795 if (hisi_hba->cmd_hdr[i])
1796 dma_free_coherent(dev, s,
1797 hisi_hba->cmd_hdr[i],
1798 hisi_hba->cmd_hdr_dma[i]);
1799
1800 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1801 if (hisi_hba->complete_hdr[i])
1802 dma_free_coherent(dev, s,
1803 hisi_hba->complete_hdr[i],
1804 hisi_hba->complete_hdr_dma[i]);
1805 }
1806
Xiaofei Tanf557e322017-06-29 21:02:14 +08001807 dma_pool_destroy(hisi_hba->buffer_pool);
John Garry89d53322015-11-18 00:50:35 +08001808
1809 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1810 if (hisi_hba->itct)
1811 dma_free_coherent(dev, s,
1812 hisi_hba->itct, hisi_hba->itct_dma);
1813
John Garrya8d547b2016-01-26 02:47:03 +08001814 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001815 if (hisi_hba->iost)
1816 dma_free_coherent(dev, s,
1817 hisi_hba->iost, hisi_hba->iost_dma);
1818
John Garrya8d547b2016-01-26 02:47:03 +08001819 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001820 if (hisi_hba->breakpoint)
1821 dma_free_coherent(dev, s,
1822 hisi_hba->breakpoint,
1823 hisi_hba->breakpoint_dma);
1824
1825
1826 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1827 if (hisi_hba->initial_fis)
1828 dma_free_coherent(dev, s,
1829 hisi_hba->initial_fis,
1830 hisi_hba->initial_fis_dma);
1831
Xiang Chen3297ded2017-10-24 23:51:35 +08001832 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001833 if (hisi_hba->sata_breakpoint)
1834 dma_free_coherent(dev, s,
1835 hisi_hba->sata_breakpoint,
1836 hisi_hba->sata_breakpoint_dma);
1837
John Garry7e9080e2015-11-18 00:50:40 +08001838 if (hisi_hba->wq)
1839 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001840}
John Garrye21fe3a2017-06-14 23:33:20 +08001841EXPORT_SYMBOL_GPL(hisi_sas_free);
John Garry6be6de12015-11-18 00:50:34 +08001842
Xiaofei Tanb4241f02017-10-24 23:51:45 +08001843void hisi_sas_rst_work_handler(struct work_struct *work)
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001844{
1845 struct hisi_hba *hisi_hba =
1846 container_of(work, struct hisi_hba, rst_work);
1847
1848 hisi_sas_controller_reset(hisi_hba);
1849}
Xiaofei Tanb4241f02017-10-24 23:51:45 +08001850EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001851
Xiaofei Tane402acd2017-12-09 01:16:38 +08001852void hisi_sas_sync_rst_work_handler(struct work_struct *work)
1853{
1854 struct hisi_sas_rst *rst =
1855 container_of(work, struct hisi_sas_rst, work);
1856
1857 if (!hisi_sas_controller_reset(rst->hisi_hba))
1858 rst->done = true;
1859 complete(rst->completion);
1860}
1861EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
1862
John Garry0fa24c12017-06-14 23:33:18 +08001863int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
1864{
1865 struct device *dev = hisi_hba->dev;
1866 struct platform_device *pdev = hisi_hba->platform_dev;
1867 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
1868 struct clk *refclk;
1869
1870 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1871 SAS_ADDR_SIZE)) {
1872 dev_err(dev, "could not get property sas-addr\n");
1873 return -ENOENT;
1874 }
1875
1876 if (np) {
1877 /*
1878 * These properties are only required for platform device-based
1879 * controller with DT firmware.
1880 */
1881 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1882 "hisilicon,sas-syscon");
1883 if (IS_ERR(hisi_hba->ctrl)) {
1884 dev_err(dev, "could not get syscon\n");
1885 return -ENOENT;
1886 }
1887
1888 if (device_property_read_u32(dev, "ctrl-reset-reg",
1889 &hisi_hba->ctrl_reset_reg)) {
1890 dev_err(dev,
1891 "could not get property ctrl-reset-reg\n");
1892 return -ENOENT;
1893 }
1894
1895 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1896 &hisi_hba->ctrl_reset_sts_reg)) {
1897 dev_err(dev,
1898 "could not get property ctrl-reset-sts-reg\n");
1899 return -ENOENT;
1900 }
1901
1902 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1903 &hisi_hba->ctrl_clock_ena_reg)) {
1904 dev_err(dev,
1905 "could not get property ctrl-clock-ena-reg\n");
1906 return -ENOENT;
1907 }
1908 }
1909
1910 refclk = devm_clk_get(dev, NULL);
1911 if (IS_ERR(refclk))
1912 dev_dbg(dev, "no ref clk property\n");
1913 else
1914 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1915
1916 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
1917 dev_err(dev, "could not get property phy-count\n");
1918 return -ENOENT;
1919 }
1920
1921 if (device_property_read_u32(dev, "queue-count",
1922 &hisi_hba->queue_count)) {
1923 dev_err(dev, "could not get property queue-count\n");
1924 return -ENOENT;
1925 }
1926
1927 return 0;
1928}
1929EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
1930
John Garry7eb78692015-11-18 00:50:31 +08001931static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1932 const struct hisi_sas_hw *hw)
1933{
John Garrye26b2f42015-11-18 00:50:32 +08001934 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08001935 struct Scsi_Host *shost;
1936 struct hisi_hba *hisi_hba;
1937 struct device *dev = &pdev->dev;
1938
John Garrye21fe3a2017-06-14 23:33:20 +08001939 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08001940 if (!shost) {
1941 dev_err(dev, "scsi host alloc failed\n");
1942 return NULL;
1943 }
John Garry7eb78692015-11-18 00:50:31 +08001944 hisi_hba = shost_priv(shost);
1945
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001946 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08001947 hisi_hba->hw = hw;
John Garry11b75242017-06-14 23:33:17 +08001948 hisi_hba->dev = dev;
John Garry0fa24c12017-06-14 23:33:18 +08001949 hisi_hba->platform_dev = pdev;
John Garry7eb78692015-11-18 00:50:31 +08001950 hisi_hba->shost = shost;
1951 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1952
Kees Cook77570ee2017-08-22 16:05:14 -07001953 timer_setup(&hisi_hba->timer, NULL, 0);
John Garryfa42d802015-11-18 00:50:43 +08001954
John Garry0fa24c12017-06-14 23:33:18 +08001955 if (hisi_sas_get_fw_info(hisi_hba) < 0)
John Garrye26b2f42015-11-18 00:50:32 +08001956 goto err_out;
1957
John Garrya6f2c7f2016-09-06 23:36:19 +08001958 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1959 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1960 dev_err(dev, "No usable DMA addressing method\n");
1961 goto err_out;
1962 }
1963
John Garrye26b2f42015-11-18 00:50:32 +08001964 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1965 hisi_hba->regs = devm_ioremap_resource(dev, res);
1966 if (IS_ERR(hisi_hba->regs))
1967 goto err_out;
1968
John Garry89d53322015-11-18 00:50:35 +08001969 if (hisi_sas_alloc(hisi_hba, shost)) {
1970 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001971 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08001972 }
John Garry6be6de12015-11-18 00:50:34 +08001973
John Garry7eb78692015-11-18 00:50:31 +08001974 return shost;
1975err_out:
John Garry76aae5f2017-08-11 00:09:43 +08001976 scsi_host_put(shost);
John Garry7eb78692015-11-18 00:50:31 +08001977 dev_err(dev, "shost alloc failed\n");
1978 return NULL;
1979}
1980
John Garrye21fe3a2017-06-14 23:33:20 +08001981void hisi_sas_init_add(struct hisi_hba *hisi_hba)
John Garry5d742422015-11-18 00:50:38 +08001982{
1983 int i;
1984
1985 for (i = 0; i < hisi_hba->n_phy; i++)
1986 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1987 hisi_hba->sas_addr,
1988 SAS_ADDR_SIZE);
1989}
John Garrye21fe3a2017-06-14 23:33:20 +08001990EXPORT_SYMBOL_GPL(hisi_sas_init_add);
John Garry5d742422015-11-18 00:50:38 +08001991
John Garry7eb78692015-11-18 00:50:31 +08001992int hisi_sas_probe(struct platform_device *pdev,
1993 const struct hisi_sas_hw *hw)
1994{
1995 struct Scsi_Host *shost;
1996 struct hisi_hba *hisi_hba;
1997 struct device *dev = &pdev->dev;
1998 struct asd_sas_phy **arr_phy;
1999 struct asd_sas_port **arr_port;
2000 struct sas_ha_struct *sha;
2001 int rc, phy_nr, port_nr, i;
2002
2003 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08002004 if (!shost)
2005 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08002006
2007 sha = SHOST_TO_SAS_HA(shost);
2008 hisi_hba = shost_priv(shost);
2009 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08002010
John Garry7eb78692015-11-18 00:50:31 +08002011 phy_nr = port_nr = hisi_hba->n_phy;
2012
2013 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2014 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08002015 if (!arr_phy || !arr_port) {
2016 rc = -ENOMEM;
2017 goto err_out_ha;
2018 }
John Garry7eb78692015-11-18 00:50:31 +08002019
2020 sha->sas_phy = arr_phy;
2021 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08002022 sha->lldd_ha = hisi_hba;
2023
2024 shost->transportt = hisi_sas_stt;
2025 shost->max_id = HISI_SAS_MAX_DEVICES;
2026 shost->max_lun = ~0;
2027 shost->max_channel = 1;
2028 shost->max_cmd_len = 16;
2029 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08002030 shost->can_queue = hisi_hba->hw->max_command_entries;
2031 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08002032
2033 sha->sas_ha_name = DRV_NAME;
John Garry11b75242017-06-14 23:33:17 +08002034 sha->dev = hisi_hba->dev;
John Garry7eb78692015-11-18 00:50:31 +08002035 sha->lldd_module = THIS_MODULE;
2036 sha->sas_addr = &hisi_hba->sas_addr[0];
2037 sha->num_phys = hisi_hba->n_phy;
2038 sha->core.shost = hisi_hba->shost;
2039
2040 for (i = 0; i < hisi_hba->n_phy; i++) {
2041 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2042 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2043 }
2044
John Garry5d742422015-11-18 00:50:38 +08002045 hisi_sas_init_add(hisi_hba);
2046
John Garry7eb78692015-11-18 00:50:31 +08002047 rc = scsi_add_host(shost, &pdev->dev);
2048 if (rc)
2049 goto err_out_ha;
2050
2051 rc = sas_register_ha(sha);
2052 if (rc)
2053 goto err_out_register_ha;
2054
Xiang Chen0757f042017-01-20 20:45:23 +08002055 rc = hisi_hba->hw->hw_init(hisi_hba);
2056 if (rc)
2057 goto err_out_register_ha;
2058
John Garry7eb78692015-11-18 00:50:31 +08002059 scsi_scan_host(shost);
2060
2061 return 0;
2062
2063err_out_register_ha:
2064 scsi_remove_host(shost);
2065err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08002066 hisi_sas_free(hisi_hba);
John Garry76aae5f2017-08-11 00:09:43 +08002067 scsi_host_put(shost);
John Garry7eb78692015-11-18 00:50:31 +08002068 return rc;
2069}
2070EXPORT_SYMBOL_GPL(hisi_sas_probe);
2071
John Garry89d53322015-11-18 00:50:35 +08002072int hisi_sas_remove(struct platform_device *pdev)
2073{
2074 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2075 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08002076 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08002077
John Garry89d53322015-11-18 00:50:35 +08002078 sas_unregister_ha(sha);
2079 sas_remove_host(sha->core.shost);
2080
2081 hisi_sas_free(hisi_hba);
John Garry76aae5f2017-08-11 00:09:43 +08002082 scsi_host_put(shost);
John Garry89d53322015-11-18 00:50:35 +08002083 return 0;
2084}
2085EXPORT_SYMBOL_GPL(hisi_sas_remove);
2086
John Garrye8899fa2015-11-18 00:50:30 +08002087static __init int hisi_sas_init(void)
2088{
John Garrye8899fa2015-11-18 00:50:30 +08002089 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2090 if (!hisi_sas_stt)
2091 return -ENOMEM;
2092
2093 return 0;
2094}
2095
2096static __exit void hisi_sas_exit(void)
2097{
2098 sas_release_transport(hisi_sas_stt);
2099}
2100
2101module_init(hisi_sas_init);
2102module_exit(hisi_sas_exit);
2103
John Garrye8899fa2015-11-18 00:50:30 +08002104MODULE_LICENSE("GPL");
2105MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2106MODULE_DESCRIPTION("HISILICON SAS controller driver");
2107MODULE_ALIAS("platform:" DRV_NAME);