scsi: hisi_sas: add RAS feature for v3 hw
[linux-2.6-block.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
CommitLineData
e8899fad
JG
1/*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12#include "hisi_sas.h"
13#define DRV_NAME "hisi_sas"
14
42e7a693
JG
15#define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
cac9b2a2
JG
18static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
441c2740
JG
20static int
21hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
7c594f04 24static int hisi_sas_softreset_ata_disk(struct domain_device *device);
cac9b2a2 25
6c7bb8a1
XC
26u8 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:
c3fe8a2b 64 case ATA_CMD_ZAC_MGMT_IN:
6c7bb8a1
XC
65 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:
c3fe8a2b 77 case ATA_CMD_ZAC_MGMT_OUT:
6c7bb8a1
XC
78 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
75904077
XC
87void 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;
f557e32c
XT
92 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;
75904077
XC
96
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
318913c6
XC
104int 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
42e7a693
JG
119static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
120{
121 return device->port->ha->lldd_ha;
122}
123
2e244f0f
JG
124struct 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
a25d0d3d
XC
130void 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
257efd1f
JG
139static 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
42e7a693
JG
146static 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
257efd1f
JG
171static 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}
27a3f229
JG
178
179void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
180 struct hisi_sas_slot *slot)
181{
27a3f229 182
d3c4dd4e 183 if (task) {
11b75249 184 struct device *dev = hisi_hba->dev;
d3c4dd4e
JG
185 struct domain_device *device = task->dev;
186 struct hisi_sas_device *sas_dev = device->lldd_dev;
27a3f229 187
6ba0fbc3
XT
188 if (!task->lldd_task)
189 return;
190
191 task->lldd_task = NULL;
192
d3c4dd4e
JG
193 if (!sas_protocol_ata(task->task_proto))
194 if (slot->n_elem)
dc1e4730
XC
195 dma_unmap_sg(dev, task->scatter,
196 task->num_scatter,
d3c4dd4e
JG
197 task->data_dir);
198
d3c4dd4e
JG
199 if (sas_dev)
200 atomic64_dec(&sas_dev->running_req);
201 }
27a3f229 202
f557e32c
XT
203 if (slot->buf)
204 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
27a3f229 205
27a3f229 206 list_del_init(&slot->entry);
6ba0fbc3 207 slot->buf = NULL;
27a3f229
JG
208 slot->task = NULL;
209 slot->port = NULL;
210 hisi_sas_slot_index_free(hisi_hba, slot->idx);
d3c4dd4e 211
59ba49f9 212 /* slot memory is fully zeroed when it is reused */
27a3f229
JG
213}
214EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
215
66ee999b
JG
216static 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
42e7a693
JG
222static 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
6f2ff1a1
JG
229static 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
441c2740
JG
235static 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
cac9b2a2
JG
243/*
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;
cac9b2a2 256 struct scsi_lun lun;
11b75249 257 struct device *dev = hisi_hba->dev;
cac9b2a2 258 int tag = abort_slot->idx;
da7b66e7 259 unsigned long flags;
cac9b2a2
JG
260
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 */
da7b66e7 273 spin_lock_irqsave(&hisi_hba->lock, flags);
cac9b2a2 274 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
da7b66e7 275 spin_unlock_irqrestore(&hisi_hba->lock, flags);
cac9b2a2
JG
276 if (task->task_done)
277 task->task_done(task);
cac9b2a2
JG
278}
279
b1a49412
XC
280static 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)
42e7a693 283{
b1a49412 284 struct hisi_hba *hisi_hba = dq->hisi_hba;
42e7a693
JG
285 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;
2e244f0f 290 struct asd_sas_port *sas_port = device->port;
11b75249 291 struct device *dev = hisi_hba->dev;
42e7a693 292 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
54c9dd2d 293 unsigned long flags;
42e7a693 294
2e244f0f 295 if (!sas_port) {
42e7a693
JG
296 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);
ddabca21 306 return SAS_PHY_DOWN;
42e7a693
JG
307 }
308
309 if (DEV_IS_GONE(sas_dev)) {
310 if (sas_dev)
ad604832 311 dev_info(dev, "task prep: device %d not ready\n",
42e7a693
JG
312 sas_dev->device_id);
313 else
314 dev_info(dev, "task prep: device %016llx not ready\n",
315 SAS_ADDR(device->sas_addr));
316
ddabca21 317 return SAS_PHY_DOWN;
42e7a693 318 }
2e244f0f
JG
319
320 port = to_hisi_sas_port(sas_port);
9859f24e 321 if (port && !port->port_attached) {
09fe9ecb 322 dev_info(dev, "task prep: %s port%d not attach device\n",
6073b771 323 (dev_is_sata(device)) ?
09fe9ecb
JG
324 "SATA/STP" : "SAS",
325 device->port->id);
326
327 return SAS_PHY_DOWN;
42e7a693
JG
328 }
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
b1a49412 342 spin_lock_irqsave(&hisi_hba->lock, flags);
685b6d6e
JG
343 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);
b1a49412
XC
348 if (rc) {
349 spin_unlock_irqrestore(&hisi_hba->lock, flags);
42e7a693 350 goto err_out;
b1a49412
XC
351 }
352 spin_unlock_irqrestore(&hisi_hba->lock, flags);
353
354 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
42e7a693
JG
355 if (rc)
356 goto err_out_tag;
357
b1a49412
XC
358 dlvry_queue = dq->id;
359 dlvry_queue_slot = dq->wr_point;
42e7a693
JG
360 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;
cac9b2a2 372 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
42e7a693 373
f557e32c
XT
374 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
375 GFP_ATOMIC, &slot->buf_dma);
376 if (!slot->buf) {
9c9d18e7 377 rc = -ENOMEM;
42e7a693 378 goto err_out_slot_buf;
9c9d18e7 379 }
42e7a693 380 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
f557e32c
XT
381 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);
42e7a693
JG
383
384 switch (task->task_proto) {
66ee999b
JG
385 case SAS_PROTOCOL_SMP:
386 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
387 break;
42e7a693
JG
388 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:
6f2ff1a1
JG
394 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
395 break;
42e7a693
JG
396 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);
f557e32c 405 goto err_out_buf;
42e7a693
JG
406 }
407
9feaf909 408 spin_lock_irqsave(&hisi_hba->lock, flags);
405314df 409 list_add_tail(&slot->entry, &sas_dev->list);
9feaf909 410 spin_unlock_irqrestore(&hisi_hba->lock, flags);
54c9dd2d 411 spin_lock_irqsave(&task->task_state_lock, flags);
42e7a693 412 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
54c9dd2d 413 spin_unlock_irqrestore(&task->task_state_lock, flags);
42e7a693 414
b1a49412 415 dq->slot_prep = slot;
42e7a693 416
f696cc32 417 atomic64_inc(&sas_dev->running_req);
42e7a693
JG
418 ++(*pass);
419
9c9d18e7 420 return 0;
42e7a693 421
f557e32c
XT
422err_out_buf:
423 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
424 slot->buf_dma);
42e7a693
JG
425err_out_slot_buf:
426 /* Nothing to be done */
427err_out_tag:
b1a49412 428 spin_lock_irqsave(&hisi_hba->lock, flags);
42e7a693 429 hisi_sas_slot_index_free(hisi_hba, slot_idx);
b1a49412 430 spin_unlock_irqrestore(&hisi_hba->lock, flags);
42e7a693
JG
431err_out:
432 dev_err(dev, "task prep: failed[%d]!\n", rc);
433 if (!sas_protocol_ata(task->task_proto))
434 if (n_elem)
dc1e4730
XC
435 dma_unmap_sg(dev, task->scatter,
436 task->num_scatter,
42e7a693
JG
437 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);
11b75249 449 struct device *dev = hisi_hba->dev;
b1a49412
XC
450 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;
42e7a693 453
917d3bda 454 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
06ec0fb9
XC
455 return -EINVAL;
456
42e7a693 457 /* protect task_prep and start_delivery sequence */
b1a49412
XC
458 spin_lock_irqsave(&dq->lock, flags);
459 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
42e7a693
JG
460 if (rc)
461 dev_err(dev, "task exec: failed[%d]!\n", rc);
462
463 if (likely(pass))
b1a49412
XC
464 hisi_hba->hw->start_delivery(dq);
465 spin_unlock_irqrestore(&dq->lock, flags);
42e7a693
JG
466
467 return rc;
468}
257efd1f 469
66139921
JG
470static 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;
66139921 486 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
2ae75787
XC
487 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;
66139921
JG
494 }
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
abda97c2
JG
511static 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;
302e0901 515 unsigned long flags;
abda97c2
JG
516 int i;
517
302e0901 518 spin_lock_irqsave(&hisi_hba->lock, flags);
abda97c2
JG
519 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
520 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
b1a49412
XC
521 int queue = i % hisi_hba->queue_count;
522 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
523
abda97c2
JG
524 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;
b1a49412 530 sas_dev->dq = dq;
405314df 531 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
abda97c2
JG
532 break;
533 }
534 }
302e0901 535 spin_unlock_irqrestore(&hisi_hba->lock, flags);
abda97c2
JG
536
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;
11b75249 545 struct device *dev = hisi_hba->dev;
abda97c2 546
685b6d6e
JG
547 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);
abda97c2
JG
551 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
583 return 0;
584}
585
31eec8a6
JG
586static int hisi_sas_slave_configure(struct scsi_device *sdev)
587{
588 struct domain_device *dev = sdev_to_domain_dev(sdev);
589 int ret = sas_slave_configure(sdev);
590
591 if (ret)
592 return ret;
593 if (!dev_is_sata(dev))
594 sas_change_queue_depth(sdev, 64);
595
596 return 0;
597}
598
701f75ec
JG
599static void hisi_sas_scan_start(struct Scsi_Host *shost)
600{
601 struct hisi_hba *hisi_hba = shost_priv(shost);
701f75ec 602
396b8044 603 hisi_hba->hw->phys_init(hisi_hba);
701f75ec
JG
604}
605
606static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
607{
608 struct hisi_hba *hisi_hba = shost_priv(shost);
609 struct sas_ha_struct *sha = &hisi_hba->sha;
610
396b8044
JG
611 /* Wait for PHY up interrupt to occur */
612 if (time < HZ)
701f75ec
JG
613 return 0;
614
615 sas_drain_work(sha);
616 return 1;
617}
618
66139921
JG
619static void hisi_sas_phyup_work(struct work_struct *work)
620{
621 struct hisi_sas_phy *phy =
622 container_of(work, struct hisi_sas_phy, phyup_ws);
623 struct hisi_hba *hisi_hba = phy->hisi_hba;
624 struct asd_sas_phy *sas_phy = &phy->sas_phy;
625 int phy_no = sas_phy->id;
626
627 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
628 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
629}
976867e6
JG
630
631static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
632{
633 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
634 struct asd_sas_phy *sas_phy = &phy->sas_phy;
635
636 phy->hisi_hba = hisi_hba;
637 phy->port = NULL;
976867e6
JG
638 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
639 sas_phy->class = SAS;
640 sas_phy->iproto = SAS_PROTOCOL_ALL;
641 sas_phy->tproto = 0;
642 sas_phy->type = PHY_TYPE_PHYSICAL;
643 sas_phy->role = PHY_ROLE_INITIATOR;
644 sas_phy->oob_mode = OOB_NOT_CONNECTED;
645 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
646 sas_phy->id = phy_no;
647 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
648 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
649 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
650 sas_phy->lldd_phy = phy;
66139921
JG
651
652 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
976867e6
JG
653}
654
184a4635
JG
655static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
656{
657 struct sas_ha_struct *sas_ha = sas_phy->ha;
658 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
659 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
660 struct asd_sas_port *sas_port = sas_phy->port;
2e244f0f 661 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
184a4635
JG
662 unsigned long flags;
663
664 if (!sas_port)
665 return;
666
667 spin_lock_irqsave(&hisi_hba->lock, flags);
668 port->port_attached = 1;
669 port->id = phy->port_id;
670 phy->port = port;
671 sas_port->lldd_port = port;
672 spin_unlock_irqrestore(&hisi_hba->lock, flags);
673}
674
d3c4dd4e 675static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
405314df 676 struct hisi_sas_slot *slot)
184a4635 677{
d3c4dd4e
JG
678 if (task) {
679 unsigned long flags;
680 struct task_status_struct *ts;
184a4635 681
d3c4dd4e 682 ts = &task->task_status;
184a4635 683
d3c4dd4e
JG
684 ts->resp = SAS_TASK_COMPLETE;
685 ts->stat = SAS_ABORTED_TASK;
686 spin_lock_irqsave(&task->task_state_lock, flags);
687 task->task_state_flags &=
688 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
689 task->task_state_flags |= SAS_TASK_STATE_DONE;
690 spin_unlock_irqrestore(&task->task_state_lock, flags);
691 }
184a4635 692
405314df 693 hisi_sas_slot_task_free(hisi_hba, task, slot);
184a4635
JG
694}
695
405314df 696/* hisi_hba.lock should be locked */
184a4635
JG
697static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
698 struct domain_device *device)
699{
405314df
JG
700 struct hisi_sas_slot *slot, *slot2;
701 struct hisi_sas_device *sas_dev = device->lldd_dev;
184a4635 702
405314df
JG
703 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
704 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
184a4635
JG
705}
706
06ec0fb9
XC
707static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
708{
405314df
JG
709 struct hisi_sas_device *sas_dev;
710 struct domain_device *device;
06ec0fb9
XC
711 int i;
712
405314df
JG
713 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
714 sas_dev = &hisi_hba->devices[i];
715 device = sas_dev->sas_device;
06ec0fb9 716
405314df
JG
717 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
718 !device)
06ec0fb9 719 continue;
405314df
JG
720
721 hisi_sas_release_task(hisi_hba, device);
06ec0fb9
XC
722 }
723}
724
d30ff263
XC
725static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
726 struct domain_device *device)
727{
728 if (hisi_hba->hw->dereg_device)
729 hisi_hba->hw->dereg_device(hisi_hba, device);
730}
731
abda97c2
JG
732static void hisi_sas_dev_gone(struct domain_device *device)
733{
734 struct hisi_sas_device *sas_dev = device->lldd_dev;
735 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
11b75249 736 struct device *dev = hisi_hba->dev;
abda97c2 737
ad604832 738 dev_info(dev, "found dev[%d:%x] is gone\n",
abda97c2
JG
739 sas_dev->device_id, sas_dev->dev_type);
740
f8e45ec2
XC
741 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
742 hisi_sas_internal_task_abort(hisi_hba, device,
40f2702b
JG
743 HISI_SAS_INT_ABT_DEV, 0);
744
f8e45ec2
XC
745 hisi_sas_dereg_device(hisi_hba, device);
746
747 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
748 device->lldd_dev = NULL;
749 memset(sas_dev, 0, sizeof(*sas_dev));
750 }
d30ff263 751
0258141a
XT
752 if (hisi_hba->hw->free_device)
753 hisi_hba->hw->free_device(sas_dev);
abda97c2 754 sas_dev->dev_type = SAS_PHY_UNUSED;
abda97c2 755}
42e7a693
JG
756
757static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
758{
759 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
760}
761
e4189d53
JG
762static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
763 void *funcdata)
764{
765 struct sas_ha_struct *sas_ha = sas_phy->ha;
766 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
767 int phy_no = sas_phy->id;
768
769 switch (func) {
770 case PHY_FUNC_HARD_RESET:
771 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
772 break;
773
774 case PHY_FUNC_LINK_RESET:
b4c67a6c
JG
775 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
776 msleep(100);
1eb8eeac 777 hisi_hba->hw->phy_start(hisi_hba, phy_no);
e4189d53
JG
778 break;
779
780 case PHY_FUNC_DISABLE:
781 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
782 break;
783
784 case PHY_FUNC_SET_LINK_RATE:
2ae75787
XC
785 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
786 break;
c52108c6
XT
787 case PHY_FUNC_GET_EVENTS:
788 if (hisi_hba->hw->get_events) {
789 hisi_hba->hw->get_events(hisi_hba, phy_no);
790 break;
791 }
792 /* fallthru */
e4189d53
JG
793 case PHY_FUNC_RELEASE_SPINUP_HOLD:
794 default:
795 return -EOPNOTSUPP;
796 }
797 return 0;
798}
184a4635 799
0efff300
JG
800static void hisi_sas_task_done(struct sas_task *task)
801{
802 if (!del_timer(&task->slow_task->timer))
803 return;
804 complete(&task->slow_task->completion);
805}
806
77570eed 807static void hisi_sas_tmf_timedout(struct timer_list *t)
0efff300 808{
77570eed
KC
809 struct sas_task_slow *slow = from_timer(slow, t, timer);
810 struct sas_task *task = slow->task;
f64a6988
XC
811 unsigned long flags;
812
813 spin_lock_irqsave(&task->task_state_lock, flags);
814 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
815 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
816 spin_unlock_irqrestore(&task->task_state_lock, flags);
0efff300 817
0efff300
JG
818 complete(&task->slow_task->completion);
819}
820
821#define TASK_TIMEOUT 20
822#define TASK_RETRY 3
823static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
824 void *parameter, u32 para_len,
825 struct hisi_sas_tmf_task *tmf)
826{
827 struct hisi_sas_device *sas_dev = device->lldd_dev;
828 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
11b75249 829 struct device *dev = hisi_hba->dev;
0efff300
JG
830 struct sas_task *task;
831 int res, retry;
832
833 for (retry = 0; retry < TASK_RETRY; retry++) {
834 task = sas_alloc_slow_task(GFP_KERNEL);
835 if (!task)
836 return -ENOMEM;
837
838 task->dev = device;
839 task->task_proto = device->tproto;
840
7c594f04
XC
841 if (dev_is_sata(device)) {
842 task->ata_task.device_control_reg_update = 1;
843 memcpy(&task->ata_task.fis, parameter, para_len);
844 } else {
845 memcpy(&task->ssp_task, parameter, para_len);
846 }
0efff300
JG
847 task->task_done = hisi_sas_task_done;
848
841b86f3 849 task->slow_task->timer.function = hisi_sas_tmf_timedout;
0efff300
JG
850 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
851 add_timer(&task->slow_task->timer);
852
853 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
854
855 if (res) {
856 del_timer(&task->slow_task->timer);
857 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
858 res);
859 goto ex_err;
860 }
861
862 wait_for_completion(&task->slow_task->completion);
863 res = TMF_RESP_FUNC_FAILED;
864 /* Even TMF timed out, return direct. */
865 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
866 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
d3c4dd4e
JG
867 struct hisi_sas_slot *slot = task->lldd_task;
868
7c594f04 869 dev_err(dev, "abort tmf: TMF task timeout\n");
d3c4dd4e
JG
870 if (slot)
871 slot->task = NULL;
872
0efff300
JG
873 goto ex_err;
874 }
875 }
876
877 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1af1b808 878 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
0efff300
JG
879 res = TMF_RESP_FUNC_COMPLETE;
880 break;
881 }
882
4ffde482
JG
883 if (task->task_status.resp == SAS_TASK_COMPLETE &&
884 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
885 res = TMF_RESP_FUNC_SUCC;
886 break;
887 }
888
0efff300
JG
889 if (task->task_status.resp == SAS_TASK_COMPLETE &&
890 task->task_status.stat == SAS_DATA_UNDERRUN) {
891 /* no error, but return the number of bytes of
892 * underrun
893 */
894 dev_warn(dev, "abort tmf: task to dev %016llx "
895 "resp: 0x%x sts 0x%x underrun\n",
896 SAS_ADDR(device->sas_addr),
897 task->task_status.resp,
898 task->task_status.stat);
899 res = task->task_status.residual;
900 break;
901 }
902
903 if (task->task_status.resp == SAS_TASK_COMPLETE &&
904 task->task_status.stat == SAS_DATA_OVERRUN) {
905 dev_warn(dev, "abort tmf: blocked task error\n");
906 res = -EMSGSIZE;
907 break;
908 }
909
910 dev_warn(dev, "abort tmf: task to dev "
911 "%016llx resp: 0x%x status 0x%x\n",
912 SAS_ADDR(device->sas_addr), task->task_status.resp,
913 task->task_status.stat);
914 sas_free_task(task);
915 task = NULL;
916 }
917ex_err:
d2d7e7a0
XC
918 if (retry == TASK_RETRY)
919 dev_warn(dev, "abort tmf: executing internal task failed!\n");
0efff300
JG
920 sas_free_task(task);
921 return res;
922}
923
7c594f04
XC
924static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
925 bool reset, int pmp, u8 *fis)
926{
927 struct ata_taskfile tf;
928
929 ata_tf_init(dev, &tf);
930 if (reset)
931 tf.ctl |= ATA_SRST;
932 else
933 tf.ctl &= ~ATA_SRST;
934 tf.command = ATA_CMD_DEV_RESET;
935 ata_tf_to_fis(&tf, pmp, 0, fis);
936}
937
938static int hisi_sas_softreset_ata_disk(struct domain_device *device)
939{
940 u8 fis[20] = {0};
941 struct ata_port *ap = device->sata_dev.ap;
942 struct ata_link *link;
943 int rc = TMF_RESP_FUNC_FAILED;
944 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
11b75249 945 struct device *dev = hisi_hba->dev;
7c594f04
XC
946 int s = sizeof(struct host_to_dev_fis);
947 unsigned long flags;
948
949 ata_for_each_link(link, ap, EDGE) {
950 int pmp = sata_srst_pmp(link);
951
952 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
953 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
954 if (rc != TMF_RESP_FUNC_COMPLETE)
955 break;
956 }
957
958 if (rc == TMF_RESP_FUNC_COMPLETE) {
959 ata_for_each_link(link, ap, EDGE) {
960 int pmp = sata_srst_pmp(link);
961
962 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
963 rc = hisi_sas_exec_internal_tmf_task(device, fis,
964 s, NULL);
965 if (rc != TMF_RESP_FUNC_COMPLETE)
966 dev_err(dev, "ata disk de-reset failed\n");
967 }
968 } else {
969 dev_err(dev, "ata disk reset failed\n");
970 }
971
972 if (rc == TMF_RESP_FUNC_COMPLETE) {
973 spin_lock_irqsave(&hisi_hba->lock, flags);
974 hisi_sas_release_task(hisi_hba, device);
975 spin_unlock_irqrestore(&hisi_hba->lock, flags);
976 }
977
978 return rc;
979}
980
0efff300
JG
981static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
982 u8 *lun, struct hisi_sas_tmf_task *tmf)
983{
984 struct sas_ssp_task ssp_task;
985
986 if (!(device->tproto & SAS_PROTOCOL_SSP))
987 return TMF_RESP_FUNC_ESUPP;
988
989 memcpy(ssp_task.LUN, lun, 8);
990
991 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
992 sizeof(ssp_task), tmf);
993}
994
a669bdbf 995static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
917d3bda 996{
a669bdbf 997 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
917d3bda
XT
998 int i;
999
1000 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
a669bdbf
XT
1001 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1002 struct domain_device *device = sas_dev->sas_device;
1003 struct asd_sas_port *sas_port;
1004 struct hisi_sas_port *port;
1005 struct hisi_sas_phy *phy = NULL;
1006 struct asd_sas_phy *sas_phy;
1007
917d3bda 1008 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
a669bdbf 1009 || !device || !device->port)
917d3bda
XT
1010 continue;
1011
a669bdbf
XT
1012 sas_port = device->port;
1013 port = to_hisi_sas_port(sas_port);
1014
1015 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1016 if (state & BIT(sas_phy->id)) {
1017 phy = sas_phy->lldd_phy;
1018 break;
1019 }
1020
1021 if (phy) {
1022 port->id = phy->port_id;
917d3bda 1023
a669bdbf
XT
1024 /* Update linkrate of directly attached device. */
1025 if (!device->parent)
1026 device->linkrate = phy->sas_phy.linkrate;
917d3bda 1027
a669bdbf
XT
1028 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1029 } else
1030 port->id = 0xff;
917d3bda
XT
1031 }
1032}
1033
1034static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1035 u32 state)
1036{
1037 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1038 struct asd_sas_port *_sas_port = NULL;
1039 int phy_no;
1040
1041 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1042 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1043 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1044 struct asd_sas_port *sas_port = sas_phy->port;
917d3bda
XT
1045 bool do_port_check = !!(_sas_port != sas_port);
1046
1047 if (!sas_phy->phy->enabled)
1048 continue;
1049
1050 /* Report PHY state change to libsas */
a669bdbf
XT
1051 if (state & BIT(phy_no)) {
1052 if (do_port_check && sas_port && sas_port->port_dev) {
917d3bda
XT
1053 struct domain_device *dev = sas_port->port_dev;
1054
1055 _sas_port = sas_port;
917d3bda
XT
1056
1057 if (DEV_IS_EXPANDER(dev->dev_type))
1058 sas_ha->notify_port_event(sas_phy,
1059 PORTE_BROADCAST_RCVD);
1060 }
1061 } else if (old_state & (1 << phy_no))
1062 /* PHY down but was up before */
1063 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1064
1065 }
917d3bda
XT
1066}
1067
06ec0fb9
XC
1068static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1069{
917d3bda
XT
1070 struct device *dev = hisi_hba->dev;
1071 struct Scsi_Host *shost = hisi_hba->shost;
1072 u32 old_state, state;
1073 unsigned long flags;
06ec0fb9
XC
1074 int rc;
1075
1076 if (!hisi_hba->hw->soft_reset)
1077 return -1;
1078
917d3bda
XT
1079 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1080 return -1;
06ec0fb9 1081
fb51e7a8 1082 dev_info(dev, "controller resetting...\n");
917d3bda 1083 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
06ec0fb9 1084
917d3bda
XT
1085 scsi_block_requests(shost);
1086 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1087 rc = hisi_hba->hw->soft_reset(hisi_hba);
1088 if (rc) {
1089 dev_warn(dev, "controller reset failed (%d)\n", rc);
1090 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
fb51e7a8 1091 scsi_unblock_requests(shost);
917d3bda
XT
1092 goto out;
1093 }
1094 spin_lock_irqsave(&hisi_hba->lock, flags);
1095 hisi_sas_release_tasks(hisi_hba);
1096 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1097
917d3bda
XT
1098 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1099
1100 /* Init and wait for PHYs to come up and all libsas event finished. */
1101 hisi_hba->hw->phys_init(hisi_hba);
1102 msleep(1000);
a669bdbf 1103 hisi_sas_refresh_port_id(hisi_hba);
fb51e7a8 1104 scsi_unblock_requests(shost);
917d3bda
XT
1105
1106 state = hisi_hba->hw->get_phys_state(hisi_hba);
1107 hisi_sas_rescan_topology(hisi_hba, old_state, state);
fb51e7a8 1108 dev_info(dev, "controller reset complete\n");
06ec0fb9
XC
1109
1110out:
06ec0fb9 1111 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
917d3bda 1112
06ec0fb9
XC
1113 return rc;
1114}
1115
0efff300
JG
1116static int hisi_sas_abort_task(struct sas_task *task)
1117{
1118 struct scsi_lun lun;
1119 struct hisi_sas_tmf_task tmf_task;
1120 struct domain_device *device = task->dev;
1121 struct hisi_sas_device *sas_dev = device->lldd_dev;
1122 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
11b75249 1123 struct device *dev = hisi_hba->dev;
0efff300
JG
1124 int rc = TMF_RESP_FUNC_FAILED;
1125 unsigned long flags;
1126
1127 if (!sas_dev) {
1128 dev_warn(dev, "Device has been removed\n");
1129 return TMF_RESP_FUNC_FAILED;
1130 }
1131
0efff300 1132 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
0efff300
JG
1133 rc = TMF_RESP_FUNC_COMPLETE;
1134 goto out;
1135 }
1136
0efff300
JG
1137 sas_dev->dev_status = HISI_SAS_DEV_EH;
1138 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1139 struct scsi_cmnd *cmnd = task->uldd_task;
1140 struct hisi_sas_slot *slot = task->lldd_task;
1141 u32 tag = slot->idx;
c35279f2 1142 int rc2;
0efff300
JG
1143
1144 int_to_scsilun(cmnd->device->lun, &lun);
1145 tmf_task.tmf = TMF_ABORT_TASK;
1146 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1147
1148 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1149 &tmf_task);
1150
c35279f2
JG
1151 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1152 HISI_SAS_INT_ABT_CMD, tag);
1153 /*
1154 * If the TMF finds that the IO is not in the device and also
1155 * the internal abort does not succeed, then it is safe to
1156 * free the slot.
1157 * Note: if the internal abort succeeds then the slot
1158 * will have already been completed
1159 */
1160 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
0efff300 1161 if (task->lldd_task) {
0efff300 1162 spin_lock_irqsave(&hisi_hba->lock, flags);
c35279f2 1163 hisi_sas_do_release_task(hisi_hba, task, slot);
0efff300
JG
1164 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1165 }
1166 }
0efff300
JG
1167 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1168 task->task_proto & SAS_PROTOCOL_STP) {
1169 if (task->dev->dev_type == SAS_SATA_DEV) {
dc8a49ca
JG
1170 hisi_sas_internal_task_abort(hisi_hba, device,
1171 HISI_SAS_INT_ABT_DEV, 0);
d30ff263 1172 hisi_sas_dereg_device(hisi_hba, device);
7c594f04 1173 rc = hisi_sas_softreset_ata_disk(device);
0efff300 1174 }
eb045e04 1175 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
dc8a49ca
JG
1176 /* SMP */
1177 struct hisi_sas_slot *slot = task->lldd_task;
1178 u32 tag = slot->idx;
0efff300 1179
ccbfe5a0
XC
1180 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1181 HISI_SAS_INT_ABT_CMD, tag);
378c233b 1182 if (rc == TMF_RESP_FUNC_FAILED && task->lldd_task) {
ccbfe5a0
XC
1183 spin_lock_irqsave(&hisi_hba->lock, flags);
1184 hisi_sas_do_release_task(hisi_hba, task, slot);
1185 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1186 }
0efff300
JG
1187 }
1188
1189out:
1190 if (rc != TMF_RESP_FUNC_COMPLETE)
1191 dev_notice(dev, "abort task: rc=%d\n", rc);
1192 return rc;
1193}
1194
1195static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1196{
1197 struct hisi_sas_tmf_task tmf_task;
1198 int rc = TMF_RESP_FUNC_FAILED;
1199
1200 tmf_task.tmf = TMF_ABORT_TASK_SET;
1201 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1202
1203 return rc;
1204}
1205
1206static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1207{
1208 int rc = TMF_RESP_FUNC_FAILED;
1209 struct hisi_sas_tmf_task tmf_task;
1210
1211 tmf_task.tmf = TMF_CLEAR_ACA;
1212 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1213
1214 return rc;
1215}
1216
1217static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1218{
1219 struct sas_phy *phy = sas_get_local_phy(device);
1220 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1221 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1222 rc = sas_phy_reset(phy, reset_type);
1223 sas_put_local_phy(phy);
1224 msleep(2000);
1225 return rc;
1226}
1227
1228static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1229{
1230 struct hisi_sas_device *sas_dev = device->lldd_dev;
1231 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1232 unsigned long flags;
1233 int rc = TMF_RESP_FUNC_FAILED;
1234
1235 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1236 return TMF_RESP_FUNC_FAILED;
1237 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1238
d30ff263
XC
1239 hisi_sas_internal_task_abort(hisi_hba, device,
1240 HISI_SAS_INT_ABT_DEV, 0);
1241 hisi_sas_dereg_device(hisi_hba, device);
1242
0efff300
JG
1243 rc = hisi_sas_debug_I_T_nexus_reset(device);
1244
6131243a
XC
1245 if (rc == TMF_RESP_FUNC_COMPLETE) {
1246 spin_lock_irqsave(&hisi_hba->lock, flags);
1247 hisi_sas_release_task(hisi_hba, device);
1248 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1249 }
1250 return rc;
0efff300
JG
1251}
1252
1253static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1254{
0efff300
JG
1255 struct hisi_sas_device *sas_dev = device->lldd_dev;
1256 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
11b75249 1257 struct device *dev = hisi_hba->dev;
0efff300
JG
1258 unsigned long flags;
1259 int rc = TMF_RESP_FUNC_FAILED;
1260
0efff300 1261 sas_dev->dev_status = HISI_SAS_DEV_EH;
055945df
JG
1262 if (dev_is_sata(device)) {
1263 struct sas_phy *phy;
1264
1265 /* Clear internal IO and then hardreset */
1266 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1267 HISI_SAS_INT_ABT_DEV, 0);
1268 if (rc == TMF_RESP_FUNC_FAILED)
1269 goto out;
d30ff263 1270 hisi_sas_dereg_device(hisi_hba, device);
0efff300 1271
055945df
JG
1272 phy = sas_get_local_phy(device);
1273
1274 rc = sas_phy_reset(phy, 1);
1275
1276 if (rc == 0) {
1277 spin_lock_irqsave(&hisi_hba->lock, flags);
1278 hisi_sas_release_task(hisi_hba, device);
1279 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1280 }
1281 sas_put_local_phy(phy);
1282 } else {
1283 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1284
1285 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1286 if (rc == TMF_RESP_FUNC_COMPLETE) {
1287 spin_lock_irqsave(&hisi_hba->lock, flags);
1288 hisi_sas_release_task(hisi_hba, device);
1289 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1290 }
1291 }
1292out:
14d3f397 1293 if (rc != TMF_RESP_FUNC_COMPLETE)
ad604832 1294 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
14d3f397 1295 sas_dev->device_id, rc);
0efff300
JG
1296 return rc;
1297}
1298
8b05ad6a
JG
1299static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1300{
1301 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
e402acdb 1302 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
8b05ad6a 1303
e402acdb
XT
1304 queue_work(hisi_hba->wq, &r.work);
1305 wait_for_completion(r.completion);
1306 if (r.done)
1307 return TMF_RESP_FUNC_COMPLETE;
1308
1309 return TMF_RESP_FUNC_FAILED;
8b05ad6a
JG
1310}
1311
0efff300
JG
1312static int hisi_sas_query_task(struct sas_task *task)
1313{
1314 struct scsi_lun lun;
1315 struct hisi_sas_tmf_task tmf_task;
1316 int rc = TMF_RESP_FUNC_FAILED;
1317
1318 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1319 struct scsi_cmnd *cmnd = task->uldd_task;
1320 struct domain_device *device = task->dev;
1321 struct hisi_sas_slot *slot = task->lldd_task;
1322 u32 tag = slot->idx;
1323
1324 int_to_scsilun(cmnd->device->lun, &lun);
1325 tmf_task.tmf = TMF_QUERY_TASK;
1326 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1327
1328 rc = hisi_sas_debug_issue_ssp_tmf(device,
1329 lun.scsi_lun,
1330 &tmf_task);
1331 switch (rc) {
1332 /* The task is still in Lun, release it then */
1333 case TMF_RESP_FUNC_SUCC:
1334 /* The task is not in Lun or failed, reset the phy */
1335 case TMF_RESP_FUNC_FAILED:
1336 case TMF_RESP_FUNC_COMPLETE:
1337 break;
997ee43c
XC
1338 default:
1339 rc = TMF_RESP_FUNC_FAILED;
1340 break;
0efff300
JG
1341 }
1342 }
1343 return rc;
1344}
1345
441c2740 1346static int
ad604832 1347hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
441c2740
JG
1348 struct sas_task *task, int abort_flag,
1349 int task_tag)
1350{
1351 struct domain_device *device = task->dev;
1352 struct hisi_sas_device *sas_dev = device->lldd_dev;
11b75249 1353 struct device *dev = hisi_hba->dev;
441c2740
JG
1354 struct hisi_sas_port *port;
1355 struct hisi_sas_slot *slot;
2e244f0f 1356 struct asd_sas_port *sas_port = device->port;
441c2740 1357 struct hisi_sas_cmd_hdr *cmd_hdr_base;
b1a49412 1358 struct hisi_sas_dq *dq = sas_dev->dq;
441c2740 1359 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
b1a49412 1360 unsigned long flags, flags_dq;
441c2740 1361
917d3bda 1362 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
06ec0fb9
XC
1363 return -EINVAL;
1364
441c2740
JG
1365 if (!device->port)
1366 return -1;
1367
2e244f0f 1368 port = to_hisi_sas_port(sas_port);
441c2740
JG
1369
1370 /* simply get a slot and send abort command */
b1a49412 1371 spin_lock_irqsave(&hisi_hba->lock, flags);
441c2740 1372 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
b1a49412
XC
1373 if (rc) {
1374 spin_unlock_irqrestore(&hisi_hba->lock, flags);
441c2740 1375 goto err_out;
b1a49412
XC
1376 }
1377 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1378
1379 spin_lock_irqsave(&dq->lock, flags_dq);
1380 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
441c2740
JG
1381 if (rc)
1382 goto err_out_tag;
1383
b1a49412
XC
1384 dlvry_queue = dq->id;
1385 dlvry_queue_slot = dq->wr_point;
1386
441c2740
JG
1387 slot = &hisi_hba->slot_info[slot_idx];
1388 memset(slot, 0, sizeof(struct hisi_sas_slot));
1389
1390 slot->idx = slot_idx;
1391 slot->n_elem = n_elem;
1392 slot->dlvry_queue = dlvry_queue;
1393 slot->dlvry_queue_slot = dlvry_queue_slot;
1394 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1395 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1396 slot->task = task;
1397 slot->port = port;
1398 task->lldd_task = slot;
1399
031da09c
XC
1400 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1401 GFP_ATOMIC, &slot->buf_dma);
1402 if (!slot->buf) {
1403 rc = -ENOMEM;
1404 goto err_out_tag;
1405 }
1406
441c2740 1407 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
031da09c
XC
1408 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1409 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
441c2740
JG
1410
1411 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1412 abort_flag, task_tag);
1413 if (rc)
031da09c 1414 goto err_out_buf;
441c2740 1415
9feaf909 1416 spin_lock_irqsave(&hisi_hba->lock, flags);
405314df 1417 list_add_tail(&slot->entry, &sas_dev->list);
9feaf909 1418 spin_unlock_irqrestore(&hisi_hba->lock, flags);
54c9dd2d 1419 spin_lock_irqsave(&task->task_state_lock, flags);
441c2740 1420 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
54c9dd2d 1421 spin_unlock_irqrestore(&task->task_state_lock, flags);
441c2740 1422
b1a49412 1423 dq->slot_prep = slot;
441c2740 1424
f696cc32
JG
1425 atomic64_inc(&sas_dev->running_req);
1426
b1a49412
XC
1427 /* send abort command to the chip */
1428 hisi_hba->hw->start_delivery(dq);
1429 spin_unlock_irqrestore(&dq->lock, flags_dq);
441c2740
JG
1430
1431 return 0;
1432
031da09c
XC
1433err_out_buf:
1434 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1435 slot->buf_dma);
441c2740 1436err_out_tag:
b1a49412 1437 spin_lock_irqsave(&hisi_hba->lock, flags);
441c2740 1438 hisi_sas_slot_index_free(hisi_hba, slot_idx);
b1a49412
XC
1439 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1440 spin_unlock_irqrestore(&dq->lock, flags_dq);
441c2740
JG
1441err_out:
1442 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1443
1444 return rc;
1445}
1446
1447/**
1448 * hisi_sas_internal_task_abort -- execute an internal
1449 * abort command for single IO command or a device
1450 * @hisi_hba: host controller struct
1451 * @device: domain device
1452 * @abort_flag: mode of operation, device or single IO
1453 * @tag: tag of IO to be aborted (only relevant to single
1454 * IO mode)
1455 */
1456static int
1457hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1458 struct domain_device *device,
1459 int abort_flag, int tag)
1460{
1461 struct sas_task *task;
1462 struct hisi_sas_device *sas_dev = device->lldd_dev;
11b75249 1463 struct device *dev = hisi_hba->dev;
441c2740 1464 int res;
441c2740
JG
1465
1466 if (!hisi_hba->hw->prep_abort)
1467 return -EOPNOTSUPP;
1468
1469 task = sas_alloc_slow_task(GFP_KERNEL);
1470 if (!task)
1471 return -ENOMEM;
1472
1473 task->dev = device;
1474 task->task_proto = device->tproto;
1475 task->task_done = hisi_sas_task_done;
841b86f3 1476 task->slow_task->timer.function = hisi_sas_tmf_timedout;
0844a3ff 1477 task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
441c2740
JG
1478 add_timer(&task->slow_task->timer);
1479
441c2740
JG
1480 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1481 task, abort_flag, tag);
441c2740
JG
1482 if (res) {
1483 del_timer(&task->slow_task->timer);
1484 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1485 res);
1486 goto exit;
1487 }
1488 wait_for_completion(&task->slow_task->completion);
1489 res = TMF_RESP_FUNC_FAILED;
1490
f64a6988
XC
1491 /* Internal abort timed out */
1492 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1493 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1494 struct hisi_sas_slot *slot = task->lldd_task;
1495
1496 if (slot)
1497 slot->task = NULL;
1498 dev_err(dev, "internal task abort: timeout.\n");
f692a677 1499 goto exit;
f64a6988
XC
1500 }
1501 }
1502
441c2740
JG
1503 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1504 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1505 res = TMF_RESP_FUNC_COMPLETE;
1506 goto exit;
1507 }
1508
c35279f2
JG
1509 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1510 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1511 res = TMF_RESP_FUNC_SUCC;
1512 goto exit;
1513 }
1514
441c2740 1515exit:
297d7302 1516 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
441c2740
JG
1517 "resp: 0x%x sts 0x%x\n",
1518 SAS_ADDR(device->sas_addr),
1519 task,
1520 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1521 task->task_status.stat);
1522 sas_free_task(task);
1523
1524 return res;
1525}
1526
184a4635
JG
1527static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1528{
1529 hisi_sas_port_notify_formed(sas_phy);
1530}
1531
184a4635
JG
1532static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1533{
1534 phy->phy_attached = 0;
1535 phy->phy_type = 0;
1536 phy->port = NULL;
1537}
1538
1539void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1540{
1541 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1542 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1543 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1544
1545 if (rdy) {
1546 /* Phy down but ready */
1547 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1548 hisi_sas_port_notify_formed(sas_phy);
1549 } else {
1550 struct hisi_sas_port *port = phy->port;
1551
1552 /* Phy down and not ready */
1553 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1554 sas_phy_disconnected(sas_phy);
1555
1556 if (port) {
1557 if (phy->phy_type & PORT_TYPE_SAS) {
1558 int port_id = port->id;
1559
1560 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1561 port_id))
1562 port->port_attached = 0;
1563 } else if (phy->phy_type & PORT_TYPE_SATA)
1564 port->port_attached = 0;
1565 }
1566 hisi_sas_phy_disconnected(phy);
1567 }
1568}
1569EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1570
571295f8
XT
1571void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1572{
1573 int i;
1574
1575 for (i = 0; i < hisi_hba->queue_count; i++) {
1576 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1577
1578 tasklet_kill(&cq->tasklet);
1579 }
1580}
1581EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
06ec0fb9 1582
e21fe3a5
JG
1583struct scsi_transport_template *hisi_sas_stt;
1584EXPORT_SYMBOL_GPL(hisi_sas_stt);
e8899fad 1585
e21fe3a5 1586static struct scsi_host_template _hisi_sas_sht = {
7eb7869f
JG
1587 .module = THIS_MODULE,
1588 .name = DRV_NAME,
1589 .queuecommand = sas_queuecommand,
1590 .target_alloc = sas_target_alloc,
31eec8a6 1591 .slave_configure = hisi_sas_slave_configure,
701f75ec
JG
1592 .scan_finished = hisi_sas_scan_finished,
1593 .scan_start = hisi_sas_scan_start,
7eb7869f
JG
1594 .change_queue_depth = sas_change_queue_depth,
1595 .bios_param = sas_bios_param,
1596 .can_queue = 1,
1597 .this_id = -1,
1598 .sg_tablesize = SG_ALL,
1599 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1600 .use_clustering = ENABLE_CLUSTERING,
1601 .eh_device_reset_handler = sas_eh_device_reset_handler,
cc199e78 1602 .eh_target_reset_handler = sas_eh_target_reset_handler,
7eb7869f
JG
1603 .target_destroy = sas_target_destroy,
1604 .ioctl = sas_ioctl,
1605};
e21fe3a5
JG
1606struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1607EXPORT_SYMBOL_GPL(hisi_sas_sht);
7eb7869f 1608
e8899fad 1609static struct sas_domain_function_template hisi_sas_transport_ops = {
abda97c2
JG
1610 .lldd_dev_found = hisi_sas_dev_found,
1611 .lldd_dev_gone = hisi_sas_dev_gone,
42e7a693 1612 .lldd_execute_task = hisi_sas_queue_command,
e4189d53 1613 .lldd_control_phy = hisi_sas_control_phy,
0efff300
JG
1614 .lldd_abort_task = hisi_sas_abort_task,
1615 .lldd_abort_task_set = hisi_sas_abort_task_set,
1616 .lldd_clear_aca = hisi_sas_clear_aca,
1617 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1618 .lldd_lu_reset = hisi_sas_lu_reset,
1619 .lldd_query_task = hisi_sas_query_task,
8b05ad6a 1620 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
184a4635 1621 .lldd_port_formed = hisi_sas_port_formed,
e8899fad
JG
1622};
1623
06ec0fb9
XC
1624void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1625{
1626 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1627
1628 for (i = 0; i < hisi_hba->queue_count; i++) {
1629 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1630 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1631
1632 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1633 memset(hisi_hba->cmd_hdr[i], 0, s);
1634 dq->wr_point = 0;
1635
1636 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1637 memset(hisi_hba->complete_hdr[i], 0, s);
1638 cq->rd_point = 0;
1639 }
1640
1641 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1642 memset(hisi_hba->initial_fis, 0, s);
1643
1644 s = max_command_entries * sizeof(struct hisi_sas_iost);
1645 memset(hisi_hba->iost, 0, s);
1646
1647 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1648 memset(hisi_hba->breakpoint, 0, s);
1649
3297ded1 1650 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
06ec0fb9
XC
1651 memset(hisi_hba->sata_breakpoint, 0, s);
1652}
1653EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1654
e21fe3a5 1655int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
6be6de18 1656{
11b75249 1657 struct device *dev = hisi_hba->dev;
a8d547bd 1658 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
6be6de18 1659
fa42d80d 1660 spin_lock_init(&hisi_hba->lock);
976867e6
JG
1661 for (i = 0; i < hisi_hba->n_phy; i++) {
1662 hisi_sas_phy_init(hisi_hba, i);
1663 hisi_hba->port[i].port_attached = 0;
1664 hisi_hba->port[i].id = -1;
976867e6
JG
1665 }
1666
af740dbe
JG
1667 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1668 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1669 hisi_hba->devices[i].device_id = i;
1670 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1671 }
1672
6be6de18 1673 for (i = 0; i < hisi_hba->queue_count; i++) {
9101a079 1674 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
4fde02ad 1675 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
9101a079
JG
1676
1677 /* Completion queue structure */
1678 cq->id = i;
1679 cq->hisi_hba = hisi_hba;
1680
4fde02ad 1681 /* Delivery queue structure */
39bade0c 1682 spin_lock_init(&dq->lock);
4fde02ad
JG
1683 dq->id = i;
1684 dq->hisi_hba = hisi_hba;
1685
6be6de18
JG
1686 /* Delivery queue */
1687 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1688 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1689 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1690 if (!hisi_hba->cmd_hdr[i])
1691 goto err_out;
6be6de18
JG
1692
1693 /* Completion queue */
1694 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1695 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1696 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1697 if (!hisi_hba->complete_hdr[i])
1698 goto err_out;
6be6de18
JG
1699 }
1700
f557e32c
XT
1701 s = sizeof(struct hisi_sas_slot_buf_table);
1702 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1703 if (!hisi_hba->buffer_pool)
6be6de18
JG
1704 goto err_out;
1705
1706 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1707 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1708 GFP_KERNEL);
1709 if (!hisi_hba->itct)
1710 goto err_out;
1711
1712 memset(hisi_hba->itct, 0, s);
1713
a8d547bd 1714 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
6be6de18
JG
1715 sizeof(struct hisi_sas_slot),
1716 GFP_KERNEL);
1717 if (!hisi_hba->slot_info)
1718 goto err_out;
1719
a8d547bd 1720 s = max_command_entries * sizeof(struct hisi_sas_iost);
6be6de18
JG
1721 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1722 GFP_KERNEL);
1723 if (!hisi_hba->iost)
1724 goto err_out;
1725
a8d547bd 1726 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
6be6de18
JG
1727 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1728 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1729 if (!hisi_hba->breakpoint)
1730 goto err_out;
1731
a8d547bd 1732 hisi_hba->slot_index_count = max_command_entries;
433f5696 1733 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
257efd1f
JG
1734 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1735 if (!hisi_hba->slot_index_tags)
1736 goto err_out;
1737
6be6de18
JG
1738 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1739 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1740 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1741 if (!hisi_hba->initial_fis)
1742 goto err_out;
6be6de18 1743
3297ded1 1744 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
6be6de18
JG
1745 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1746 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1747 if (!hisi_hba->sata_breakpoint)
1748 goto err_out;
06ec0fb9 1749 hisi_sas_init_mem(hisi_hba);
6be6de18 1750
257efd1f
JG
1751 hisi_sas_slot_index_init(hisi_hba);
1752
7e9080e1
JG
1753 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1754 if (!hisi_hba->wq) {
1755 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1756 goto err_out;
1757 }
1758
6be6de18
JG
1759 return 0;
1760err_out:
1761 return -ENOMEM;
1762}
e21fe3a5 1763EXPORT_SYMBOL_GPL(hisi_sas_alloc);
6be6de18 1764
e21fe3a5 1765void hisi_sas_free(struct hisi_hba *hisi_hba)
89d53322 1766{
11b75249 1767 struct device *dev = hisi_hba->dev;
a8d547bd 1768 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
89d53322
JG
1769
1770 for (i = 0; i < hisi_hba->queue_count; i++) {
1771 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1772 if (hisi_hba->cmd_hdr[i])
1773 dma_free_coherent(dev, s,
1774 hisi_hba->cmd_hdr[i],
1775 hisi_hba->cmd_hdr_dma[i]);
1776
1777 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1778 if (hisi_hba->complete_hdr[i])
1779 dma_free_coherent(dev, s,
1780 hisi_hba->complete_hdr[i],
1781 hisi_hba->complete_hdr_dma[i]);
1782 }
1783
f557e32c 1784 dma_pool_destroy(hisi_hba->buffer_pool);
89d53322
JG
1785
1786 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1787 if (hisi_hba->itct)
1788 dma_free_coherent(dev, s,
1789 hisi_hba->itct, hisi_hba->itct_dma);
1790
a8d547bd 1791 s = max_command_entries * sizeof(struct hisi_sas_iost);
89d53322
JG
1792 if (hisi_hba->iost)
1793 dma_free_coherent(dev, s,
1794 hisi_hba->iost, hisi_hba->iost_dma);
1795
a8d547bd 1796 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
89d53322
JG
1797 if (hisi_hba->breakpoint)
1798 dma_free_coherent(dev, s,
1799 hisi_hba->breakpoint,
1800 hisi_hba->breakpoint_dma);
1801
1802
1803 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1804 if (hisi_hba->initial_fis)
1805 dma_free_coherent(dev, s,
1806 hisi_hba->initial_fis,
1807 hisi_hba->initial_fis_dma);
1808
3297ded1 1809 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
89d53322
JG
1810 if (hisi_hba->sata_breakpoint)
1811 dma_free_coherent(dev, s,
1812 hisi_hba->sata_breakpoint,
1813 hisi_hba->sata_breakpoint_dma);
1814
7e9080e1
JG
1815 if (hisi_hba->wq)
1816 destroy_workqueue(hisi_hba->wq);
89d53322 1817}
e21fe3a5 1818EXPORT_SYMBOL_GPL(hisi_sas_free);
6be6de18 1819
b4241f0f 1820void hisi_sas_rst_work_handler(struct work_struct *work)
06ec0fb9
XC
1821{
1822 struct hisi_hba *hisi_hba =
1823 container_of(work, struct hisi_hba, rst_work);
1824
1825 hisi_sas_controller_reset(hisi_hba);
1826}
b4241f0f 1827EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
06ec0fb9 1828
e402acdb
XT
1829void hisi_sas_sync_rst_work_handler(struct work_struct *work)
1830{
1831 struct hisi_sas_rst *rst =
1832 container_of(work, struct hisi_sas_rst, work);
1833
1834 if (!hisi_sas_controller_reset(rst->hisi_hba))
1835 rst->done = true;
1836 complete(rst->completion);
1837}
1838EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
1839
0fa24c19 1840int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
7eb7869f 1841{
0fa24c19
JG
1842 struct device *dev = hisi_hba->dev;
1843 struct platform_device *pdev = hisi_hba->platform_dev;
1844 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
3bc45af8 1845 struct clk *refclk;
7eb7869f 1846
4d558c77 1847 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
0fa24c19
JG
1848 SAS_ADDR_SIZE)) {
1849 dev_err(dev, "could not get property sas-addr\n");
1850 return -ENOENT;
1851 }
e26b2f40 1852
4d558c77 1853 if (np) {
0fa24c19
JG
1854 /*
1855 * These properties are only required for platform device-based
1856 * controller with DT firmware.
1857 */
4d558c77
JG
1858 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1859 "hisilicon,sas-syscon");
0fa24c19
JG
1860 if (IS_ERR(hisi_hba->ctrl)) {
1861 dev_err(dev, "could not get syscon\n");
1862 return -ENOENT;
1863 }
e26b2f40 1864
4d558c77 1865 if (device_property_read_u32(dev, "ctrl-reset-reg",
0fa24c19
JG
1866 &hisi_hba->ctrl_reset_reg)) {
1867 dev_err(dev,
1868 "could not get property ctrl-reset-reg\n");
1869 return -ENOENT;
1870 }
e26b2f40 1871
4d558c77 1872 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
0fa24c19
JG
1873 &hisi_hba->ctrl_reset_sts_reg)) {
1874 dev_err(dev,
1875 "could not get property ctrl-reset-sts-reg\n");
1876 return -ENOENT;
1877 }
e26b2f40 1878
4d558c77 1879 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
0fa24c19
JG
1880 &hisi_hba->ctrl_clock_ena_reg)) {
1881 dev_err(dev,
1882 "could not get property ctrl-clock-ena-reg\n");
1883 return -ENOENT;
1884 }
4d558c77
JG
1885 }
1886
0fa24c19 1887 refclk = devm_clk_get(dev, NULL);
3bc45af8 1888 if (IS_ERR(refclk))
87e287c1 1889 dev_dbg(dev, "no ref clk property\n");
3bc45af8
JG
1890 else
1891 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1892
0fa24c19
JG
1893 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
1894 dev_err(dev, "could not get property phy-count\n");
1895 return -ENOENT;
1896 }
e26b2f40 1897
4d558c77 1898 if (device_property_read_u32(dev, "queue-count",
0fa24c19
JG
1899 &hisi_hba->queue_count)) {
1900 dev_err(dev, "could not get property queue-count\n");
1901 return -ENOENT;
1902 }
1903
1904 return 0;
1905}
1906EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
1907
1908static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1909 const struct hisi_sas_hw *hw)
1910{
1911 struct resource *res;
1912 struct Scsi_Host *shost;
1913 struct hisi_hba *hisi_hba;
1914 struct device *dev = &pdev->dev;
1915
e21fe3a5 1916 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
0fa24c19
JG
1917 if (!shost) {
1918 dev_err(dev, "scsi host alloc failed\n");
1919 return NULL;
1920 }
1921 hisi_hba = shost_priv(shost);
1922
1923 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
1924 hisi_hba->hw = hw;
1925 hisi_hba->dev = dev;
1926 hisi_hba->platform_dev = pdev;
1927 hisi_hba->shost = shost;
1928 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1929
77570eed 1930 timer_setup(&hisi_hba->timer, NULL, 0);
0fa24c19
JG
1931
1932 if (hisi_sas_get_fw_info(hisi_hba) < 0)
e26b2f40
JG
1933 goto err_out;
1934
a6f2c7ff
JG
1935 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1936 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1937 dev_err(dev, "No usable DMA addressing method\n");
1938 goto err_out;
1939 }
1940
e26b2f40
JG
1941 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1942 hisi_hba->regs = devm_ioremap_resource(dev, res);
1943 if (IS_ERR(hisi_hba->regs))
1944 goto err_out;
1945
89d53322
JG
1946 if (hisi_sas_alloc(hisi_hba, shost)) {
1947 hisi_sas_free(hisi_hba);
6be6de18 1948 goto err_out;
89d53322 1949 }
6be6de18 1950
7eb7869f
JG
1951 return shost;
1952err_out:
76aae5f6 1953 scsi_host_put(shost);
7eb7869f
JG
1954 dev_err(dev, "shost alloc failed\n");
1955 return NULL;
1956}
1957
e21fe3a5 1958void hisi_sas_init_add(struct hisi_hba *hisi_hba)
5d74242e
JG
1959{
1960 int i;
1961
1962 for (i = 0; i < hisi_hba->n_phy; i++)
1963 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1964 hisi_hba->sas_addr,
1965 SAS_ADDR_SIZE);
1966}
e21fe3a5 1967EXPORT_SYMBOL_GPL(hisi_sas_init_add);
5d74242e 1968
7eb7869f
JG
1969int hisi_sas_probe(struct platform_device *pdev,
1970 const struct hisi_sas_hw *hw)
1971{
1972 struct Scsi_Host *shost;
1973 struct hisi_hba *hisi_hba;
1974 struct device *dev = &pdev->dev;
1975 struct asd_sas_phy **arr_phy;
1976 struct asd_sas_port **arr_port;
1977 struct sas_ha_struct *sha;
1978 int rc, phy_nr, port_nr, i;
1979
1980 shost = hisi_sas_shost_alloc(pdev, hw);
d37a0082
XT
1981 if (!shost)
1982 return -ENOMEM;
7eb7869f
JG
1983
1984 sha = SHOST_TO_SAS_HA(shost);
1985 hisi_hba = shost_priv(shost);
1986 platform_set_drvdata(pdev, sha);
50cb916f 1987
7eb7869f
JG
1988 phy_nr = port_nr = hisi_hba->n_phy;
1989
1990 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1991 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
d37a0082
XT
1992 if (!arr_phy || !arr_port) {
1993 rc = -ENOMEM;
1994 goto err_out_ha;
1995 }
7eb7869f
JG
1996
1997 sha->sas_phy = arr_phy;
1998 sha->sas_port = arr_port;
7eb7869f
JG
1999 sha->lldd_ha = hisi_hba;
2000
2001 shost->transportt = hisi_sas_stt;
2002 shost->max_id = HISI_SAS_MAX_DEVICES;
2003 shost->max_lun = ~0;
2004 shost->max_channel = 1;
2005 shost->max_cmd_len = 16;
2006 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
a8d547bd
JG
2007 shost->can_queue = hisi_hba->hw->max_command_entries;
2008 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
7eb7869f
JG
2009
2010 sha->sas_ha_name = DRV_NAME;
11b75249 2011 sha->dev = hisi_hba->dev;
7eb7869f
JG
2012 sha->lldd_module = THIS_MODULE;
2013 sha->sas_addr = &hisi_hba->sas_addr[0];
2014 sha->num_phys = hisi_hba->n_phy;
2015 sha->core.shost = hisi_hba->shost;
2016
2017 for (i = 0; i < hisi_hba->n_phy; i++) {
2018 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2019 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2020 }
2021
5d74242e
JG
2022 hisi_sas_init_add(hisi_hba);
2023
7eb7869f
JG
2024 rc = scsi_add_host(shost, &pdev->dev);
2025 if (rc)
2026 goto err_out_ha;
2027
2028 rc = sas_register_ha(sha);
2029 if (rc)
2030 goto err_out_register_ha;
2031
0757f041
XC
2032 rc = hisi_hba->hw->hw_init(hisi_hba);
2033 if (rc)
2034 goto err_out_register_ha;
2035
7eb7869f
JG
2036 scsi_scan_host(shost);
2037
2038 return 0;
2039
2040err_out_register_ha:
2041 scsi_remove_host(shost);
2042err_out_ha:
d37a0082 2043 hisi_sas_free(hisi_hba);
76aae5f6 2044 scsi_host_put(shost);
7eb7869f
JG
2045 return rc;
2046}
2047EXPORT_SYMBOL_GPL(hisi_sas_probe);
2048
89d53322
JG
2049int hisi_sas_remove(struct platform_device *pdev)
2050{
2051 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2052 struct hisi_hba *hisi_hba = sha->lldd_ha;
d37a0082 2053 struct Scsi_Host *shost = sha->core.shost;
89d53322 2054
89d53322
JG
2055 sas_unregister_ha(sha);
2056 sas_remove_host(sha->core.shost);
2057
2058 hisi_sas_free(hisi_hba);
76aae5f6 2059 scsi_host_put(shost);
89d53322
JG
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(hisi_sas_remove);
2063
e8899fad
JG
2064static __init int hisi_sas_init(void)
2065{
e8899fad
JG
2066 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2067 if (!hisi_sas_stt)
2068 return -ENOMEM;
2069
2070 return 0;
2071}
2072
2073static __exit void hisi_sas_exit(void)
2074{
2075 sas_release_transport(hisi_sas_stt);
2076}
2077
2078module_init(hisi_sas_init);
2079module_exit(hisi_sas_exit);
2080
e8899fad
JG
2081MODULE_LICENSE("GPL");
2082MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2083MODULE_DESCRIPTION("HISILICON SAS controller driver");
2084MODULE_ALIAS("platform:" DRV_NAME);