blob: e3e7285f5eb18325cca26bda0c27b8146a42a6c1 [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);
Xiaofei Tan057c3d12017-12-09 01:16:45 +080025static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
26 void *funcdata);
John Garrycac9b2a2016-02-25 17:42:11 +080027
chenxiang468f4b82017-12-28 18:20:47 +080028u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080029{
chenxiang468f4b82017-12-28 18:20:47 +080030 switch (fis->command) {
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080031 case ATA_CMD_FPDMA_WRITE:
32 case ATA_CMD_FPDMA_READ:
33 case ATA_CMD_FPDMA_RECV:
34 case ATA_CMD_FPDMA_SEND:
35 case ATA_CMD_NCQ_NON_DATA:
36 return HISI_SAS_SATA_PROTOCOL_FPDMA;
37
38 case ATA_CMD_DOWNLOAD_MICRO:
39 case ATA_CMD_ID_ATA:
40 case ATA_CMD_PMP_READ:
41 case ATA_CMD_READ_LOG_EXT:
42 case ATA_CMD_PIO_READ:
43 case ATA_CMD_PIO_READ_EXT:
44 case ATA_CMD_PMP_WRITE:
45 case ATA_CMD_WRITE_LOG_EXT:
46 case ATA_CMD_PIO_WRITE:
47 case ATA_CMD_PIO_WRITE_EXT:
48 return HISI_SAS_SATA_PROTOCOL_PIO;
49
50 case ATA_CMD_DSM:
51 case ATA_CMD_DOWNLOAD_MICRO_DMA:
52 case ATA_CMD_PMP_READ_DMA:
53 case ATA_CMD_PMP_WRITE_DMA:
54 case ATA_CMD_READ:
55 case ATA_CMD_READ_EXT:
56 case ATA_CMD_READ_LOG_DMA_EXT:
57 case ATA_CMD_READ_STREAM_DMA_EXT:
58 case ATA_CMD_TRUSTED_RCV_DMA:
59 case ATA_CMD_TRUSTED_SND_DMA:
60 case ATA_CMD_WRITE:
61 case ATA_CMD_WRITE_EXT:
62 case ATA_CMD_WRITE_FUA_EXT:
63 case ATA_CMD_WRITE_QUEUED:
64 case ATA_CMD_WRITE_LOG_DMA_EXT:
65 case ATA_CMD_WRITE_STREAM_DMA_EXT:
Xiaofei Tanc3fe8a2b2017-08-11 00:09:34 +080066 case ATA_CMD_ZAC_MGMT_IN:
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080067 return HISI_SAS_SATA_PROTOCOL_DMA;
68
69 case ATA_CMD_CHK_POWER:
70 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_EDD:
72 case ATA_CMD_FLUSH:
73 case ATA_CMD_FLUSH_EXT:
74 case ATA_CMD_VERIFY:
75 case ATA_CMD_VERIFY_EXT:
76 case ATA_CMD_SET_FEATURES:
77 case ATA_CMD_STANDBY:
78 case ATA_CMD_STANDBYNOW1:
Xiaofei Tanc3fe8a2b2017-08-11 00:09:34 +080079 case ATA_CMD_ZAC_MGMT_OUT:
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080080 return HISI_SAS_SATA_PROTOCOL_NONDATA;
81 default:
chenxiang468f4b82017-12-28 18:20:47 +080082 {
83 if (fis->command == ATA_CMD_SET_MAX) {
84 switch (fis->features) {
85 case ATA_SET_MAX_PASSWD:
86 case ATA_SET_MAX_LOCK:
87 return HISI_SAS_SATA_PROTOCOL_PIO;
88
89 case ATA_SET_MAX_PASSWD_DMA:
90 case ATA_SET_MAX_UNLOCK_DMA:
91 return HISI_SAS_SATA_PROTOCOL_DMA;
92
93 default:
94 return HISI_SAS_SATA_PROTOCOL_NONDATA;
95 }
96 }
Xiang Chen6c7bb8a2017-06-14 23:33:14 +080097 if (direction == DMA_NONE)
98 return HISI_SAS_SATA_PROTOCOL_NONDATA;
99 return HISI_SAS_SATA_PROTOCOL_PIO;
100 }
chenxiang468f4b82017-12-28 18:20:47 +0800101 }
Xiang Chen6c7bb8a2017-06-14 23:33:14 +0800102}
103EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
104
Xiang Chen75904072017-06-14 23:33:15 +0800105void hisi_sas_sata_done(struct sas_task *task,
106 struct hisi_sas_slot *slot)
107{
108 struct task_status_struct *ts = &task->task_status;
109 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
Xiaofei Tanf557e322017-06-29 21:02:14 +0800110 struct hisi_sas_status_buffer *status_buf =
111 hisi_sas_status_buf_addr_mem(slot);
112 u8 *iu = &status_buf->iu[0];
113 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
Xiang Chen75904072017-06-14 23:33:15 +0800114
115 resp->frame_len = sizeof(struct dev_to_host_fis);
116 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
117
118 ts->buf_valid_size = sizeof(*resp);
119}
120EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
121
Xiang Chen318913c2017-06-14 23:33:16 +0800122int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
123{
124 struct ata_queued_cmd *qc = task->uldd_task;
125
126 if (qc) {
127 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
128 qc->tf.command == ATA_CMD_FPDMA_READ) {
129 *tag = qc->tag;
130 return 1;
131 }
132 }
133 return 0;
134}
135EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
136
John Garry42e7a692015-11-18 00:50:49 +0800137static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
138{
139 return device->port->ha->lldd_ha;
140}
141
John Garry2e244f02017-03-23 01:25:17 +0800142struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
143{
144 return container_of(sas_port, struct hisi_sas_port, sas_port);
145}
146EXPORT_SYMBOL_GPL(to_hisi_sas_port);
147
Xiang Chena25d0d32017-08-11 00:09:40 +0800148void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
149{
150 int phy_no;
151
152 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
153 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
154}
155EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
156
John Garry257efd12015-11-18 00:50:36 +0800157static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
158{
159 void *bitmap = hisi_hba->slot_index_tags;
160
161 clear_bit(slot_idx, bitmap);
162}
163
John Garry42e7a692015-11-18 00:50:49 +0800164static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
165{
166 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
167}
168
169static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
170{
171 void *bitmap = hisi_hba->slot_index_tags;
172
173 set_bit(slot_idx, bitmap);
174}
175
176static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
177{
178 unsigned int index;
179 void *bitmap = hisi_hba->slot_index_tags;
180
181 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
182 if (index >= hisi_hba->slot_index_count)
183 return -SAS_QUEUE_FULL;
184 hisi_sas_slot_index_set(hisi_hba, index);
185 *slot_idx = index;
186 return 0;
187}
188
John Garry257efd12015-11-18 00:50:36 +0800189static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
190{
191 int i;
192
193 for (i = 0; i < hisi_hba->slot_index_count; ++i)
194 hisi_sas_slot_index_clear(hisi_hba, i);
195}
John Garry27a3f222015-11-18 00:50:50 +0800196
197void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
198 struct hisi_sas_slot *slot)
199{
John Garry27a3f222015-11-18 00:50:50 +0800200
John Garryd3c4dd42017-04-10 21:22:00 +0800201 if (task) {
John Garry11b75242017-06-14 23:33:17 +0800202 struct device *dev = hisi_hba->dev;
John Garryd3c4dd42017-04-10 21:22:00 +0800203 struct domain_device *device = task->dev;
204 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry27a3f222015-11-18 00:50:50 +0800205
Xiaofei Tan6ba0fbc2017-10-24 23:51:38 +0800206 if (!task->lldd_task)
207 return;
208
209 task->lldd_task = NULL;
210
John Garryd3c4dd42017-04-10 21:22:00 +0800211 if (!sas_protocol_ata(task->task_proto))
212 if (slot->n_elem)
Xiang Chendc1e4732017-12-09 01:16:33 +0800213 dma_unmap_sg(dev, task->scatter,
214 task->num_scatter,
John Garryd3c4dd42017-04-10 21:22:00 +0800215 task->data_dir);
216
John Garryd3c4dd42017-04-10 21:22:00 +0800217 if (sas_dev)
218 atomic64_dec(&sas_dev->running_req);
219 }
John Garry27a3f222015-11-18 00:50:50 +0800220
Xiaofei Tanf557e322017-06-29 21:02:14 +0800221 if (slot->buf)
222 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
John Garry27a3f222015-11-18 00:50:50 +0800223
John Garry27a3f222015-11-18 00:50:50 +0800224 list_del_init(&slot->entry);
Xiaofei Tan6ba0fbc2017-10-24 23:51:38 +0800225 slot->buf = NULL;
John Garry27a3f222015-11-18 00:50:50 +0800226 slot->task = NULL;
227 slot->port = NULL;
228 hisi_sas_slot_index_free(hisi_hba, slot->idx);
John Garryd3c4dd42017-04-10 21:22:00 +0800229
John Garry59ba49f2016-09-06 23:36:14 +0800230 /* slot memory is fully zeroed when it is reused */
John Garry27a3f222015-11-18 00:50:50 +0800231}
232EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
233
John Garry66ee9992015-11-18 00:50:54 +0800234static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
235 struct hisi_sas_slot *slot)
236{
237 return hisi_hba->hw->prep_smp(hisi_hba, slot);
238}
239
John Garry42e7a692015-11-18 00:50:49 +0800240static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
241 struct hisi_sas_slot *slot, int is_tmf,
242 struct hisi_sas_tmf_task *tmf)
243{
244 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
245}
246
John Garry6f2ff1a2016-01-26 02:47:20 +0800247static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
248 struct hisi_sas_slot *slot)
249{
250 return hisi_hba->hw->prep_stp(hisi_hba, slot);
251}
252
John Garry441c2742016-08-24 19:05:47 +0800253static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
254 struct hisi_sas_slot *slot,
255 int device_id, int abort_flag, int tag_to_abort)
256{
257 return hisi_hba->hw->prep_abort(hisi_hba, slot,
258 device_id, abort_flag, tag_to_abort);
259}
260
John Garrycac9b2a2016-02-25 17:42:11 +0800261/*
262 * This function will issue an abort TMF regardless of whether the
263 * task is in the sdev or not. Then it will do the task complete
264 * cleanup and callbacks.
265 */
266static void hisi_sas_slot_abort(struct work_struct *work)
267{
268 struct hisi_sas_slot *abort_slot =
269 container_of(work, struct hisi_sas_slot, abort_slot);
270 struct sas_task *task = abort_slot->task;
271 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
272 struct scsi_cmnd *cmnd = task->uldd_task;
273 struct hisi_sas_tmf_task tmf_task;
John Garrycac9b2a2016-02-25 17:42:11 +0800274 struct scsi_lun lun;
John Garry11b75242017-06-14 23:33:17 +0800275 struct device *dev = hisi_hba->dev;
John Garrycac9b2a2016-02-25 17:42:11 +0800276 int tag = abort_slot->idx;
John Garryda7b66e2017-01-03 20:24:50 +0800277 unsigned long flags;
John Garrycac9b2a2016-02-25 17:42:11 +0800278
279 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
280 dev_err(dev, "cannot abort slot for non-ssp task\n");
281 goto out;
282 }
283
284 int_to_scsilun(cmnd->device->lun, &lun);
285 tmf_task.tmf = TMF_ABORT_TASK;
286 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
287
288 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
289out:
290 /* Do cleanup for this task */
John Garryda7b66e2017-01-03 20:24:50 +0800291 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800292 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
John Garryda7b66e2017-01-03 20:24:50 +0800293 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800294 if (task->task_done)
295 task->task_done(task);
John Garrycac9b2a2016-02-25 17:42:11 +0800296}
297
Xiang Chenb1a49412017-06-14 23:33:13 +0800298static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
299 *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
300 int *pass)
John Garry42e7a692015-11-18 00:50:49 +0800301{
Xiang Chenb1a49412017-06-14 23:33:13 +0800302 struct hisi_hba *hisi_hba = dq->hisi_hba;
John Garry42e7a692015-11-18 00:50:49 +0800303 struct domain_device *device = task->dev;
304 struct hisi_sas_device *sas_dev = device->lldd_dev;
305 struct hisi_sas_port *port;
306 struct hisi_sas_slot *slot;
307 struct hisi_sas_cmd_hdr *cmd_hdr_base;
John Garry2e244f02017-03-23 01:25:17 +0800308 struct asd_sas_port *sas_port = device->port;
John Garry11b75242017-06-14 23:33:17 +0800309 struct device *dev = hisi_hba->dev;
John Garry42e7a692015-11-18 00:50:49 +0800310 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
John Garry54c9dd22017-03-23 01:25:29 +0800311 unsigned long flags;
John Garry42e7a692015-11-18 00:50:49 +0800312
John Garry2e244f02017-03-23 01:25:17 +0800313 if (!sas_port) {
John Garry42e7a692015-11-18 00:50:49 +0800314 struct task_status_struct *ts = &task->task_status;
315
316 ts->resp = SAS_TASK_UNDELIVERED;
317 ts->stat = SAS_PHY_DOWN;
318 /*
319 * libsas will use dev->port, should
320 * not call task_done for sata
321 */
322 if (device->dev_type != SAS_SATA_DEV)
323 task->task_done(task);
John Garryddabca22017-03-23 01:25:22 +0800324 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800325 }
326
327 if (DEV_IS_GONE(sas_dev)) {
328 if (sas_dev)
John Garryad604832017-06-14 23:33:12 +0800329 dev_info(dev, "task prep: device %d not ready\n",
John Garry42e7a692015-11-18 00:50:49 +0800330 sas_dev->device_id);
331 else
332 dev_info(dev, "task prep: device %016llx not ready\n",
333 SAS_ADDR(device->sas_addr));
334
John Garryddabca22017-03-23 01:25:22 +0800335 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800336 }
John Garry2e244f02017-03-23 01:25:17 +0800337
338 port = to_hisi_sas_port(sas_port);
John Garry9859f242016-08-24 19:05:52 +0800339 if (port && !port->port_attached) {
John Garry09fe9ec2016-09-06 23:36:18 +0800340 dev_info(dev, "task prep: %s port%d not attach device\n",
Xiang Chen6073b772017-03-23 01:25:38 +0800341 (dev_is_sata(device)) ?
John Garry09fe9ec2016-09-06 23:36:18 +0800342 "SATA/STP" : "SAS",
343 device->port->id);
John Garry42e7a692015-11-18 00:50:49 +0800344
John Garry09fe9ec2016-09-06 23:36:18 +0800345 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800346 }
347
348 if (!sas_protocol_ata(task->task_proto)) {
349 if (task->num_scatter) {
350 n_elem = dma_map_sg(dev, task->scatter,
351 task->num_scatter, task->data_dir);
352 if (!n_elem) {
353 rc = -ENOMEM;
354 goto prep_out;
355 }
356 }
357 } else
358 n_elem = task->num_scatter;
359
Xiang Chenb1a49412017-06-14 23:33:13 +0800360 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry685b6d62016-04-15 21:36:36 +0800361 if (hisi_hba->hw->slot_index_alloc)
362 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
363 device);
364 else
365 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +0800366 if (rc) {
367 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800368 goto err_out;
Xiang Chenb1a49412017-06-14 23:33:13 +0800369 }
370 spin_unlock_irqrestore(&hisi_hba->lock, flags);
371
372 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
John Garry42e7a692015-11-18 00:50:49 +0800373 if (rc)
374 goto err_out_tag;
375
Xiang Chenb1a49412017-06-14 23:33:13 +0800376 dlvry_queue = dq->id;
377 dlvry_queue_slot = dq->wr_point;
John Garry42e7a692015-11-18 00:50:49 +0800378 slot = &hisi_hba->slot_info[slot_idx];
379 memset(slot, 0, sizeof(struct hisi_sas_slot));
380
381 slot->idx = slot_idx;
382 slot->n_elem = n_elem;
383 slot->dlvry_queue = dlvry_queue;
384 slot->dlvry_queue_slot = dlvry_queue_slot;
385 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
386 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
387 slot->task = task;
388 slot->port = port;
389 task->lldd_task = slot;
John Garrycac9b2a2016-02-25 17:42:11 +0800390 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
John Garry42e7a692015-11-18 00:50:49 +0800391
Xiaofei Tanf557e322017-06-29 21:02:14 +0800392 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
393 GFP_ATOMIC, &slot->buf_dma);
394 if (!slot->buf) {
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300395 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800396 goto err_out_slot_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300397 }
John Garry42e7a692015-11-18 00:50:49 +0800398 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
Xiaofei Tanf557e322017-06-29 21:02:14 +0800399 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
400 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
John Garry42e7a692015-11-18 00:50:49 +0800401
402 switch (task->task_proto) {
John Garry66ee9992015-11-18 00:50:54 +0800403 case SAS_PROTOCOL_SMP:
404 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
405 break;
John Garry42e7a692015-11-18 00:50:49 +0800406 case SAS_PROTOCOL_SSP:
407 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
408 break;
409 case SAS_PROTOCOL_SATA:
410 case SAS_PROTOCOL_STP:
411 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
John Garry6f2ff1a2016-01-26 02:47:20 +0800412 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
413 break;
John Garry42e7a692015-11-18 00:50:49 +0800414 default:
415 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
416 task->task_proto);
417 rc = -EINVAL;
418 break;
419 }
420
421 if (rc) {
422 dev_err(dev, "task prep: rc = 0x%x\n", rc);
Xiaofei Tanf557e322017-06-29 21:02:14 +0800423 goto err_out_buf;
John Garry42e7a692015-11-18 00:50:49 +0800424 }
425
Xiang Chen9feaf902017-10-24 23:51:34 +0800426 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry405314d2017-03-23 01:25:21 +0800427 list_add_tail(&slot->entry, &sas_dev->list);
Xiang Chen9feaf902017-10-24 23:51:34 +0800428 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry54c9dd22017-03-23 01:25:29 +0800429 spin_lock_irqsave(&task->task_state_lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800430 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
John Garry54c9dd22017-03-23 01:25:29 +0800431 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800432
Xiang Chenb1a49412017-06-14 23:33:13 +0800433 dq->slot_prep = slot;
John Garry42e7a692015-11-18 00:50:49 +0800434
John Garryf696cc32016-11-07 20:48:39 +0800435 atomic64_inc(&sas_dev->running_req);
John Garry42e7a692015-11-18 00:50:49 +0800436 ++(*pass);
437
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300438 return 0;
John Garry42e7a692015-11-18 00:50:49 +0800439
Xiaofei Tanf557e322017-06-29 21:02:14 +0800440err_out_buf:
441 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
442 slot->buf_dma);
John Garry42e7a692015-11-18 00:50:49 +0800443err_out_slot_buf:
444 /* Nothing to be done */
445err_out_tag:
Xiang Chenb1a49412017-06-14 23:33:13 +0800446 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800447 hisi_sas_slot_index_free(hisi_hba, slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +0800448 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800449err_out:
450 dev_err(dev, "task prep: failed[%d]!\n", rc);
451 if (!sas_protocol_ata(task->task_proto))
452 if (n_elem)
Xiang Chendc1e4732017-12-09 01:16:33 +0800453 dma_unmap_sg(dev, task->scatter,
454 task->num_scatter,
John Garry42e7a692015-11-18 00:50:49 +0800455 task->data_dir);
456prep_out:
457 return rc;
458}
459
460static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
461 int is_tmf, struct hisi_sas_tmf_task *tmf)
462{
463 u32 rc;
464 u32 pass = 0;
465 unsigned long flags;
466 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
John Garry11b75242017-06-14 23:33:17 +0800467 struct device *dev = hisi_hba->dev;
Xiang Chenb1a49412017-06-14 23:33:13 +0800468 struct domain_device *device = task->dev;
469 struct hisi_sas_device *sas_dev = device->lldd_dev;
470 struct hisi_sas_dq *dq = sas_dev->dq;
John Garry42e7a692015-11-18 00:50:49 +0800471
Xiaofei Tan917d3bd2017-08-11 00:09:26 +0800472 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800473 return -EINVAL;
474
John Garry42e7a692015-11-18 00:50:49 +0800475 /* protect task_prep and start_delivery sequence */
Xiang Chenb1a49412017-06-14 23:33:13 +0800476 spin_lock_irqsave(&dq->lock, flags);
477 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
John Garry42e7a692015-11-18 00:50:49 +0800478 if (rc)
479 dev_err(dev, "task exec: failed[%d]!\n", rc);
480
481 if (likely(pass))
Xiang Chenb1a49412017-06-14 23:33:13 +0800482 hisi_hba->hw->start_delivery(dq);
483 spin_unlock_irqrestore(&dq->lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800484
485 return rc;
486}
John Garry257efd12015-11-18 00:50:36 +0800487
John Garry66139922015-11-18 00:50:48 +0800488static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
489{
490 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
491 struct asd_sas_phy *sas_phy = &phy->sas_phy;
492 struct sas_ha_struct *sas_ha;
493
494 if (!phy->phy_attached)
495 return;
496
497 sas_ha = &hisi_hba->sha;
498 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
499
500 if (sas_phy->phy) {
501 struct sas_phy *sphy = sas_phy->phy;
502
503 sphy->negotiated_linkrate = sas_phy->linkrate;
John Garry66139922015-11-18 00:50:48 +0800504 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
Xiang Chen2ae75782016-11-07 20:48:40 +0800505 sphy->maximum_linkrate_hw =
506 hisi_hba->hw->phy_get_max_linkrate();
507 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
508 sphy->minimum_linkrate = phy->minimum_linkrate;
509
510 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
511 sphy->maximum_linkrate = phy->maximum_linkrate;
John Garry66139922015-11-18 00:50:48 +0800512 }
513
514 if (phy->phy_type & PORT_TYPE_SAS) {
515 struct sas_identify_frame *id;
516
517 id = (struct sas_identify_frame *)phy->frame_rcvd;
518 id->dev_type = phy->identify.device_type;
519 id->initiator_bits = SAS_PROTOCOL_ALL;
520 id->target_bits = phy->identify.target_port_protocols;
521 } else if (phy->phy_type & PORT_TYPE_SATA) {
522 /*Nothing*/
523 }
524
525 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
526 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
527}
528
John Garryabda97c2015-11-18 00:50:51 +0800529static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
530{
531 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
532 struct hisi_sas_device *sas_dev = NULL;
Xiaofei Tan302e0902017-10-24 23:51:33 +0800533 unsigned long flags;
John Garryabda97c2015-11-18 00:50:51 +0800534 int i;
535
Xiaofei Tan302e0902017-10-24 23:51:33 +0800536 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garryabda97c2015-11-18 00:50:51 +0800537 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
538 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
Xiang Chenb1a49412017-06-14 23:33:13 +0800539 int queue = i % hisi_hba->queue_count;
540 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
541
John Garryabda97c2015-11-18 00:50:51 +0800542 hisi_hba->devices[i].device_id = i;
543 sas_dev = &hisi_hba->devices[i];
544 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
545 sas_dev->dev_type = device->dev_type;
546 sas_dev->hisi_hba = hisi_hba;
547 sas_dev->sas_device = device;
Xiang Chenb1a49412017-06-14 23:33:13 +0800548 sas_dev->dq = dq;
John Garry405314d2017-03-23 01:25:21 +0800549 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
John Garryabda97c2015-11-18 00:50:51 +0800550 break;
551 }
552 }
Xiaofei Tan302e0902017-10-24 23:51:33 +0800553 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garryabda97c2015-11-18 00:50:51 +0800554
555 return sas_dev;
556}
557
558static int hisi_sas_dev_found(struct domain_device *device)
559{
560 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
561 struct domain_device *parent_dev = device->parent;
562 struct hisi_sas_device *sas_dev;
John Garry11b75242017-06-14 23:33:17 +0800563 struct device *dev = hisi_hba->dev;
John Garryabda97c2015-11-18 00:50:51 +0800564
John Garry685b6d62016-04-15 21:36:36 +0800565 if (hisi_hba->hw->alloc_dev)
566 sas_dev = hisi_hba->hw->alloc_dev(device);
567 else
568 sas_dev = hisi_sas_alloc_dev(device);
John Garryabda97c2015-11-18 00:50:51 +0800569 if (!sas_dev) {
570 dev_err(dev, "fail alloc dev: max support %d devices\n",
571 HISI_SAS_MAX_DEVICES);
572 return -EINVAL;
573 }
574
575 device->lldd_dev = sas_dev;
576 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
577
578 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
579 int phy_no;
580 u8 phy_num = parent_dev->ex_dev.num_phys;
581 struct ex_phy *phy;
582
583 for (phy_no = 0; phy_no < phy_num; phy_no++) {
584 phy = &parent_dev->ex_dev.ex_phy[phy_no];
585 if (SAS_ADDR(phy->attached_sas_addr) ==
586 SAS_ADDR(device->sas_addr)) {
587 sas_dev->attached_phy = phy_no;
588 break;
589 }
590 }
591
592 if (phy_no == phy_num) {
593 dev_info(dev, "dev found: no attached "
594 "dev:%016llx at ex:%016llx\n",
595 SAS_ADDR(device->sas_addr),
596 SAS_ADDR(parent_dev->sas_addr));
597 return -EINVAL;
598 }
599 }
600
Xiang Chenf1c88212017-12-09 01:16:41 +0800601 dev_info(dev, "dev[%d:%x] found\n",
602 sas_dev->device_id, sas_dev->dev_type);
603
John Garryabda97c2015-11-18 00:50:51 +0800604 return 0;
605}
606
John Garry31eec8a2016-02-25 17:42:14 +0800607static int hisi_sas_slave_configure(struct scsi_device *sdev)
608{
609 struct domain_device *dev = sdev_to_domain_dev(sdev);
610 int ret = sas_slave_configure(sdev);
611
612 if (ret)
613 return ret;
614 if (!dev_is_sata(dev))
615 sas_change_queue_depth(sdev, 64);
616
617 return 0;
618}
619
John Garry701f75e2015-11-18 00:50:55 +0800620static void hisi_sas_scan_start(struct Scsi_Host *shost)
621{
622 struct hisi_hba *hisi_hba = shost_priv(shost);
John Garry701f75e2015-11-18 00:50:55 +0800623
John Garry396b8042017-03-23 01:25:19 +0800624 hisi_hba->hw->phys_init(hisi_hba);
John Garry701f75e2015-11-18 00:50:55 +0800625}
626
627static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
628{
629 struct hisi_hba *hisi_hba = shost_priv(shost);
630 struct sas_ha_struct *sha = &hisi_hba->sha;
631
John Garry396b8042017-03-23 01:25:19 +0800632 /* Wait for PHY up interrupt to occur */
633 if (time < HZ)
John Garry701f75e2015-11-18 00:50:55 +0800634 return 0;
635
636 sas_drain_work(sha);
637 return 1;
638}
639
John Garry66139922015-11-18 00:50:48 +0800640static void hisi_sas_phyup_work(struct work_struct *work)
641{
642 struct hisi_sas_phy *phy =
Xiaofei Tane537b622017-12-09 01:16:44 +0800643 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
John Garry66139922015-11-18 00:50:48 +0800644 struct hisi_hba *hisi_hba = phy->hisi_hba;
645 struct asd_sas_phy *sas_phy = &phy->sas_phy;
646 int phy_no = sas_phy->id;
647
648 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
649 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
650}
John Garry976867e2015-11-18 00:50:42 +0800651
Xiaofei Tan057c3d12017-12-09 01:16:45 +0800652static void hisi_sas_linkreset_work(struct work_struct *work)
653{
654 struct hisi_sas_phy *phy =
655 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
656 struct asd_sas_phy *sas_phy = &phy->sas_phy;
657
658 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
659}
660
Xiaofei Tane537b622017-12-09 01:16:44 +0800661static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
662 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
Xiaofei Tan057c3d12017-12-09 01:16:45 +0800663 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
Xiaofei Tane537b622017-12-09 01:16:44 +0800664};
665
666bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
667 enum hisi_sas_phy_event event)
668{
669 struct hisi_hba *hisi_hba = phy->hisi_hba;
670
671 if (WARN_ON(event >= HISI_PHYES_NUM))
672 return false;
673
674 return queue_work(hisi_hba->wq, &phy->works[event]);
675}
676EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
677
John Garry976867e2015-11-18 00:50:42 +0800678static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
679{
680 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
681 struct asd_sas_phy *sas_phy = &phy->sas_phy;
Xiaofei Tane537b622017-12-09 01:16:44 +0800682 int i;
John Garry976867e2015-11-18 00:50:42 +0800683
684 phy->hisi_hba = hisi_hba;
685 phy->port = NULL;
John Garry976867e2015-11-18 00:50:42 +0800686 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
687 sas_phy->class = SAS;
688 sas_phy->iproto = SAS_PROTOCOL_ALL;
689 sas_phy->tproto = 0;
690 sas_phy->type = PHY_TYPE_PHYSICAL;
691 sas_phy->role = PHY_ROLE_INITIATOR;
692 sas_phy->oob_mode = OOB_NOT_CONNECTED;
693 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
694 sas_phy->id = phy_no;
695 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
696 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
697 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
698 sas_phy->lldd_phy = phy;
John Garry66139922015-11-18 00:50:48 +0800699
Xiaofei Tane537b622017-12-09 01:16:44 +0800700 for (i = 0; i < HISI_PHYES_NUM; i++)
701 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
John Garry976867e2015-11-18 00:50:42 +0800702}
703
John Garry184a4632015-11-18 00:50:52 +0800704static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
705{
706 struct sas_ha_struct *sas_ha = sas_phy->ha;
707 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
708 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
709 struct asd_sas_port *sas_port = sas_phy->port;
John Garry2e244f02017-03-23 01:25:17 +0800710 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
John Garry184a4632015-11-18 00:50:52 +0800711 unsigned long flags;
712
713 if (!sas_port)
714 return;
715
716 spin_lock_irqsave(&hisi_hba->lock, flags);
717 port->port_attached = 1;
718 port->id = phy->port_id;
719 phy->port = port;
720 sas_port->lldd_port = port;
721 spin_unlock_irqrestore(&hisi_hba->lock, flags);
722}
723
John Garryd3c4dd42017-04-10 21:22:00 +0800724static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
John Garry405314d2017-03-23 01:25:21 +0800725 struct hisi_sas_slot *slot)
John Garry184a4632015-11-18 00:50:52 +0800726{
John Garryd3c4dd42017-04-10 21:22:00 +0800727 if (task) {
728 unsigned long flags;
729 struct task_status_struct *ts;
John Garry184a4632015-11-18 00:50:52 +0800730
John Garryd3c4dd42017-04-10 21:22:00 +0800731 ts = &task->task_status;
John Garry184a4632015-11-18 00:50:52 +0800732
John Garryd3c4dd42017-04-10 21:22:00 +0800733 ts->resp = SAS_TASK_COMPLETE;
734 ts->stat = SAS_ABORTED_TASK;
735 spin_lock_irqsave(&task->task_state_lock, flags);
736 task->task_state_flags &=
737 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
738 task->task_state_flags |= SAS_TASK_STATE_DONE;
739 spin_unlock_irqrestore(&task->task_state_lock, flags);
740 }
John Garry184a4632015-11-18 00:50:52 +0800741
John Garry405314d2017-03-23 01:25:21 +0800742 hisi_sas_slot_task_free(hisi_hba, task, slot);
John Garry184a4632015-11-18 00:50:52 +0800743}
744
John Garry405314d2017-03-23 01:25:21 +0800745/* hisi_hba.lock should be locked */
John Garry184a4632015-11-18 00:50:52 +0800746static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
747 struct domain_device *device)
748{
John Garry405314d2017-03-23 01:25:21 +0800749 struct hisi_sas_slot *slot, *slot2;
750 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry184a4632015-11-18 00:50:52 +0800751
John Garry405314d2017-03-23 01:25:21 +0800752 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
753 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
John Garry184a4632015-11-18 00:50:52 +0800754}
755
Xiang Chen4d0951e2017-12-09 01:16:50 +0800756void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800757{
John Garry405314d2017-03-23 01:25:21 +0800758 struct hisi_sas_device *sas_dev;
759 struct domain_device *device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800760 int i;
761
John Garry405314d2017-03-23 01:25:21 +0800762 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
763 sas_dev = &hisi_hba->devices[i];
764 device = sas_dev->sas_device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800765
John Garry405314d2017-03-23 01:25:21 +0800766 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
767 !device)
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800768 continue;
John Garry405314d2017-03-23 01:25:21 +0800769
770 hisi_sas_release_task(hisi_hba, device);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800771 }
772}
Xiang Chen4d0951e2017-12-09 01:16:50 +0800773EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800774
Xiang Chend30ff262017-06-14 23:33:32 +0800775static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
776 struct domain_device *device)
777{
778 if (hisi_hba->hw->dereg_device)
779 hisi_hba->hw->dereg_device(hisi_hba, device);
780}
781
John Garryabda97c2015-11-18 00:50:51 +0800782static void hisi_sas_dev_gone(struct domain_device *device)
783{
784 struct hisi_sas_device *sas_dev = device->lldd_dev;
785 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
John Garry11b75242017-06-14 23:33:17 +0800786 struct device *dev = hisi_hba->dev;
John Garryabda97c2015-11-18 00:50:51 +0800787
Xiang Chenf1c88212017-12-09 01:16:41 +0800788 dev_info(dev, "dev[%d:%x] is gone\n",
John Garryabda97c2015-11-18 00:50:51 +0800789 sas_dev->device_id, sas_dev->dev_type);
790
Xiang Chenf8e45ec2017-12-09 01:16:37 +0800791 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
792 hisi_sas_internal_task_abort(hisi_hba, device,
John Garry40f27022016-08-24 19:05:48 +0800793 HISI_SAS_INT_ABT_DEV, 0);
794
Xiang Chenf8e45ec2017-12-09 01:16:37 +0800795 hisi_sas_dereg_device(hisi_hba, device);
Xiang Chend30ff262017-06-14 23:33:32 +0800796
Xiang Chenf8e45ec2017-12-09 01:16:37 +0800797 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
798 device->lldd_dev = NULL;
799 memset(sas_dev, 0, sizeof(*sas_dev));
800 }
801
Xiaofei Tan02581412017-12-09 01:16:34 +0800802 if (hisi_hba->hw->free_device)
803 hisi_hba->hw->free_device(sas_dev);
John Garryabda97c2015-11-18 00:50:51 +0800804 sas_dev->dev_type = SAS_PHY_UNUSED;
John Garryabda97c2015-11-18 00:50:51 +0800805}
John Garry42e7a692015-11-18 00:50:49 +0800806
807static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
808{
809 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
810}
811
John Garrye4189d52015-11-18 00:50:57 +0800812static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
813 void *funcdata)
814{
815 struct sas_ha_struct *sas_ha = sas_phy->ha;
816 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
817 int phy_no = sas_phy->id;
818
819 switch (func) {
820 case PHY_FUNC_HARD_RESET:
821 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
822 break;
823
824 case PHY_FUNC_LINK_RESET:
John Garryb4c67a62017-03-23 01:25:23 +0800825 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
826 msleep(100);
Xiang Chen1eb8eea2017-10-24 23:51:36 +0800827 hisi_hba->hw->phy_start(hisi_hba, phy_no);
John Garrye4189d52015-11-18 00:50:57 +0800828 break;
829
830 case PHY_FUNC_DISABLE:
831 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
832 break;
833
834 case PHY_FUNC_SET_LINK_RATE:
Xiang Chen2ae75782016-11-07 20:48:40 +0800835 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
836 break;
Xiaofei Tanc52108c2017-08-11 00:09:29 +0800837 case PHY_FUNC_GET_EVENTS:
838 if (hisi_hba->hw->get_events) {
839 hisi_hba->hw->get_events(hisi_hba, phy_no);
840 break;
841 }
842 /* fallthru */
John Garrye4189d52015-11-18 00:50:57 +0800843 case PHY_FUNC_RELEASE_SPINUP_HOLD:
844 default:
845 return -EOPNOTSUPP;
846 }
847 return 0;
848}
John Garry184a4632015-11-18 00:50:52 +0800849
John Garry0efff302015-11-18 00:50:56 +0800850static void hisi_sas_task_done(struct sas_task *task)
851{
852 if (!del_timer(&task->slow_task->timer))
853 return;
854 complete(&task->slow_task->completion);
855}
856
Kees Cook77570ee2017-08-22 16:05:14 -0700857static void hisi_sas_tmf_timedout(struct timer_list *t)
John Garry0efff302015-11-18 00:50:56 +0800858{
Kees Cook77570ee2017-08-22 16:05:14 -0700859 struct sas_task_slow *slow = from_timer(slow, t, timer);
860 struct sas_task *task = slow->task;
Xiang Chenf64a6982017-06-14 23:33:11 +0800861 unsigned long flags;
John Garry0efff302015-11-18 00:50:56 +0800862
Xiang Chenf64a6982017-06-14 23:33:11 +0800863 spin_lock_irqsave(&task->task_state_lock, flags);
864 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
865 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
866 spin_unlock_irqrestore(&task->task_state_lock, flags);
867
John Garry0efff302015-11-18 00:50:56 +0800868 complete(&task->slow_task->completion);
869}
870
871#define TASK_TIMEOUT 20
872#define TASK_RETRY 3
873static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
874 void *parameter, u32 para_len,
875 struct hisi_sas_tmf_task *tmf)
876{
877 struct hisi_sas_device *sas_dev = device->lldd_dev;
878 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
John Garry11b75242017-06-14 23:33:17 +0800879 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +0800880 struct sas_task *task;
881 int res, retry;
882
883 for (retry = 0; retry < TASK_RETRY; retry++) {
884 task = sas_alloc_slow_task(GFP_KERNEL);
885 if (!task)
886 return -ENOMEM;
887
888 task->dev = device;
889 task->task_proto = device->tproto;
890
Xiang Chen7c594f02017-03-23 01:25:20 +0800891 if (dev_is_sata(device)) {
892 task->ata_task.device_control_reg_update = 1;
893 memcpy(&task->ata_task.fis, parameter, para_len);
894 } else {
895 memcpy(&task->ssp_task, parameter, para_len);
896 }
John Garry0efff302015-11-18 00:50:56 +0800897 task->task_done = hisi_sas_task_done;
898
Kees Cook841b86f2017-10-23 09:40:42 +0200899 task->slow_task->timer.function = hisi_sas_tmf_timedout;
John Garry0efff302015-11-18 00:50:56 +0800900 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
901 add_timer(&task->slow_task->timer);
902
903 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
904
905 if (res) {
906 del_timer(&task->slow_task->timer);
907 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
908 res);
909 goto ex_err;
910 }
911
912 wait_for_completion(&task->slow_task->completion);
913 res = TMF_RESP_FUNC_FAILED;
914 /* Even TMF timed out, return direct. */
915 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
916 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
John Garryd3c4dd42017-04-10 21:22:00 +0800917 struct hisi_sas_slot *slot = task->lldd_task;
918
Xiang Chenf1c88212017-12-09 01:16:41 +0800919 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
John Garryd3c4dd42017-04-10 21:22:00 +0800920 if (slot)
921 slot->task = NULL;
922
John Garry0efff302015-11-18 00:50:56 +0800923 goto ex_err;
Xiang Chenf1c88212017-12-09 01:16:41 +0800924 } else
925 dev_err(dev, "abort tmf: TMF task timeout\n");
John Garry0efff302015-11-18 00:50:56 +0800926 }
927
928 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800929 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800930 res = TMF_RESP_FUNC_COMPLETE;
931 break;
932 }
933
934 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800935 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
936 res = TMF_RESP_FUNC_SUCC;
937 break;
938 }
939
940 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800941 task->task_status.stat == SAS_DATA_UNDERRUN) {
942 /* no error, but return the number of bytes of
943 * underrun
944 */
945 dev_warn(dev, "abort tmf: task to dev %016llx "
946 "resp: 0x%x sts 0x%x underrun\n",
947 SAS_ADDR(device->sas_addr),
948 task->task_status.resp,
949 task->task_status.stat);
950 res = task->task_status.residual;
951 break;
952 }
953
954 if (task->task_status.resp == SAS_TASK_COMPLETE &&
955 task->task_status.stat == SAS_DATA_OVERRUN) {
956 dev_warn(dev, "abort tmf: blocked task error\n");
957 res = -EMSGSIZE;
958 break;
959 }
960
961 dev_warn(dev, "abort tmf: task to dev "
962 "%016llx resp: 0x%x status 0x%x\n",
963 SAS_ADDR(device->sas_addr), task->task_status.resp,
964 task->task_status.stat);
965 sas_free_task(task);
966 task = NULL;
967 }
968ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800969 if (retry == TASK_RETRY)
970 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800971 sas_free_task(task);
972 return res;
973}
974
Xiang Chen7c594f02017-03-23 01:25:20 +0800975static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
976 bool reset, int pmp, u8 *fis)
977{
978 struct ata_taskfile tf;
979
980 ata_tf_init(dev, &tf);
981 if (reset)
982 tf.ctl |= ATA_SRST;
983 else
984 tf.ctl &= ~ATA_SRST;
985 tf.command = ATA_CMD_DEV_RESET;
986 ata_tf_to_fis(&tf, pmp, 0, fis);
987}
988
989static int hisi_sas_softreset_ata_disk(struct domain_device *device)
990{
991 u8 fis[20] = {0};
992 struct ata_port *ap = device->sata_dev.ap;
993 struct ata_link *link;
994 int rc = TMF_RESP_FUNC_FAILED;
995 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
John Garry11b75242017-06-14 23:33:17 +0800996 struct device *dev = hisi_hba->dev;
Xiang Chen7c594f02017-03-23 01:25:20 +0800997 int s = sizeof(struct host_to_dev_fis);
998 unsigned long flags;
999
1000 ata_for_each_link(link, ap, EDGE) {
1001 int pmp = sata_srst_pmp(link);
1002
1003 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1004 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1005 if (rc != TMF_RESP_FUNC_COMPLETE)
1006 break;
1007 }
1008
1009 if (rc == TMF_RESP_FUNC_COMPLETE) {
1010 ata_for_each_link(link, ap, EDGE) {
1011 int pmp = sata_srst_pmp(link);
1012
1013 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1014 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1015 s, NULL);
1016 if (rc != TMF_RESP_FUNC_COMPLETE)
1017 dev_err(dev, "ata disk de-reset failed\n");
1018 }
1019 } else {
1020 dev_err(dev, "ata disk reset failed\n");
1021 }
1022
1023 if (rc == TMF_RESP_FUNC_COMPLETE) {
1024 spin_lock_irqsave(&hisi_hba->lock, flags);
1025 hisi_sas_release_task(hisi_hba, device);
1026 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1027 }
1028
1029 return rc;
1030}
1031
John Garry0efff302015-11-18 00:50:56 +08001032static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1033 u8 *lun, struct hisi_sas_tmf_task *tmf)
1034{
1035 struct sas_ssp_task ssp_task;
1036
1037 if (!(device->tproto & SAS_PROTOCOL_SSP))
1038 return TMF_RESP_FUNC_ESUPP;
1039
1040 memcpy(ssp_task.LUN, lun, 8);
1041
1042 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1043 sizeof(ssp_task), tmf);
1044}
1045
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001046static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001047{
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001048 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001049 int i;
1050
1051 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001052 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1053 struct domain_device *device = sas_dev->sas_device;
1054 struct asd_sas_port *sas_port;
1055 struct hisi_sas_port *port;
1056 struct hisi_sas_phy *phy = NULL;
1057 struct asd_sas_phy *sas_phy;
1058
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001059 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001060 || !device || !device->port)
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001061 continue;
1062
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001063 sas_port = device->port;
1064 port = to_hisi_sas_port(sas_port);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001065
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001066 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1067 if (state & BIT(sas_phy->id)) {
1068 phy = sas_phy->lldd_phy;
1069 break;
1070 }
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001071
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001072 if (phy) {
1073 port->id = phy->port_id;
1074
1075 /* Update linkrate of directly attached device. */
1076 if (!device->parent)
1077 device->linkrate = phy->sas_phy.linkrate;
1078
1079 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1080 } else
1081 port->id = 0xff;
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001082 }
1083}
1084
1085static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1086 u32 state)
1087{
1088 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1089 struct asd_sas_port *_sas_port = NULL;
1090 int phy_no;
1091
1092 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1093 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1094 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1095 struct asd_sas_port *sas_port = sas_phy->port;
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001096 bool do_port_check = !!(_sas_port != sas_port);
1097
1098 if (!sas_phy->phy->enabled)
1099 continue;
1100
1101 /* Report PHY state change to libsas */
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001102 if (state & BIT(phy_no)) {
1103 if (do_port_check && sas_port && sas_port->port_dev) {
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001104 struct domain_device *dev = sas_port->port_dev;
1105
1106 _sas_port = sas_port;
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001107
1108 if (DEV_IS_EXPANDER(dev->dev_type))
1109 sas_ha->notify_port_event(sas_phy,
1110 PORTE_BROADCAST_RCVD);
1111 }
1112 } else if (old_state & (1 << phy_no))
1113 /* PHY down but was up before */
1114 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1115
1116 }
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001117}
1118
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001119static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1120{
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001121 struct device *dev = hisi_hba->dev;
1122 struct Scsi_Host *shost = hisi_hba->shost;
1123 u32 old_state, state;
1124 unsigned long flags;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001125 int rc;
1126
1127 if (!hisi_hba->hw->soft_reset)
1128 return -1;
1129
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001130 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001131 return -1;
1132
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001133 dev_info(dev, "controller resetting...\n");
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001134 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1135
1136 scsi_block_requests(shost);
1137 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1138 rc = hisi_hba->hw->soft_reset(hisi_hba);
1139 if (rc) {
1140 dev_warn(dev, "controller reset failed (%d)\n", rc);
1141 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001142 scsi_unblock_requests(shost);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001143 goto out;
1144 }
1145 spin_lock_irqsave(&hisi_hba->lock, flags);
1146 hisi_sas_release_tasks(hisi_hba);
1147 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1148
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001149 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1150
1151 /* Init and wait for PHYs to come up and all libsas event finished. */
1152 hisi_hba->hw->phys_init(hisi_hba);
1153 msleep(1000);
Xiaofei Tana669bdb2017-12-09 01:16:35 +08001154 hisi_sas_refresh_port_id(hisi_hba);
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001155 scsi_unblock_requests(shost);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001156
1157 state = hisi_hba->hw->get_phys_state(hisi_hba);
1158 hisi_sas_rescan_topology(hisi_hba, old_state, state);
Xiaofei Tanfb51e7a2017-12-09 01:16:36 +08001159 dev_info(dev, "controller reset complete\n");
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001160
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001161out:
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001162 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001163
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001164 return rc;
1165}
1166
John Garry0efff302015-11-18 00:50:56 +08001167static int hisi_sas_abort_task(struct sas_task *task)
1168{
1169 struct scsi_lun lun;
1170 struct hisi_sas_tmf_task tmf_task;
1171 struct domain_device *device = task->dev;
1172 struct hisi_sas_device *sas_dev = device->lldd_dev;
1173 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
John Garry11b75242017-06-14 23:33:17 +08001174 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +08001175 int rc = TMF_RESP_FUNC_FAILED;
1176 unsigned long flags;
1177
1178 if (!sas_dev) {
1179 dev_warn(dev, "Device has been removed\n");
1180 return TMF_RESP_FUNC_FAILED;
1181 }
1182
John Garry0efff302015-11-18 00:50:56 +08001183 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
John Garry0efff302015-11-18 00:50:56 +08001184 rc = TMF_RESP_FUNC_COMPLETE;
1185 goto out;
1186 }
1187
John Garry0efff302015-11-18 00:50:56 +08001188 sas_dev->dev_status = HISI_SAS_DEV_EH;
1189 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1190 struct scsi_cmnd *cmnd = task->uldd_task;
1191 struct hisi_sas_slot *slot = task->lldd_task;
1192 u32 tag = slot->idx;
John Garryc35279f2017-03-23 01:25:25 +08001193 int rc2;
John Garry0efff302015-11-18 00:50:56 +08001194
1195 int_to_scsilun(cmnd->device->lun, &lun);
1196 tmf_task.tmf = TMF_ABORT_TASK;
1197 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1198
1199 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1200 &tmf_task);
1201
John Garryc35279f2017-03-23 01:25:25 +08001202 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1203 HISI_SAS_INT_ABT_CMD, tag);
Xiaofei Tan813709f2017-12-09 01:16:46 +08001204 if (rc2 < 0) {
1205 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1206 return TMF_RESP_FUNC_FAILED;
1207 }
1208
John Garryc35279f2017-03-23 01:25:25 +08001209 /*
1210 * If the TMF finds that the IO is not in the device and also
1211 * the internal abort does not succeed, then it is safe to
1212 * free the slot.
1213 * Note: if the internal abort succeeds then the slot
1214 * will have already been completed
1215 */
1216 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
John Garry0efff302015-11-18 00:50:56 +08001217 if (task->lldd_task) {
John Garry0efff302015-11-18 00:50:56 +08001218 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garryc35279f2017-03-23 01:25:25 +08001219 hisi_sas_do_release_task(hisi_hba, task, slot);
John Garry0efff302015-11-18 00:50:56 +08001220 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1221 }
1222 }
John Garry0efff302015-11-18 00:50:56 +08001223 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1224 task->task_proto & SAS_PROTOCOL_STP) {
1225 if (task->dev->dev_type == SAS_SATA_DEV) {
Xiaofei Tan813709f2017-12-09 01:16:46 +08001226 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1227 HISI_SAS_INT_ABT_DEV, 0);
1228 if (rc < 0) {
1229 dev_err(dev, "abort task: internal abort failed\n");
1230 goto out;
1231 }
Xiang Chend30ff262017-06-14 23:33:32 +08001232 hisi_sas_dereg_device(hisi_hba, device);
Xiang Chen7c594f02017-03-23 01:25:20 +08001233 rc = hisi_sas_softreset_ata_disk(device);
John Garry0efff302015-11-18 00:50:56 +08001234 }
Gustavo A. R. Silvaeb045e02017-05-22 13:00:29 -05001235 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
John Garrydc8a49c2016-08-24 19:05:49 +08001236 /* SMP */
1237 struct hisi_sas_slot *slot = task->lldd_task;
1238 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +08001239
Xiang Chenccbfe5a2017-03-23 01:25:36 +08001240 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1241 HISI_SAS_INT_ABT_CMD, tag);
Xiaofei Tan813709f2017-12-09 01:16:46 +08001242 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1243 task->lldd_task) {
Xiang Chenccbfe5a2017-03-23 01:25:36 +08001244 spin_lock_irqsave(&hisi_hba->lock, flags);
1245 hisi_sas_do_release_task(hisi_hba, task, slot);
1246 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1247 }
John Garry0efff302015-11-18 00:50:56 +08001248 }
1249
1250out:
1251 if (rc != TMF_RESP_FUNC_COMPLETE)
1252 dev_notice(dev, "abort task: rc=%d\n", rc);
1253 return rc;
1254}
1255
1256static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1257{
Xiaofei Tan2a038132017-12-09 01:16:47 +08001258 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1259 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +08001260 struct hisi_sas_tmf_task tmf_task;
1261 int rc = TMF_RESP_FUNC_FAILED;
Xiaofei Tan2a038132017-12-09 01:16:47 +08001262 unsigned long flags;
1263
1264 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1265 HISI_SAS_INT_ABT_DEV, 0);
1266 if (rc < 0) {
1267 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1268 return TMF_RESP_FUNC_FAILED;
1269 }
1270 hisi_sas_dereg_device(hisi_hba, device);
John Garry0efff302015-11-18 00:50:56 +08001271
1272 tmf_task.tmf = TMF_ABORT_TASK_SET;
1273 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1274
Xiaofei Tan2a038132017-12-09 01:16:47 +08001275 if (rc == TMF_RESP_FUNC_COMPLETE) {
1276 spin_lock_irqsave(&hisi_hba->lock, flags);
1277 hisi_sas_release_task(hisi_hba, device);
1278 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1279 }
1280
John Garry0efff302015-11-18 00:50:56 +08001281 return rc;
1282}
1283
1284static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1285{
1286 int rc = TMF_RESP_FUNC_FAILED;
1287 struct hisi_sas_tmf_task tmf_task;
1288
1289 tmf_task.tmf = TMF_CLEAR_ACA;
1290 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1291
1292 return rc;
1293}
1294
1295static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1296{
1297 struct sas_phy *phy = sas_get_local_phy(device);
1298 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1299 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1300 rc = sas_phy_reset(phy, reset_type);
1301 sas_put_local_phy(phy);
1302 msleep(2000);
1303 return rc;
1304}
1305
1306static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1307{
1308 struct hisi_sas_device *sas_dev = device->lldd_dev;
1309 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
Xiaofei Tan813709f2017-12-09 01:16:46 +08001310 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +08001311 int rc = TMF_RESP_FUNC_FAILED;
Xiaofei Tan813709f2017-12-09 01:16:46 +08001312 unsigned long flags;
John Garry0efff302015-11-18 00:50:56 +08001313
1314 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1315 return TMF_RESP_FUNC_FAILED;
1316 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1317
Xiaofei Tan813709f2017-12-09 01:16:46 +08001318 rc = hisi_sas_internal_task_abort(hisi_hba, device,
Xiang Chend30ff262017-06-14 23:33:32 +08001319 HISI_SAS_INT_ABT_DEV, 0);
Xiaofei Tan813709f2017-12-09 01:16:46 +08001320 if (rc < 0) {
1321 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1322 return TMF_RESP_FUNC_FAILED;
1323 }
Xiang Chend30ff262017-06-14 23:33:32 +08001324 hisi_sas_dereg_device(hisi_hba, device);
1325
John Garry0efff302015-11-18 00:50:56 +08001326 rc = hisi_sas_debug_I_T_nexus_reset(device);
1327
Xiang Chen9960a242017-12-09 01:16:48 +08001328 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) {
Xiang Chen61312432017-03-23 01:25:28 +08001329 spin_lock_irqsave(&hisi_hba->lock, flags);
1330 hisi_sas_release_task(hisi_hba, device);
1331 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1332 }
1333 return rc;
John Garry0efff302015-11-18 00:50:56 +08001334}
1335
1336static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1337{
John Garry0efff302015-11-18 00:50:56 +08001338 struct hisi_sas_device *sas_dev = device->lldd_dev;
1339 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
John Garry11b75242017-06-14 23:33:17 +08001340 struct device *dev = hisi_hba->dev;
John Garry0efff302015-11-18 00:50:56 +08001341 unsigned long flags;
1342 int rc = TMF_RESP_FUNC_FAILED;
1343
John Garry0efff302015-11-18 00:50:56 +08001344 sas_dev->dev_status = HISI_SAS_DEV_EH;
John Garry055945d2017-03-23 01:25:26 +08001345 if (dev_is_sata(device)) {
1346 struct sas_phy *phy;
John Garry0efff302015-11-18 00:50:56 +08001347
John Garry055945d2017-03-23 01:25:26 +08001348 /* Clear internal IO and then hardreset */
1349 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1350 HISI_SAS_INT_ABT_DEV, 0);
Xiaofei Tan813709f2017-12-09 01:16:46 +08001351 if (rc < 0) {
1352 dev_err(dev, "lu_reset: internal abort failed\n");
John Garry055945d2017-03-23 01:25:26 +08001353 goto out;
Xiaofei Tan813709f2017-12-09 01:16:46 +08001354 }
Xiang Chend30ff262017-06-14 23:33:32 +08001355 hisi_sas_dereg_device(hisi_hba, device);
John Garry055945d2017-03-23 01:25:26 +08001356
1357 phy = sas_get_local_phy(device);
1358
1359 rc = sas_phy_reset(phy, 1);
1360
1361 if (rc == 0) {
1362 spin_lock_irqsave(&hisi_hba->lock, flags);
1363 hisi_sas_release_task(hisi_hba, device);
1364 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1365 }
1366 sas_put_local_phy(phy);
1367 } else {
1368 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1369
Xiaofei Tan2a038132017-12-09 01:16:47 +08001370 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1371 HISI_SAS_INT_ABT_DEV, 0);
1372 if (rc < 0) {
1373 dev_err(dev, "lu_reset: internal abort failed\n");
1374 goto out;
1375 }
1376 hisi_sas_dereg_device(hisi_hba, device);
1377
John Garry055945d2017-03-23 01:25:26 +08001378 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1379 if (rc == TMF_RESP_FUNC_COMPLETE) {
1380 spin_lock_irqsave(&hisi_hba->lock, flags);
1381 hisi_sas_release_task(hisi_hba, device);
1382 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1383 }
1384 }
1385out:
John Garry14d3f392017-03-23 01:25:37 +08001386 if (rc != TMF_RESP_FUNC_COMPLETE)
John Garryad604832017-06-14 23:33:12 +08001387 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
John Garry14d3f392017-03-23 01:25:37 +08001388 sas_dev->device_id, rc);
John Garry0efff302015-11-18 00:50:56 +08001389 return rc;
1390}
1391
John Garry8b05ad62017-03-23 01:25:35 +08001392static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1393{
1394 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
Xiaofei Tane402acd2017-12-09 01:16:38 +08001395 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
John Garry8b05ad62017-03-23 01:25:35 +08001396
Xiaofei Tane402acd2017-12-09 01:16:38 +08001397 queue_work(hisi_hba->wq, &r.work);
1398 wait_for_completion(r.completion);
1399 if (r.done)
1400 return TMF_RESP_FUNC_COMPLETE;
1401
1402 return TMF_RESP_FUNC_FAILED;
John Garry8b05ad62017-03-23 01:25:35 +08001403}
1404
John Garry0efff302015-11-18 00:50:56 +08001405static int hisi_sas_query_task(struct sas_task *task)
1406{
1407 struct scsi_lun lun;
1408 struct hisi_sas_tmf_task tmf_task;
1409 int rc = TMF_RESP_FUNC_FAILED;
1410
1411 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1412 struct scsi_cmnd *cmnd = task->uldd_task;
1413 struct domain_device *device = task->dev;
1414 struct hisi_sas_slot *slot = task->lldd_task;
1415 u32 tag = slot->idx;
1416
1417 int_to_scsilun(cmnd->device->lun, &lun);
1418 tmf_task.tmf = TMF_QUERY_TASK;
1419 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1420
1421 rc = hisi_sas_debug_issue_ssp_tmf(device,
1422 lun.scsi_lun,
1423 &tmf_task);
1424 switch (rc) {
1425 /* The task is still in Lun, release it then */
1426 case TMF_RESP_FUNC_SUCC:
1427 /* The task is not in Lun or failed, reset the phy */
1428 case TMF_RESP_FUNC_FAILED:
1429 case TMF_RESP_FUNC_COMPLETE:
1430 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001431 default:
1432 rc = TMF_RESP_FUNC_FAILED;
1433 break;
John Garry0efff302015-11-18 00:50:56 +08001434 }
1435 }
1436 return rc;
1437}
1438
John Garry441c2742016-08-24 19:05:47 +08001439static int
John Garryad604832017-06-14 23:33:12 +08001440hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
John Garry441c2742016-08-24 19:05:47 +08001441 struct sas_task *task, int abort_flag,
1442 int task_tag)
1443{
1444 struct domain_device *device = task->dev;
1445 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry11b75242017-06-14 23:33:17 +08001446 struct device *dev = hisi_hba->dev;
John Garry441c2742016-08-24 19:05:47 +08001447 struct hisi_sas_port *port;
1448 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001449 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001450 struct hisi_sas_cmd_hdr *cmd_hdr_base;
Xiang Chenb1a49412017-06-14 23:33:13 +08001451 struct hisi_sas_dq *dq = sas_dev->dq;
John Garry441c2742016-08-24 19:05:47 +08001452 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
Xiang Chenb1a49412017-06-14 23:33:13 +08001453 unsigned long flags, flags_dq;
John Garry441c2742016-08-24 19:05:47 +08001454
Xiaofei Tan917d3bd2017-08-11 00:09:26 +08001455 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001456 return -EINVAL;
1457
John Garry441c2742016-08-24 19:05:47 +08001458 if (!device->port)
1459 return -1;
1460
John Garry2e244f02017-03-23 01:25:17 +08001461 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001462
1463 /* simply get a slot and send abort command */
Xiang Chenb1a49412017-06-14 23:33:13 +08001464 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001465 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +08001466 if (rc) {
1467 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001468 goto err_out;
Xiang Chenb1a49412017-06-14 23:33:13 +08001469 }
1470 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1471
1472 spin_lock_irqsave(&dq->lock, flags_dq);
1473 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
John Garry441c2742016-08-24 19:05:47 +08001474 if (rc)
1475 goto err_out_tag;
1476
Xiang Chenb1a49412017-06-14 23:33:13 +08001477 dlvry_queue = dq->id;
1478 dlvry_queue_slot = dq->wr_point;
1479
John Garry441c2742016-08-24 19:05:47 +08001480 slot = &hisi_hba->slot_info[slot_idx];
1481 memset(slot, 0, sizeof(struct hisi_sas_slot));
1482
1483 slot->idx = slot_idx;
1484 slot->n_elem = n_elem;
1485 slot->dlvry_queue = dlvry_queue;
1486 slot->dlvry_queue_slot = dlvry_queue_slot;
1487 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1488 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1489 slot->task = task;
1490 slot->port = port;
1491 task->lldd_task = slot;
1492
Xiang Chen031da092017-08-11 00:09:35 +08001493 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1494 GFP_ATOMIC, &slot->buf_dma);
1495 if (!slot->buf) {
1496 rc = -ENOMEM;
1497 goto err_out_tag;
1498 }
1499
John Garry441c2742016-08-24 19:05:47 +08001500 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
Xiang Chen031da092017-08-11 00:09:35 +08001501 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1502 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
John Garry441c2742016-08-24 19:05:47 +08001503
1504 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1505 abort_flag, task_tag);
1506 if (rc)
Xiang Chen031da092017-08-11 00:09:35 +08001507 goto err_out_buf;
John Garry441c2742016-08-24 19:05:47 +08001508
Xiang Chen9feaf902017-10-24 23:51:34 +08001509 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry405314d2017-03-23 01:25:21 +08001510 list_add_tail(&slot->entry, &sas_dev->list);
Xiang Chen9feaf902017-10-24 23:51:34 +08001511 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garry54c9dd22017-03-23 01:25:29 +08001512 spin_lock_irqsave(&task->task_state_lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001513 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
John Garry54c9dd22017-03-23 01:25:29 +08001514 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001515
Xiang Chenb1a49412017-06-14 23:33:13 +08001516 dq->slot_prep = slot;
John Garry441c2742016-08-24 19:05:47 +08001517
John Garryf696cc32016-11-07 20:48:39 +08001518 atomic64_inc(&sas_dev->running_req);
1519
Xiang Chenb1a49412017-06-14 23:33:13 +08001520 /* send abort command to the chip */
1521 hisi_hba->hw->start_delivery(dq);
1522 spin_unlock_irqrestore(&dq->lock, flags_dq);
John Garry441c2742016-08-24 19:05:47 +08001523
1524 return 0;
1525
Xiang Chen031da092017-08-11 00:09:35 +08001526err_out_buf:
1527 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1528 slot->buf_dma);
John Garry441c2742016-08-24 19:05:47 +08001529err_out_tag:
Xiang Chenb1a49412017-06-14 23:33:13 +08001530 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001531 hisi_sas_slot_index_free(hisi_hba, slot_idx);
Xiang Chenb1a49412017-06-14 23:33:13 +08001532 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1533 spin_unlock_irqrestore(&dq->lock, flags_dq);
John Garry441c2742016-08-24 19:05:47 +08001534err_out:
1535 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1536
1537 return rc;
1538}
1539
1540/**
1541 * hisi_sas_internal_task_abort -- execute an internal
1542 * abort command for single IO command or a device
1543 * @hisi_hba: host controller struct
1544 * @device: domain device
1545 * @abort_flag: mode of operation, device or single IO
1546 * @tag: tag of IO to be aborted (only relevant to single
1547 * IO mode)
1548 */
1549static int
1550hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1551 struct domain_device *device,
1552 int abort_flag, int tag)
1553{
1554 struct sas_task *task;
1555 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry11b75242017-06-14 23:33:17 +08001556 struct device *dev = hisi_hba->dev;
John Garry441c2742016-08-24 19:05:47 +08001557 int res;
John Garry441c2742016-08-24 19:05:47 +08001558
Xiaofei Tan813709f2017-12-09 01:16:46 +08001559 /*
1560 * The interface is not realized means this HW don't support internal
1561 * abort, or don't need to do internal abort. Then here, we return
1562 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1563 * the internal abort has been executed and returned CQ.
1564 */
John Garry441c2742016-08-24 19:05:47 +08001565 if (!hisi_hba->hw->prep_abort)
Xiaofei Tan813709f2017-12-09 01:16:46 +08001566 return TMF_RESP_FUNC_FAILED;
John Garry441c2742016-08-24 19:05:47 +08001567
1568 task = sas_alloc_slow_task(GFP_KERNEL);
1569 if (!task)
1570 return -ENOMEM;
1571
1572 task->dev = device;
1573 task->task_proto = device->tproto;
1574 task->task_done = hisi_sas_task_done;
Kees Cook841b86f2017-10-23 09:40:42 +02001575 task->slow_task->timer.function = hisi_sas_tmf_timedout;
John Garry0844a3f2017-04-10 21:21:59 +08001576 task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
John Garry441c2742016-08-24 19:05:47 +08001577 add_timer(&task->slow_task->timer);
1578
John Garry441c2742016-08-24 19:05:47 +08001579 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1580 task, abort_flag, tag);
John Garry441c2742016-08-24 19:05:47 +08001581 if (res) {
1582 del_timer(&task->slow_task->timer);
1583 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1584 res);
1585 goto exit;
1586 }
1587 wait_for_completion(&task->slow_task->completion);
1588 res = TMF_RESP_FUNC_FAILED;
1589
Xiang Chenf64a6982017-06-14 23:33:11 +08001590 /* Internal abort timed out */
1591 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1592 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1593 struct hisi_sas_slot *slot = task->lldd_task;
1594
1595 if (slot)
1596 slot->task = NULL;
Xiang Chenf1c88212017-12-09 01:16:41 +08001597 dev_err(dev, "internal task abort: timeout and not done.\n");
Xiaofei Tan813709f2017-12-09 01:16:46 +08001598 res = -EIO;
Xiang Chenf692a672017-10-24 23:51:32 +08001599 goto exit;
Xiang Chenf1c88212017-12-09 01:16:41 +08001600 } else
1601 dev_err(dev, "internal task abort: timeout.\n");
Xiang Chenf64a6982017-06-14 23:33:11 +08001602 }
1603
John Garry441c2742016-08-24 19:05:47 +08001604 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1605 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1606 res = TMF_RESP_FUNC_COMPLETE;
1607 goto exit;
1608 }
1609
John Garryc35279f2017-03-23 01:25:25 +08001610 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1611 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1612 res = TMF_RESP_FUNC_SUCC;
1613 goto exit;
1614 }
1615
John Garry441c2742016-08-24 19:05:47 +08001616exit:
John Garry297d7302017-01-20 20:45:22 +08001617 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001618 "resp: 0x%x sts 0x%x\n",
1619 SAS_ADDR(device->sas_addr),
1620 task,
1621 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1622 task->task_status.stat);
1623 sas_free_task(task);
1624
1625 return res;
1626}
1627
John Garry184a4632015-11-18 00:50:52 +08001628static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1629{
1630 hisi_sas_port_notify_formed(sas_phy);
1631}
1632
Xiang Chen336bd782017-12-09 01:16:49 +08001633static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1634{
1635}
1636
John Garry184a4632015-11-18 00:50:52 +08001637static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1638{
1639 phy->phy_attached = 0;
1640 phy->phy_type = 0;
1641 phy->port = NULL;
1642}
1643
1644void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1645{
1646 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1647 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1648 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1649
1650 if (rdy) {
1651 /* Phy down but ready */
1652 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1653 hisi_sas_port_notify_formed(sas_phy);
1654 } else {
1655 struct hisi_sas_port *port = phy->port;
1656
1657 /* Phy down and not ready */
1658 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1659 sas_phy_disconnected(sas_phy);
1660
1661 if (port) {
1662 if (phy->phy_type & PORT_TYPE_SAS) {
1663 int port_id = port->id;
1664
1665 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1666 port_id))
1667 port->port_attached = 0;
1668 } else if (phy->phy_type & PORT_TYPE_SATA)
1669 port->port_attached = 0;
1670 }
1671 hisi_sas_phy_disconnected(phy);
1672 }
1673}
1674EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1675
Xiaofei Tan571295f2017-10-24 23:51:47 +08001676void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1677{
1678 int i;
1679
1680 for (i = 0; i < hisi_hba->queue_count; i++) {
1681 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1682
1683 tasklet_kill(&cq->tasklet);
1684 }
1685}
1686EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001687
John Garrye21fe3a2017-06-14 23:33:20 +08001688struct scsi_transport_template *hisi_sas_stt;
1689EXPORT_SYMBOL_GPL(hisi_sas_stt);
John Garrye8899fa2015-11-18 00:50:30 +08001690
Wei Yongjun1e15fea2018-01-11 11:13:58 +00001691static struct device_attribute *host_attrs[] = {
Jason Yan8eea9dd2017-12-08 17:42:06 +08001692 &dev_attr_phy_event_threshold,
1693 NULL,
1694};
1695
John Garrye21fe3a2017-06-14 23:33:20 +08001696static struct scsi_host_template _hisi_sas_sht = {
John Garry7eb78692015-11-18 00:50:31 +08001697 .module = THIS_MODULE,
1698 .name = DRV_NAME,
1699 .queuecommand = sas_queuecommand,
1700 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001701 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001702 .scan_finished = hisi_sas_scan_finished,
1703 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001704 .change_queue_depth = sas_change_queue_depth,
1705 .bios_param = sas_bios_param,
1706 .can_queue = 1,
1707 .this_id = -1,
1708 .sg_tablesize = SG_ALL,
1709 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1710 .use_clustering = ENABLE_CLUSTERING,
1711 .eh_device_reset_handler = sas_eh_device_reset_handler,
Hannes Reineckecc199e72017-08-25 13:57:02 +02001712 .eh_target_reset_handler = sas_eh_target_reset_handler,
John Garry7eb78692015-11-18 00:50:31 +08001713 .target_destroy = sas_target_destroy,
1714 .ioctl = sas_ioctl,
Jason Yan8eea9dd2017-12-08 17:42:06 +08001715 .shost_attrs = host_attrs,
John Garry7eb78692015-11-18 00:50:31 +08001716};
John Garrye21fe3a2017-06-14 23:33:20 +08001717struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1718EXPORT_SYMBOL_GPL(hisi_sas_sht);
John Garry7eb78692015-11-18 00:50:31 +08001719
John Garrye8899fa2015-11-18 00:50:30 +08001720static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001721 .lldd_dev_found = hisi_sas_dev_found,
1722 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001723 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001724 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001725 .lldd_abort_task = hisi_sas_abort_task,
1726 .lldd_abort_task_set = hisi_sas_abort_task_set,
1727 .lldd_clear_aca = hisi_sas_clear_aca,
1728 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1729 .lldd_lu_reset = hisi_sas_lu_reset,
1730 .lldd_query_task = hisi_sas_query_task,
John Garry8b05ad62017-03-23 01:25:35 +08001731 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
John Garry184a4632015-11-18 00:50:52 +08001732 .lldd_port_formed = hisi_sas_port_formed,
Xiang Chen336bd782017-12-09 01:16:49 +08001733 .lldd_port_deformed = hisi_sas_port_deformed,
John Garrye8899fa2015-11-18 00:50:30 +08001734};
1735
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001736void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1737{
1738 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1739
1740 for (i = 0; i < hisi_hba->queue_count; i++) {
1741 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1742 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1743
1744 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1745 memset(hisi_hba->cmd_hdr[i], 0, s);
1746 dq->wr_point = 0;
1747
1748 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1749 memset(hisi_hba->complete_hdr[i], 0, s);
1750 cq->rd_point = 0;
1751 }
1752
1753 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1754 memset(hisi_hba->initial_fis, 0, s);
1755
1756 s = max_command_entries * sizeof(struct hisi_sas_iost);
1757 memset(hisi_hba->iost, 0, s);
1758
1759 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1760 memset(hisi_hba->breakpoint, 0, s);
1761
Xiang Chen3297ded2017-10-24 23:51:35 +08001762 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001763 memset(hisi_hba->sata_breakpoint, 0, s);
1764}
1765EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1766
John Garrye21fe3a2017-06-14 23:33:20 +08001767int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
John Garry6be6de12015-11-18 00:50:34 +08001768{
John Garry11b75242017-06-14 23:33:17 +08001769 struct device *dev = hisi_hba->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001770 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001771
John Garryfa42d802015-11-18 00:50:43 +08001772 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001773 for (i = 0; i < hisi_hba->n_phy; i++) {
1774 hisi_sas_phy_init(hisi_hba, i);
1775 hisi_hba->port[i].port_attached = 0;
1776 hisi_hba->port[i].id = -1;
John Garry976867e2015-11-18 00:50:42 +08001777 }
1778
John Garryaf740db2015-11-18 00:50:41 +08001779 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1780 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1781 hisi_hba->devices[i].device_id = i;
1782 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1783 }
1784
John Garry6be6de12015-11-18 00:50:34 +08001785 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001786 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001787 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001788
1789 /* Completion queue structure */
1790 cq->id = i;
1791 cq->hisi_hba = hisi_hba;
1792
John Garry4fde02a2016-09-06 23:36:12 +08001793 /* Delivery queue structure */
Xiang Chen39bade02017-12-09 01:16:32 +08001794 spin_lock_init(&dq->lock);
John Garry4fde02a2016-09-06 23:36:12 +08001795 dq->id = i;
1796 dq->hisi_hba = hisi_hba;
1797
John Garry6be6de12015-11-18 00:50:34 +08001798 /* Delivery queue */
1799 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1800 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1801 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1802 if (!hisi_hba->cmd_hdr[i])
1803 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001804
1805 /* Completion queue */
1806 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1807 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1808 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1809 if (!hisi_hba->complete_hdr[i])
1810 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001811 }
1812
Xiaofei Tanf557e322017-06-29 21:02:14 +08001813 s = sizeof(struct hisi_sas_slot_buf_table);
1814 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1815 if (!hisi_hba->buffer_pool)
John Garry6be6de12015-11-18 00:50:34 +08001816 goto err_out;
1817
1818 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1819 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1820 GFP_KERNEL);
1821 if (!hisi_hba->itct)
1822 goto err_out;
1823
1824 memset(hisi_hba->itct, 0, s);
1825
John Garrya8d547b2016-01-26 02:47:03 +08001826 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001827 sizeof(struct hisi_sas_slot),
1828 GFP_KERNEL);
1829 if (!hisi_hba->slot_info)
1830 goto err_out;
1831
John Garrya8d547b2016-01-26 02:47:03 +08001832 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001833 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1834 GFP_KERNEL);
1835 if (!hisi_hba->iost)
1836 goto err_out;
1837
John Garrya8d547b2016-01-26 02:47:03 +08001838 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001839 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1840 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1841 if (!hisi_hba->breakpoint)
1842 goto err_out;
1843
John Garrya8d547b2016-01-26 02:47:03 +08001844 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001845 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001846 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1847 if (!hisi_hba->slot_index_tags)
1848 goto err_out;
1849
John Garry6be6de12015-11-18 00:50:34 +08001850 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1851 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1852 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1853 if (!hisi_hba->initial_fis)
1854 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001855
Xiang Chen3297ded2017-10-24 23:51:35 +08001856 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001857 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1858 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1859 if (!hisi_hba->sata_breakpoint)
1860 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001861 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001862
John Garry257efd12015-11-18 00:50:36 +08001863 hisi_sas_slot_index_init(hisi_hba);
1864
John Garry7e9080e2015-11-18 00:50:40 +08001865 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1866 if (!hisi_hba->wq) {
1867 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1868 goto err_out;
1869 }
1870
John Garry6be6de12015-11-18 00:50:34 +08001871 return 0;
1872err_out:
1873 return -ENOMEM;
1874}
John Garrye21fe3a2017-06-14 23:33:20 +08001875EXPORT_SYMBOL_GPL(hisi_sas_alloc);
John Garry6be6de12015-11-18 00:50:34 +08001876
John Garrye21fe3a2017-06-14 23:33:20 +08001877void hisi_sas_free(struct hisi_hba *hisi_hba)
John Garry89d53322015-11-18 00:50:35 +08001878{
John Garry11b75242017-06-14 23:33:17 +08001879 struct device *dev = hisi_hba->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001880 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001881
1882 for (i = 0; i < hisi_hba->queue_count; i++) {
1883 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1884 if (hisi_hba->cmd_hdr[i])
1885 dma_free_coherent(dev, s,
1886 hisi_hba->cmd_hdr[i],
1887 hisi_hba->cmd_hdr_dma[i]);
1888
1889 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1890 if (hisi_hba->complete_hdr[i])
1891 dma_free_coherent(dev, s,
1892 hisi_hba->complete_hdr[i],
1893 hisi_hba->complete_hdr_dma[i]);
1894 }
1895
Xiaofei Tanf557e322017-06-29 21:02:14 +08001896 dma_pool_destroy(hisi_hba->buffer_pool);
John Garry89d53322015-11-18 00:50:35 +08001897
1898 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1899 if (hisi_hba->itct)
1900 dma_free_coherent(dev, s,
1901 hisi_hba->itct, hisi_hba->itct_dma);
1902
John Garrya8d547b2016-01-26 02:47:03 +08001903 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001904 if (hisi_hba->iost)
1905 dma_free_coherent(dev, s,
1906 hisi_hba->iost, hisi_hba->iost_dma);
1907
John Garrya8d547b2016-01-26 02:47:03 +08001908 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001909 if (hisi_hba->breakpoint)
1910 dma_free_coherent(dev, s,
1911 hisi_hba->breakpoint,
1912 hisi_hba->breakpoint_dma);
1913
1914
1915 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1916 if (hisi_hba->initial_fis)
1917 dma_free_coherent(dev, s,
1918 hisi_hba->initial_fis,
1919 hisi_hba->initial_fis_dma);
1920
Xiang Chen3297ded2017-10-24 23:51:35 +08001921 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001922 if (hisi_hba->sata_breakpoint)
1923 dma_free_coherent(dev, s,
1924 hisi_hba->sata_breakpoint,
1925 hisi_hba->sata_breakpoint_dma);
1926
John Garry7e9080e2015-11-18 00:50:40 +08001927 if (hisi_hba->wq)
1928 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001929}
John Garrye21fe3a2017-06-14 23:33:20 +08001930EXPORT_SYMBOL_GPL(hisi_sas_free);
John Garry6be6de12015-11-18 00:50:34 +08001931
Xiaofei Tanb4241f02017-10-24 23:51:45 +08001932void hisi_sas_rst_work_handler(struct work_struct *work)
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001933{
1934 struct hisi_hba *hisi_hba =
1935 container_of(work, struct hisi_hba, rst_work);
1936
1937 hisi_sas_controller_reset(hisi_hba);
1938}
Xiaofei Tanb4241f02017-10-24 23:51:45 +08001939EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001940
Xiaofei Tane402acd2017-12-09 01:16:38 +08001941void hisi_sas_sync_rst_work_handler(struct work_struct *work)
1942{
1943 struct hisi_sas_rst *rst =
1944 container_of(work, struct hisi_sas_rst, work);
1945
1946 if (!hisi_sas_controller_reset(rst->hisi_hba))
1947 rst->done = true;
1948 complete(rst->completion);
1949}
1950EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
1951
John Garry0fa24c12017-06-14 23:33:18 +08001952int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
1953{
1954 struct device *dev = hisi_hba->dev;
1955 struct platform_device *pdev = hisi_hba->platform_dev;
1956 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
1957 struct clk *refclk;
1958
1959 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1960 SAS_ADDR_SIZE)) {
1961 dev_err(dev, "could not get property sas-addr\n");
1962 return -ENOENT;
1963 }
1964
1965 if (np) {
1966 /*
1967 * These properties are only required for platform device-based
1968 * controller with DT firmware.
1969 */
1970 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1971 "hisilicon,sas-syscon");
1972 if (IS_ERR(hisi_hba->ctrl)) {
1973 dev_err(dev, "could not get syscon\n");
1974 return -ENOENT;
1975 }
1976
1977 if (device_property_read_u32(dev, "ctrl-reset-reg",
1978 &hisi_hba->ctrl_reset_reg)) {
1979 dev_err(dev,
1980 "could not get property ctrl-reset-reg\n");
1981 return -ENOENT;
1982 }
1983
1984 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1985 &hisi_hba->ctrl_reset_sts_reg)) {
1986 dev_err(dev,
1987 "could not get property ctrl-reset-sts-reg\n");
1988 return -ENOENT;
1989 }
1990
1991 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1992 &hisi_hba->ctrl_clock_ena_reg)) {
1993 dev_err(dev,
1994 "could not get property ctrl-clock-ena-reg\n");
1995 return -ENOENT;
1996 }
1997 }
1998
1999 refclk = devm_clk_get(dev, NULL);
2000 if (IS_ERR(refclk))
2001 dev_dbg(dev, "no ref clk property\n");
2002 else
2003 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2004
2005 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2006 dev_err(dev, "could not get property phy-count\n");
2007 return -ENOENT;
2008 }
2009
2010 if (device_property_read_u32(dev, "queue-count",
2011 &hisi_hba->queue_count)) {
2012 dev_err(dev, "could not get property queue-count\n");
2013 return -ENOENT;
2014 }
2015
2016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2019
John Garry7eb78692015-11-18 00:50:31 +08002020static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2021 const struct hisi_sas_hw *hw)
2022{
John Garrye26b2f42015-11-18 00:50:32 +08002023 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08002024 struct Scsi_Host *shost;
2025 struct hisi_hba *hisi_hba;
2026 struct device *dev = &pdev->dev;
2027
John Garrye21fe3a2017-06-14 23:33:20 +08002028 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08002029 if (!shost) {
2030 dev_err(dev, "scsi host alloc failed\n");
2031 return NULL;
2032 }
John Garry7eb78692015-11-18 00:50:31 +08002033 hisi_hba = shost_priv(shost);
2034
Xiang Chen06ec0fb2017-03-23 01:25:18 +08002035 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08002036 hisi_hba->hw = hw;
John Garry11b75242017-06-14 23:33:17 +08002037 hisi_hba->dev = dev;
John Garry0fa24c12017-06-14 23:33:18 +08002038 hisi_hba->platform_dev = pdev;
John Garry7eb78692015-11-18 00:50:31 +08002039 hisi_hba->shost = shost;
2040 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2041
Kees Cook77570ee2017-08-22 16:05:14 -07002042 timer_setup(&hisi_hba->timer, NULL, 0);
John Garryfa42d802015-11-18 00:50:43 +08002043
John Garry0fa24c12017-06-14 23:33:18 +08002044 if (hisi_sas_get_fw_info(hisi_hba) < 0)
John Garrye26b2f42015-11-18 00:50:32 +08002045 goto err_out;
2046
John Garrya6f2c7f2016-09-06 23:36:19 +08002047 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2048 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2049 dev_err(dev, "No usable DMA addressing method\n");
2050 goto err_out;
2051 }
2052
John Garrye26b2f42015-11-18 00:50:32 +08002053 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2054 hisi_hba->regs = devm_ioremap_resource(dev, res);
2055 if (IS_ERR(hisi_hba->regs))
2056 goto err_out;
2057
John Garry89d53322015-11-18 00:50:35 +08002058 if (hisi_sas_alloc(hisi_hba, shost)) {
2059 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08002060 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08002061 }
John Garry6be6de12015-11-18 00:50:34 +08002062
John Garry7eb78692015-11-18 00:50:31 +08002063 return shost;
2064err_out:
John Garry76aae5f2017-08-11 00:09:43 +08002065 scsi_host_put(shost);
John Garry7eb78692015-11-18 00:50:31 +08002066 dev_err(dev, "shost alloc failed\n");
2067 return NULL;
2068}
2069
John Garrye21fe3a2017-06-14 23:33:20 +08002070void hisi_sas_init_add(struct hisi_hba *hisi_hba)
John Garry5d742422015-11-18 00:50:38 +08002071{
2072 int i;
2073
2074 for (i = 0; i < hisi_hba->n_phy; i++)
2075 memcpy(&hisi_hba->phy[i].dev_sas_addr,
2076 hisi_hba->sas_addr,
2077 SAS_ADDR_SIZE);
2078}
John Garrye21fe3a2017-06-14 23:33:20 +08002079EXPORT_SYMBOL_GPL(hisi_sas_init_add);
John Garry5d742422015-11-18 00:50:38 +08002080
John Garry7eb78692015-11-18 00:50:31 +08002081int hisi_sas_probe(struct platform_device *pdev,
2082 const struct hisi_sas_hw *hw)
2083{
2084 struct Scsi_Host *shost;
2085 struct hisi_hba *hisi_hba;
2086 struct device *dev = &pdev->dev;
2087 struct asd_sas_phy **arr_phy;
2088 struct asd_sas_port **arr_port;
2089 struct sas_ha_struct *sha;
2090 int rc, phy_nr, port_nr, i;
2091
2092 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08002093 if (!shost)
2094 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08002095
2096 sha = SHOST_TO_SAS_HA(shost);
2097 hisi_hba = shost_priv(shost);
2098 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08002099
John Garry7eb78692015-11-18 00:50:31 +08002100 phy_nr = port_nr = hisi_hba->n_phy;
2101
2102 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2103 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08002104 if (!arr_phy || !arr_port) {
2105 rc = -ENOMEM;
2106 goto err_out_ha;
2107 }
John Garry7eb78692015-11-18 00:50:31 +08002108
2109 sha->sas_phy = arr_phy;
2110 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08002111 sha->lldd_ha = hisi_hba;
2112
2113 shost->transportt = hisi_sas_stt;
2114 shost->max_id = HISI_SAS_MAX_DEVICES;
2115 shost->max_lun = ~0;
2116 shost->max_channel = 1;
2117 shost->max_cmd_len = 16;
2118 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08002119 shost->can_queue = hisi_hba->hw->max_command_entries;
2120 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08002121
2122 sha->sas_ha_name = DRV_NAME;
John Garry11b75242017-06-14 23:33:17 +08002123 sha->dev = hisi_hba->dev;
John Garry7eb78692015-11-18 00:50:31 +08002124 sha->lldd_module = THIS_MODULE;
2125 sha->sas_addr = &hisi_hba->sas_addr[0];
2126 sha->num_phys = hisi_hba->n_phy;
2127 sha->core.shost = hisi_hba->shost;
2128
2129 for (i = 0; i < hisi_hba->n_phy; i++) {
2130 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2131 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2132 }
2133
John Garry5d742422015-11-18 00:50:38 +08002134 hisi_sas_init_add(hisi_hba);
2135
John Garry7eb78692015-11-18 00:50:31 +08002136 rc = scsi_add_host(shost, &pdev->dev);
2137 if (rc)
2138 goto err_out_ha;
2139
2140 rc = sas_register_ha(sha);
2141 if (rc)
2142 goto err_out_register_ha;
2143
Xiang Chen0757f042017-01-20 20:45:23 +08002144 rc = hisi_hba->hw->hw_init(hisi_hba);
2145 if (rc)
2146 goto err_out_register_ha;
2147
John Garry7eb78692015-11-18 00:50:31 +08002148 scsi_scan_host(shost);
2149
2150 return 0;
2151
2152err_out_register_ha:
2153 scsi_remove_host(shost);
2154err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08002155 hisi_sas_free(hisi_hba);
John Garry76aae5f2017-08-11 00:09:43 +08002156 scsi_host_put(shost);
John Garry7eb78692015-11-18 00:50:31 +08002157 return rc;
2158}
2159EXPORT_SYMBOL_GPL(hisi_sas_probe);
2160
John Garry89d53322015-11-18 00:50:35 +08002161int hisi_sas_remove(struct platform_device *pdev)
2162{
2163 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2164 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08002165 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08002166
John Garry89d53322015-11-18 00:50:35 +08002167 sas_unregister_ha(sha);
2168 sas_remove_host(sha->core.shost);
2169
2170 hisi_sas_free(hisi_hba);
John Garry76aae5f2017-08-11 00:09:43 +08002171 scsi_host_put(shost);
John Garry89d53322015-11-18 00:50:35 +08002172 return 0;
2173}
2174EXPORT_SYMBOL_GPL(hisi_sas_remove);
2175
John Garrye8899fa2015-11-18 00:50:30 +08002176static __init int hisi_sas_init(void)
2177{
John Garrye8899fa2015-11-18 00:50:30 +08002178 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2179 if (!hisi_sas_stt)
2180 return -ENOMEM;
2181
2182 return 0;
2183}
2184
2185static __exit void hisi_sas_exit(void)
2186{
2187 sas_release_transport(hisi_sas_stt);
2188}
2189
2190module_init(hisi_sas_init);
2191module_exit(hisi_sas_exit);
2192
John Garrye8899fa2015-11-18 00:50:30 +08002193MODULE_LICENSE("GPL");
2194MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2195MODULE_DESCRIPTION("HISILICON SAS controller driver");
2196MODULE_ALIAS("platform:" DRV_NAME);