]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/hisi_sas/hisi_sas_main.c
scsi: ufs: make ufshcd_config_pwr_mode of non-static func
[mirror_ubuntu-eoan-kernel.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);
057c3d1f
XT
25static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
26 void *funcdata);
cac9b2a2 27
468f4b8d 28u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
6c7bb8a1 29{
468f4b8d 30 switch (fis->command) {
6c7bb8a1
XC
31 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:
edafeef4 36 return HISI_SAS_SATA_PROTOCOL_FPDMA;
6c7bb8a1
XC
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:
edafeef4 48 return HISI_SAS_SATA_PROTOCOL_PIO;
6c7bb8a1
XC
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:
c3fe8a2b 66 case ATA_CMD_ZAC_MGMT_IN:
edafeef4 67 return HISI_SAS_SATA_PROTOCOL_DMA;
6c7bb8a1
XC
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:
c3fe8a2b 79 case ATA_CMD_ZAC_MGMT_OUT:
edafeef4 80 return HISI_SAS_SATA_PROTOCOL_NONDATA;
468f4b8d 81
3ff0f0b6
XT
82 case ATA_CMD_SET_MAX:
83 switch (fis->features) {
84 case ATA_SET_MAX_PASSWD:
85 case ATA_SET_MAX_LOCK:
86 return HISI_SAS_SATA_PROTOCOL_PIO;
468f4b8d 87
3ff0f0b6
XT
88 case ATA_SET_MAX_PASSWD_DMA:
89 case ATA_SET_MAX_UNLOCK_DMA:
90 return HISI_SAS_SATA_PROTOCOL_DMA;
91
92 default:
93 return HISI_SAS_SATA_PROTOCOL_NONDATA;
468f4b8d 94 }
3ff0f0b6
XT
95
96 default:
97 {
6c7bb8a1
XC
98 if (direction == DMA_NONE)
99 return HISI_SAS_SATA_PROTOCOL_NONDATA;
100 return HISI_SAS_SATA_PROTOCOL_PIO;
101 }
468f4b8d 102 }
6c7bb8a1
XC
103}
104EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
105
75904077
XC
106void hisi_sas_sata_done(struct sas_task *task,
107 struct hisi_sas_slot *slot)
108{
109 struct task_status_struct *ts = &task->task_status;
110 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
f557e32c
XT
111 struct hisi_sas_status_buffer *status_buf =
112 hisi_sas_status_buf_addr_mem(slot);
113 u8 *iu = &status_buf->iu[0];
114 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
75904077
XC
115
116 resp->frame_len = sizeof(struct dev_to_host_fis);
117 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
118
119 ts->buf_valid_size = sizeof(*resp);
120}
121EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
122
318913c6
XC
123int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
124{
125 struct ata_queued_cmd *qc = task->uldd_task;
126
127 if (qc) {
128 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
129 qc->tf.command == ATA_CMD_FPDMA_READ) {
130 *tag = qc->tag;
131 return 1;
132 }
133 }
134 return 0;
135}
136EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
137
c2c1d9de
XC
138/*
139 * This function assumes linkrate mask fits in 8 bits, which it
140 * does for all HW versions supported.
141 */
142u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
143{
144 u16 rate = 0;
145 int i;
146
147 max -= SAS_LINK_RATE_1_5_GBPS;
148 for (i = 0; i <= max; i++)
149 rate |= 1 << (i * 2);
150 return rate;
151}
152EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
153
42e7a693
JG
154static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
155{
156 return device->port->ha->lldd_ha;
157}
158
2e244f0f
JG
159struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
160{
161 return container_of(sas_port, struct hisi_sas_port, sas_port);
162}
163EXPORT_SYMBOL_GPL(to_hisi_sas_port);
164
a25d0d3d
XC
165void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
166{
167 int phy_no;
168
169 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
170 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
171}
172EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
173
257efd1f
JG
174static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
175{
176 void *bitmap = hisi_hba->slot_index_tags;
177
178 clear_bit(slot_idx, bitmap);
179}
180
42e7a693
JG
181static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
182{
183 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
184}
185
186static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
187{
188 void *bitmap = hisi_hba->slot_index_tags;
189
190 set_bit(slot_idx, bitmap);
191}
192
193static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
194{
195 unsigned int index;
196 void *bitmap = hisi_hba->slot_index_tags;
197
198 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
199 if (index >= hisi_hba->slot_index_count)
200 return -SAS_QUEUE_FULL;
201 hisi_sas_slot_index_set(hisi_hba, index);
202 *slot_idx = index;
203 return 0;
204}
205
257efd1f
JG
206static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
207{
208 int i;
209
210 for (i = 0; i < hisi_hba->slot_index_count; ++i)
211 hisi_sas_slot_index_clear(hisi_hba, i);
212}
27a3f229
JG
213
214void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
215 struct hisi_sas_slot *slot)
216{
27a3f229 217
d3c4dd4e 218 if (task) {
11b75249 219 struct device *dev = hisi_hba->dev;
27a3f229 220
6ba0fbc3
XT
221 if (!task->lldd_task)
222 return;
223
224 task->lldd_task = NULL;
225
d3c4dd4e
JG
226 if (!sas_protocol_ata(task->task_proto))
227 if (slot->n_elem)
dc1e4730
XC
228 dma_unmap_sg(dev, task->scatter,
229 task->num_scatter,
d3c4dd4e 230 task->data_dir);
d3c4dd4e 231 }
27a3f229 232
f557e32c
XT
233 if (slot->buf)
234 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
27a3f229 235
27a3f229 236 list_del_init(&slot->entry);
6ba0fbc3 237 slot->buf = NULL;
27a3f229
JG
238 slot->task = NULL;
239 slot->port = NULL;
240 hisi_sas_slot_index_free(hisi_hba, slot->idx);
d3c4dd4e 241
59ba49f9 242 /* slot memory is fully zeroed when it is reused */
27a3f229
JG
243}
244EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
245
66ee999b
JG
246static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
247 struct hisi_sas_slot *slot)
248{
249 return hisi_hba->hw->prep_smp(hisi_hba, slot);
250}
251
42e7a693
JG
252static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
253 struct hisi_sas_slot *slot, int is_tmf,
254 struct hisi_sas_tmf_task *tmf)
255{
256 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
257}
258
6f2ff1a1
JG
259static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
260 struct hisi_sas_slot *slot)
261{
262 return hisi_hba->hw->prep_stp(hisi_hba, slot);
263}
264
441c2740
JG
265static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
266 struct hisi_sas_slot *slot,
267 int device_id, int abort_flag, int tag_to_abort)
268{
269 return hisi_hba->hw->prep_abort(hisi_hba, slot,
270 device_id, abort_flag, tag_to_abort);
271}
272
cac9b2a2
JG
273/*
274 * This function will issue an abort TMF regardless of whether the
275 * task is in the sdev or not. Then it will do the task complete
276 * cleanup and callbacks.
277 */
278static void hisi_sas_slot_abort(struct work_struct *work)
279{
280 struct hisi_sas_slot *abort_slot =
281 container_of(work, struct hisi_sas_slot, abort_slot);
282 struct sas_task *task = abort_slot->task;
283 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
284 struct scsi_cmnd *cmnd = task->uldd_task;
285 struct hisi_sas_tmf_task tmf_task;
cac9b2a2 286 struct scsi_lun lun;
11b75249 287 struct device *dev = hisi_hba->dev;
cac9b2a2 288 int tag = abort_slot->idx;
da7b66e7 289 unsigned long flags;
cac9b2a2
JG
290
291 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
292 dev_err(dev, "cannot abort slot for non-ssp task\n");
293 goto out;
294 }
295
296 int_to_scsilun(cmnd->device->lun, &lun);
297 tmf_task.tmf = TMF_ABORT_TASK;
298 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
299
300 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
301out:
302 /* Do cleanup for this task */
da7b66e7 303 spin_lock_irqsave(&hisi_hba->lock, flags);
cac9b2a2 304 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
da7b66e7 305 spin_unlock_irqrestore(&hisi_hba->lock, flags);
cac9b2a2
JG
306 if (task->task_done)
307 task->task_done(task);
cac9b2a2
JG
308}
309
b1a49412
XC
310static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq
311 *dq, int is_tmf, struct hisi_sas_tmf_task *tmf,
312 int *pass)
42e7a693 313{
b1a49412 314 struct hisi_hba *hisi_hba = dq->hisi_hba;
42e7a693
JG
315 struct domain_device *device = task->dev;
316 struct hisi_sas_device *sas_dev = device->lldd_dev;
317 struct hisi_sas_port *port;
318 struct hisi_sas_slot *slot;
319 struct hisi_sas_cmd_hdr *cmd_hdr_base;
2e244f0f 320 struct asd_sas_port *sas_port = device->port;
11b75249 321 struct device *dev = hisi_hba->dev;
42e7a693 322 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
54c9dd2d 323 unsigned long flags;
42e7a693 324
2e244f0f 325 if (!sas_port) {
42e7a693
JG
326 struct task_status_struct *ts = &task->task_status;
327
328 ts->resp = SAS_TASK_UNDELIVERED;
329 ts->stat = SAS_PHY_DOWN;
330 /*
331 * libsas will use dev->port, should
332 * not call task_done for sata
333 */
334 if (device->dev_type != SAS_SATA_DEV)
335 task->task_done(task);
6bf6db51 336 return -ECOMM;
42e7a693
JG
337 }
338
339 if (DEV_IS_GONE(sas_dev)) {
340 if (sas_dev)
ad604832 341 dev_info(dev, "task prep: device %d not ready\n",
42e7a693
JG
342 sas_dev->device_id);
343 else
344 dev_info(dev, "task prep: device %016llx not ready\n",
345 SAS_ADDR(device->sas_addr));
346
6bf6db51 347 return -ECOMM;
42e7a693 348 }
2e244f0f
JG
349
350 port = to_hisi_sas_port(sas_port);
9859f24e 351 if (port && !port->port_attached) {
09fe9ecb 352 dev_info(dev, "task prep: %s port%d not attach device\n",
6073b771 353 (dev_is_sata(device)) ?
09fe9ecb
JG
354 "SATA/STP" : "SAS",
355 device->port->id);
356
6bf6db51 357 return -ECOMM;
42e7a693
JG
358 }
359
360 if (!sas_protocol_ata(task->task_proto)) {
361 if (task->num_scatter) {
362 n_elem = dma_map_sg(dev, task->scatter,
363 task->num_scatter, task->data_dir);
364 if (!n_elem) {
365 rc = -ENOMEM;
366 goto prep_out;
367 }
368 }
369 } else
370 n_elem = task->num_scatter;
371
b1a49412 372 spin_lock_irqsave(&hisi_hba->lock, flags);
685b6d6e
JG
373 if (hisi_hba->hw->slot_index_alloc)
374 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
375 device);
376 else
377 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
b1a49412
XC
378 if (rc) {
379 spin_unlock_irqrestore(&hisi_hba->lock, flags);
42e7a693 380 goto err_out;
b1a49412
XC
381 }
382 spin_unlock_irqrestore(&hisi_hba->lock, flags);
383
384 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
42e7a693
JG
385 if (rc)
386 goto err_out_tag;
387
b1a49412
XC
388 dlvry_queue = dq->id;
389 dlvry_queue_slot = dq->wr_point;
42e7a693
JG
390 slot = &hisi_hba->slot_info[slot_idx];
391 memset(slot, 0, sizeof(struct hisi_sas_slot));
392
393 slot->idx = slot_idx;
394 slot->n_elem = n_elem;
395 slot->dlvry_queue = dlvry_queue;
396 slot->dlvry_queue_slot = dlvry_queue_slot;
397 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
398 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
399 slot->task = task;
400 slot->port = port;
cd938e53
XC
401 if (is_tmf)
402 slot->is_internal = true;
42e7a693 403 task->lldd_task = slot;
cac9b2a2 404 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
42e7a693 405
f557e32c
XT
406 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
407 GFP_ATOMIC, &slot->buf_dma);
408 if (!slot->buf) {
9c9d18e7 409 rc = -ENOMEM;
42e7a693 410 goto err_out_slot_buf;
9c9d18e7 411 }
42e7a693 412 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
f557e32c
XT
413 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
414 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
42e7a693
JG
415
416 switch (task->task_proto) {
66ee999b
JG
417 case SAS_PROTOCOL_SMP:
418 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
419 break;
42e7a693
JG
420 case SAS_PROTOCOL_SSP:
421 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
422 break;
423 case SAS_PROTOCOL_SATA:
424 case SAS_PROTOCOL_STP:
425 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
6f2ff1a1
JG
426 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
427 break;
42e7a693
JG
428 default:
429 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
430 task->task_proto);
431 rc = -EINVAL;
432 break;
433 }
434
435 if (rc) {
436 dev_err(dev, "task prep: rc = 0x%x\n", rc);
f557e32c 437 goto err_out_buf;
42e7a693
JG
438 }
439
9feaf909 440 spin_lock_irqsave(&hisi_hba->lock, flags);
405314df 441 list_add_tail(&slot->entry, &sas_dev->list);
9feaf909 442 spin_unlock_irqrestore(&hisi_hba->lock, flags);
54c9dd2d 443 spin_lock_irqsave(&task->task_state_lock, flags);
42e7a693 444 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
54c9dd2d 445 spin_unlock_irqrestore(&task->task_state_lock, flags);
42e7a693 446
b1a49412 447 dq->slot_prep = slot;
42e7a693
JG
448 ++(*pass);
449
9c9d18e7 450 return 0;
42e7a693 451
f557e32c
XT
452err_out_buf:
453 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
454 slot->buf_dma);
42e7a693
JG
455err_out_slot_buf:
456 /* Nothing to be done */
457err_out_tag:
b1a49412 458 spin_lock_irqsave(&hisi_hba->lock, flags);
42e7a693 459 hisi_sas_slot_index_free(hisi_hba, slot_idx);
b1a49412 460 spin_unlock_irqrestore(&hisi_hba->lock, flags);
42e7a693
JG
461err_out:
462 dev_err(dev, "task prep: failed[%d]!\n", rc);
463 if (!sas_protocol_ata(task->task_proto))
464 if (n_elem)
dc1e4730
XC
465 dma_unmap_sg(dev, task->scatter,
466 task->num_scatter,
42e7a693
JG
467 task->data_dir);
468prep_out:
469 return rc;
470}
471
472static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
473 int is_tmf, struct hisi_sas_tmf_task *tmf)
474{
475 u32 rc;
476 u32 pass = 0;
477 unsigned long flags;
478 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
11b75249 479 struct device *dev = hisi_hba->dev;
b1a49412
XC
480 struct domain_device *device = task->dev;
481 struct hisi_sas_device *sas_dev = device->lldd_dev;
482 struct hisi_sas_dq *dq = sas_dev->dq;
42e7a693 483
917d3bda 484 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
06ec0fb9
XC
485 return -EINVAL;
486
42e7a693 487 /* protect task_prep and start_delivery sequence */
b1a49412
XC
488 spin_lock_irqsave(&dq->lock, flags);
489 rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass);
42e7a693
JG
490 if (rc)
491 dev_err(dev, "task exec: failed[%d]!\n", rc);
492
493 if (likely(pass))
b1a49412
XC
494 hisi_hba->hw->start_delivery(dq);
495 spin_unlock_irqrestore(&dq->lock, flags);
42e7a693
JG
496
497 return rc;
498}
257efd1f 499
66139921
JG
500static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
501{
502 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
503 struct asd_sas_phy *sas_phy = &phy->sas_phy;
504 struct sas_ha_struct *sas_ha;
505
506 if (!phy->phy_attached)
507 return;
508
509 sas_ha = &hisi_hba->sha;
510 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
511
512 if (sas_phy->phy) {
513 struct sas_phy *sphy = sas_phy->phy;
514
515 sphy->negotiated_linkrate = sas_phy->linkrate;
66139921 516 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
2ae75787
XC
517 sphy->maximum_linkrate_hw =
518 hisi_hba->hw->phy_get_max_linkrate();
519 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
520 sphy->minimum_linkrate = phy->minimum_linkrate;
521
522 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
523 sphy->maximum_linkrate = phy->maximum_linkrate;
66139921
JG
524 }
525
526 if (phy->phy_type & PORT_TYPE_SAS) {
527 struct sas_identify_frame *id;
528
529 id = (struct sas_identify_frame *)phy->frame_rcvd;
530 id->dev_type = phy->identify.device_type;
531 id->initiator_bits = SAS_PROTOCOL_ALL;
532 id->target_bits = phy->identify.target_port_protocols;
533 } else if (phy->phy_type & PORT_TYPE_SATA) {
534 /*Nothing*/
535 }
536
537 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
538 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
539}
540
abda97c2
JG
541static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
542{
543 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
544 struct hisi_sas_device *sas_dev = NULL;
302e0901 545 unsigned long flags;
abda97c2
JG
546 int i;
547
302e0901 548 spin_lock_irqsave(&hisi_hba->lock, flags);
abda97c2
JG
549 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
550 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
b1a49412
XC
551 int queue = i % hisi_hba->queue_count;
552 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
553
abda97c2
JG
554 hisi_hba->devices[i].device_id = i;
555 sas_dev = &hisi_hba->devices[i];
556 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
557 sas_dev->dev_type = device->dev_type;
558 sas_dev->hisi_hba = hisi_hba;
559 sas_dev->sas_device = device;
b1a49412 560 sas_dev->dq = dq;
405314df 561 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
abda97c2
JG
562 break;
563 }
564 }
302e0901 565 spin_unlock_irqrestore(&hisi_hba->lock, flags);
abda97c2
JG
566
567 return sas_dev;
568}
569
570static int hisi_sas_dev_found(struct domain_device *device)
571{
572 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
573 struct domain_device *parent_dev = device->parent;
574 struct hisi_sas_device *sas_dev;
11b75249 575 struct device *dev = hisi_hba->dev;
abda97c2 576
685b6d6e
JG
577 if (hisi_hba->hw->alloc_dev)
578 sas_dev = hisi_hba->hw->alloc_dev(device);
579 else
580 sas_dev = hisi_sas_alloc_dev(device);
abda97c2
JG
581 if (!sas_dev) {
582 dev_err(dev, "fail alloc dev: max support %d devices\n",
583 HISI_SAS_MAX_DEVICES);
584 return -EINVAL;
585 }
586
587 device->lldd_dev = sas_dev;
588 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
589
590 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
591 int phy_no;
592 u8 phy_num = parent_dev->ex_dev.num_phys;
593 struct ex_phy *phy;
594
595 for (phy_no = 0; phy_no < phy_num; phy_no++) {
596 phy = &parent_dev->ex_dev.ex_phy[phy_no];
597 if (SAS_ADDR(phy->attached_sas_addr) ==
c90a0bea 598 SAS_ADDR(device->sas_addr))
abda97c2 599 break;
abda97c2
JG
600 }
601
602 if (phy_no == phy_num) {
603 dev_info(dev, "dev found: no attached "
604 "dev:%016llx at ex:%016llx\n",
605 SAS_ADDR(device->sas_addr),
606 SAS_ADDR(parent_dev->sas_addr));
607 return -EINVAL;
608 }
609 }
610
f1c88211
XC
611 dev_info(dev, "dev[%d:%x] found\n",
612 sas_dev->device_id, sas_dev->dev_type);
613
abda97c2
JG
614 return 0;
615}
616
31eec8a6
JG
617static int hisi_sas_slave_configure(struct scsi_device *sdev)
618{
619 struct domain_device *dev = sdev_to_domain_dev(sdev);
620 int ret = sas_slave_configure(sdev);
621
622 if (ret)
623 return ret;
624 if (!dev_is_sata(dev))
625 sas_change_queue_depth(sdev, 64);
626
627 return 0;
628}
629
701f75ec
JG
630static void hisi_sas_scan_start(struct Scsi_Host *shost)
631{
632 struct hisi_hba *hisi_hba = shost_priv(shost);
701f75ec 633
396b8044 634 hisi_hba->hw->phys_init(hisi_hba);
701f75ec
JG
635}
636
637static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
638{
639 struct hisi_hba *hisi_hba = shost_priv(shost);
640 struct sas_ha_struct *sha = &hisi_hba->sha;
641
396b8044
JG
642 /* Wait for PHY up interrupt to occur */
643 if (time < HZ)
701f75ec
JG
644 return 0;
645
646 sas_drain_work(sha);
647 return 1;
648}
649
66139921
JG
650static void hisi_sas_phyup_work(struct work_struct *work)
651{
652 struct hisi_sas_phy *phy =
e537b62b 653 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
66139921
JG
654 struct hisi_hba *hisi_hba = phy->hisi_hba;
655 struct asd_sas_phy *sas_phy = &phy->sas_phy;
656 int phy_no = sas_phy->id;
657
658 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
659 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
660}
976867e6 661
057c3d1f
XT
662static void hisi_sas_linkreset_work(struct work_struct *work)
663{
664 struct hisi_sas_phy *phy =
665 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
666 struct asd_sas_phy *sas_phy = &phy->sas_phy;
667
668 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
669}
670
e537b62b
XT
671static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
672 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
057c3d1f 673 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
e537b62b
XT
674};
675
676bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
677 enum hisi_sas_phy_event event)
678{
679 struct hisi_hba *hisi_hba = phy->hisi_hba;
680
681 if (WARN_ON(event >= HISI_PHYES_NUM))
682 return false;
683
684 return queue_work(hisi_hba->wq, &phy->works[event]);
685}
686EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
687
976867e6
JG
688static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
689{
690 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
691 struct asd_sas_phy *sas_phy = &phy->sas_phy;
e537b62b 692 int i;
976867e6
JG
693
694 phy->hisi_hba = hisi_hba;
695 phy->port = NULL;
eba8c20c
XT
696 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
697 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
976867e6
JG
698 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
699 sas_phy->class = SAS;
700 sas_phy->iproto = SAS_PROTOCOL_ALL;
701 sas_phy->tproto = 0;
702 sas_phy->type = PHY_TYPE_PHYSICAL;
703 sas_phy->role = PHY_ROLE_INITIATOR;
704 sas_phy->oob_mode = OOB_NOT_CONNECTED;
705 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
706 sas_phy->id = phy_no;
707 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
708 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
709 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
710 sas_phy->lldd_phy = phy;
66139921 711
e537b62b
XT
712 for (i = 0; i < HISI_PHYES_NUM; i++)
713 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
976867e6
JG
714}
715
184a4635
JG
716static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
717{
718 struct sas_ha_struct *sas_ha = sas_phy->ha;
719 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
720 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
721 struct asd_sas_port *sas_port = sas_phy->port;
2e244f0f 722 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
184a4635
JG
723 unsigned long flags;
724
725 if (!sas_port)
726 return;
727
728 spin_lock_irqsave(&hisi_hba->lock, flags);
729 port->port_attached = 1;
730 port->id = phy->port_id;
731 phy->port = port;
732 sas_port->lldd_port = port;
733 spin_unlock_irqrestore(&hisi_hba->lock, flags);
734}
735
d3c4dd4e 736static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
405314df 737 struct hisi_sas_slot *slot)
184a4635 738{
d3c4dd4e
JG
739 if (task) {
740 unsigned long flags;
741 struct task_status_struct *ts;
184a4635 742
d3c4dd4e 743 ts = &task->task_status;
184a4635 744
d3c4dd4e
JG
745 ts->resp = SAS_TASK_COMPLETE;
746 ts->stat = SAS_ABORTED_TASK;
747 spin_lock_irqsave(&task->task_state_lock, flags);
748 task->task_state_flags &=
749 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
750 task->task_state_flags |= SAS_TASK_STATE_DONE;
751 spin_unlock_irqrestore(&task->task_state_lock, flags);
752 }
184a4635 753
405314df 754 hisi_sas_slot_task_free(hisi_hba, task, slot);
184a4635
JG
755}
756
405314df 757/* hisi_hba.lock should be locked */
184a4635
JG
758static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
759 struct domain_device *device)
760{
405314df
JG
761 struct hisi_sas_slot *slot, *slot2;
762 struct hisi_sas_device *sas_dev = device->lldd_dev;
184a4635 763
405314df
JG
764 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
765 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
184a4635
JG
766}
767
4d0951ee 768void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
06ec0fb9 769{
405314df
JG
770 struct hisi_sas_device *sas_dev;
771 struct domain_device *device;
06ec0fb9
XC
772 int i;
773
405314df
JG
774 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
775 sas_dev = &hisi_hba->devices[i];
776 device = sas_dev->sas_device;
06ec0fb9 777
405314df
JG
778 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
779 !device)
06ec0fb9 780 continue;
405314df
JG
781
782 hisi_sas_release_task(hisi_hba, device);
06ec0fb9
XC
783 }
784}
4d0951ee 785EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
06ec0fb9 786
d30ff263
XC
787static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
788 struct domain_device *device)
789{
790 if (hisi_hba->hw->dereg_device)
791 hisi_hba->hw->dereg_device(hisi_hba, device);
792}
793
abda97c2
JG
794static void hisi_sas_dev_gone(struct domain_device *device)
795{
796 struct hisi_sas_device *sas_dev = device->lldd_dev;
797 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
11b75249 798 struct device *dev = hisi_hba->dev;
abda97c2 799
f1c88211 800 dev_info(dev, "dev[%d:%x] is gone\n",
abda97c2
JG
801 sas_dev->device_id, sas_dev->dev_type);
802
f8e45ec2
XC
803 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
804 hisi_sas_internal_task_abort(hisi_hba, device,
40f2702b
JG
805 HISI_SAS_INT_ABT_DEV, 0);
806
f8e45ec2
XC
807 hisi_sas_dereg_device(hisi_hba, device);
808
809 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
810 device->lldd_dev = NULL;
f8e45ec2 811 }
d30ff263 812
0258141a
XT
813 if (hisi_hba->hw->free_device)
814 hisi_hba->hw->free_device(sas_dev);
abda97c2 815 sas_dev->dev_type = SAS_PHY_UNUSED;
abda97c2 816}
42e7a693
JG
817
818static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
819{
820 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
821}
822
e4189d53
JG
823static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
824 void *funcdata)
825{
826 struct sas_ha_struct *sas_ha = sas_phy->ha;
827 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
828 int phy_no = sas_phy->id;
829
830 switch (func) {
831 case PHY_FUNC_HARD_RESET:
832 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
833 break;
834
835 case PHY_FUNC_LINK_RESET:
b4c67a6c
JG
836 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
837 msleep(100);
1eb8eeac 838 hisi_hba->hw->phy_start(hisi_hba, phy_no);
e4189d53
JG
839 break;
840
841 case PHY_FUNC_DISABLE:
842 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
843 break;
844
845 case PHY_FUNC_SET_LINK_RATE:
2ae75787
XC
846 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
847 break;
c52108c6
XT
848 case PHY_FUNC_GET_EVENTS:
849 if (hisi_hba->hw->get_events) {
850 hisi_hba->hw->get_events(hisi_hba, phy_no);
851 break;
852 }
853 /* fallthru */
e4189d53
JG
854 case PHY_FUNC_RELEASE_SPINUP_HOLD:
855 default:
856 return -EOPNOTSUPP;
857 }
858 return 0;
859}
184a4635 860
0efff300
JG
861static void hisi_sas_task_done(struct sas_task *task)
862{
863 if (!del_timer(&task->slow_task->timer))
864 return;
865 complete(&task->slow_task->completion);
866}
867
77570eed 868static void hisi_sas_tmf_timedout(struct timer_list *t)
0efff300 869{
77570eed
KC
870 struct sas_task_slow *slow = from_timer(slow, t, timer);
871 struct sas_task *task = slow->task;
f64a6988
XC
872 unsigned long flags;
873
874 spin_lock_irqsave(&task->task_state_lock, flags);
875 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
876 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
877 spin_unlock_irqrestore(&task->task_state_lock, flags);
0efff300 878
0efff300
JG
879 complete(&task->slow_task->completion);
880}
881
882#define TASK_TIMEOUT 20
883#define TASK_RETRY 3
bb9abc4a 884#define INTERNAL_ABORT_TIMEOUT 6
0efff300
JG
885static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
886 void *parameter, u32 para_len,
887 struct hisi_sas_tmf_task *tmf)
888{
889 struct hisi_sas_device *sas_dev = device->lldd_dev;
890 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
11b75249 891 struct device *dev = hisi_hba->dev;
0efff300
JG
892 struct sas_task *task;
893 int res, retry;
894
895 for (retry = 0; retry < TASK_RETRY; retry++) {
896 task = sas_alloc_slow_task(GFP_KERNEL);
897 if (!task)
898 return -ENOMEM;
899
900 task->dev = device;
901 task->task_proto = device->tproto;
902
7c594f04
XC
903 if (dev_is_sata(device)) {
904 task->ata_task.device_control_reg_update = 1;
905 memcpy(&task->ata_task.fis, parameter, para_len);
906 } else {
907 memcpy(&task->ssp_task, parameter, para_len);
908 }
0efff300
JG
909 task->task_done = hisi_sas_task_done;
910
841b86f3 911 task->slow_task->timer.function = hisi_sas_tmf_timedout;
0efff300
JG
912 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
913 add_timer(&task->slow_task->timer);
914
915 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
916
917 if (res) {
918 del_timer(&task->slow_task->timer);
919 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
920 res);
921 goto ex_err;
922 }
923
924 wait_for_completion(&task->slow_task->completion);
925 res = TMF_RESP_FUNC_FAILED;
926 /* Even TMF timed out, return direct. */
927 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
928 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
d3c4dd4e
JG
929 struct hisi_sas_slot *slot = task->lldd_task;
930
f1c88211 931 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
d3c4dd4e
JG
932 if (slot)
933 slot->task = NULL;
934
0efff300 935 goto ex_err;
f1c88211
XC
936 } else
937 dev_err(dev, "abort tmf: TMF task timeout\n");
0efff300
JG
938 }
939
940 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1af1b808 941 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
0efff300
JG
942 res = TMF_RESP_FUNC_COMPLETE;
943 break;
944 }
945
4ffde482
JG
946 if (task->task_status.resp == SAS_TASK_COMPLETE &&
947 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
948 res = TMF_RESP_FUNC_SUCC;
949 break;
950 }
951
0efff300
JG
952 if (task->task_status.resp == SAS_TASK_COMPLETE &&
953 task->task_status.stat == SAS_DATA_UNDERRUN) {
954 /* no error, but return the number of bytes of
955 * underrun
956 */
957 dev_warn(dev, "abort tmf: task to dev %016llx "
958 "resp: 0x%x sts 0x%x underrun\n",
959 SAS_ADDR(device->sas_addr),
960 task->task_status.resp,
961 task->task_status.stat);
962 res = task->task_status.residual;
963 break;
964 }
965
966 if (task->task_status.resp == SAS_TASK_COMPLETE &&
967 task->task_status.stat == SAS_DATA_OVERRUN) {
968 dev_warn(dev, "abort tmf: blocked task error\n");
969 res = -EMSGSIZE;
970 break;
971 }
972
973 dev_warn(dev, "abort tmf: task to dev "
974 "%016llx resp: 0x%x status 0x%x\n",
975 SAS_ADDR(device->sas_addr), task->task_status.resp,
976 task->task_status.stat);
977 sas_free_task(task);
978 task = NULL;
979 }
980ex_err:
d2d7e7a0
XC
981 if (retry == TASK_RETRY)
982 dev_warn(dev, "abort tmf: executing internal task failed!\n");
0efff300
JG
983 sas_free_task(task);
984 return res;
985}
986
7c594f04
XC
987static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
988 bool reset, int pmp, u8 *fis)
989{
990 struct ata_taskfile tf;
991
992 ata_tf_init(dev, &tf);
993 if (reset)
994 tf.ctl |= ATA_SRST;
995 else
996 tf.ctl &= ~ATA_SRST;
997 tf.command = ATA_CMD_DEV_RESET;
998 ata_tf_to_fis(&tf, pmp, 0, fis);
999}
1000
1001static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1002{
1003 u8 fis[20] = {0};
1004 struct ata_port *ap = device->sata_dev.ap;
1005 struct ata_link *link;
1006 int rc = TMF_RESP_FUNC_FAILED;
1007 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
11b75249 1008 struct device *dev = hisi_hba->dev;
7c594f04
XC
1009 int s = sizeof(struct host_to_dev_fis);
1010 unsigned long flags;
1011
1012 ata_for_each_link(link, ap, EDGE) {
1013 int pmp = sata_srst_pmp(link);
1014
1015 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1016 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1017 if (rc != TMF_RESP_FUNC_COMPLETE)
1018 break;
1019 }
1020
1021 if (rc == TMF_RESP_FUNC_COMPLETE) {
1022 ata_for_each_link(link, ap, EDGE) {
1023 int pmp = sata_srst_pmp(link);
1024
1025 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1026 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1027 s, NULL);
1028 if (rc != TMF_RESP_FUNC_COMPLETE)
1029 dev_err(dev, "ata disk de-reset failed\n");
1030 }
1031 } else {
1032 dev_err(dev, "ata disk reset failed\n");
1033 }
1034
1035 if (rc == TMF_RESP_FUNC_COMPLETE) {
1036 spin_lock_irqsave(&hisi_hba->lock, flags);
1037 hisi_sas_release_task(hisi_hba, device);
1038 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1039 }
1040
1041 return rc;
1042}
1043
0efff300
JG
1044static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1045 u8 *lun, struct hisi_sas_tmf_task *tmf)
1046{
1047 struct sas_ssp_task ssp_task;
1048
1049 if (!(device->tproto & SAS_PROTOCOL_SSP))
1050 return TMF_RESP_FUNC_ESUPP;
1051
1052 memcpy(ssp_task.LUN, lun, 8);
1053
1054 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1055 sizeof(ssp_task), tmf);
1056}
1057
a669bdbf 1058static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
917d3bda 1059{
a669bdbf 1060 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
917d3bda
XT
1061 int i;
1062
1063 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
a669bdbf
XT
1064 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1065 struct domain_device *device = sas_dev->sas_device;
1066 struct asd_sas_port *sas_port;
1067 struct hisi_sas_port *port;
1068 struct hisi_sas_phy *phy = NULL;
1069 struct asd_sas_phy *sas_phy;
1070
917d3bda 1071 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
a669bdbf 1072 || !device || !device->port)
917d3bda
XT
1073 continue;
1074
a669bdbf
XT
1075 sas_port = device->port;
1076 port = to_hisi_sas_port(sas_port);
1077
1078 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1079 if (state & BIT(sas_phy->id)) {
1080 phy = sas_phy->lldd_phy;
1081 break;
1082 }
1083
1084 if (phy) {
1085 port->id = phy->port_id;
917d3bda 1086
a669bdbf
XT
1087 /* Update linkrate of directly attached device. */
1088 if (!device->parent)
1089 device->linkrate = phy->sas_phy.linkrate;
917d3bda 1090
a669bdbf
XT
1091 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1092 } else
1093 port->id = 0xff;
917d3bda
XT
1094 }
1095}
1096
1097static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1098 u32 state)
1099{
1100 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1101 struct asd_sas_port *_sas_port = NULL;
1102 int phy_no;
1103
1104 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1105 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1106 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1107 struct asd_sas_port *sas_port = sas_phy->port;
917d3bda
XT
1108 bool do_port_check = !!(_sas_port != sas_port);
1109
1110 if (!sas_phy->phy->enabled)
1111 continue;
1112
1113 /* Report PHY state change to libsas */
a669bdbf
XT
1114 if (state & BIT(phy_no)) {
1115 if (do_port_check && sas_port && sas_port->port_dev) {
917d3bda
XT
1116 struct domain_device *dev = sas_port->port_dev;
1117
1118 _sas_port = sas_port;
917d3bda
XT
1119
1120 if (DEV_IS_EXPANDER(dev->dev_type))
1121 sas_ha->notify_port_event(sas_phy,
1122 PORTE_BROADCAST_RCVD);
1123 }
1124 } else if (old_state & (1 << phy_no))
1125 /* PHY down but was up before */
1126 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1127
1128 }
917d3bda
XT
1129}
1130
06ec0fb9
XC
1131static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1132{
917d3bda
XT
1133 struct device *dev = hisi_hba->dev;
1134 struct Scsi_Host *shost = hisi_hba->shost;
1135 u32 old_state, state;
1136 unsigned long flags;
06ec0fb9
XC
1137 int rc;
1138
1139 if (!hisi_hba->hw->soft_reset)
1140 return -1;
1141
917d3bda
XT
1142 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1143 return -1;
06ec0fb9 1144
fb51e7a8 1145 dev_info(dev, "controller resetting...\n");
917d3bda 1146 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
06ec0fb9 1147
917d3bda 1148 scsi_block_requests(shost);
6f7c32d6
JG
1149 if (timer_pending(&hisi_hba->timer))
1150 del_timer_sync(&hisi_hba->timer);
1151
917d3bda
XT
1152 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1153 rc = hisi_hba->hw->soft_reset(hisi_hba);
1154 if (rc) {
1155 dev_warn(dev, "controller reset failed (%d)\n", rc);
1156 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
fb51e7a8 1157 scsi_unblock_requests(shost);
917d3bda
XT
1158 goto out;
1159 }
1160 spin_lock_irqsave(&hisi_hba->lock, flags);
1161 hisi_sas_release_tasks(hisi_hba);
1162 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1163
917d3bda
XT
1164 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1165
1166 /* Init and wait for PHYs to come up and all libsas event finished. */
1167 hisi_hba->hw->phys_init(hisi_hba);
1168 msleep(1000);
a669bdbf 1169 hisi_sas_refresh_port_id(hisi_hba);
fb51e7a8 1170 scsi_unblock_requests(shost);
917d3bda
XT
1171
1172 state = hisi_hba->hw->get_phys_state(hisi_hba);
1173 hisi_sas_rescan_topology(hisi_hba, old_state, state);
fb51e7a8 1174 dev_info(dev, "controller reset complete\n");
06ec0fb9
XC
1175
1176out:
06ec0fb9 1177 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
917d3bda 1178
06ec0fb9
XC
1179 return rc;
1180}
1181
0efff300
JG
1182static int hisi_sas_abort_task(struct sas_task *task)
1183{
1184 struct scsi_lun lun;
1185 struct hisi_sas_tmf_task tmf_task;
1186 struct domain_device *device = task->dev;
1187 struct hisi_sas_device *sas_dev = device->lldd_dev;
c6ef8954
XC
1188 struct hisi_hba *hisi_hba;
1189 struct device *dev;
0efff300
JG
1190 int rc = TMF_RESP_FUNC_FAILED;
1191 unsigned long flags;
1192
c6ef8954 1193 if (!sas_dev)
0efff300 1194 return TMF_RESP_FUNC_FAILED;
c6ef8954
XC
1195
1196 hisi_hba = dev_to_hisi_hba(task->dev);
1197 dev = hisi_hba->dev;
0efff300 1198
b81b6cce 1199 spin_lock_irqsave(&task->task_state_lock, flags);
0efff300 1200 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
b81b6cce 1201 spin_unlock_irqrestore(&task->task_state_lock, flags);
0efff300
JG
1202 rc = TMF_RESP_FUNC_COMPLETE;
1203 goto out;
1204 }
b81b6cce
XC
1205 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1206 spin_unlock_irqrestore(&task->task_state_lock, flags);
0efff300 1207
0efff300
JG
1208 sas_dev->dev_status = HISI_SAS_DEV_EH;
1209 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1210 struct scsi_cmnd *cmnd = task->uldd_task;
1211 struct hisi_sas_slot *slot = task->lldd_task;
1212 u32 tag = slot->idx;
c35279f2 1213 int rc2;
0efff300
JG
1214
1215 int_to_scsilun(cmnd->device->lun, &lun);
1216 tmf_task.tmf = TMF_ABORT_TASK;
1217 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1218
1219 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1220 &tmf_task);
1221
c35279f2
JG
1222 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1223 HISI_SAS_INT_ABT_CMD, tag);
813709f2
XT
1224 if (rc2 < 0) {
1225 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1226 return TMF_RESP_FUNC_FAILED;
1227 }
1228
c35279f2
JG
1229 /*
1230 * If the TMF finds that the IO is not in the device and also
1231 * the internal abort does not succeed, then it is safe to
1232 * free the slot.
1233 * Note: if the internal abort succeeds then the slot
1234 * will have already been completed
1235 */
1236 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
0efff300 1237 if (task->lldd_task) {
0efff300 1238 spin_lock_irqsave(&hisi_hba->lock, flags);
c35279f2 1239 hisi_sas_do_release_task(hisi_hba, task, slot);
0efff300
JG
1240 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1241 }
1242 }
0efff300
JG
1243 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1244 task->task_proto & SAS_PROTOCOL_STP) {
1245 if (task->dev->dev_type == SAS_SATA_DEV) {
813709f2
XT
1246 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1247 HISI_SAS_INT_ABT_DEV, 0);
1248 if (rc < 0) {
1249 dev_err(dev, "abort task: internal abort failed\n");
1250 goto out;
1251 }
d30ff263 1252 hisi_sas_dereg_device(hisi_hba, device);
7c594f04 1253 rc = hisi_sas_softreset_ata_disk(device);
0efff300 1254 }
eb045e04 1255 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
dc8a49ca
JG
1256 /* SMP */
1257 struct hisi_sas_slot *slot = task->lldd_task;
1258 u32 tag = slot->idx;
0efff300 1259
ccbfe5a0
XC
1260 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1261 HISI_SAS_INT_ABT_CMD, tag);
813709f2
XT
1262 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1263 task->lldd_task) {
ccbfe5a0
XC
1264 spin_lock_irqsave(&hisi_hba->lock, flags);
1265 hisi_sas_do_release_task(hisi_hba, task, slot);
1266 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1267 }
0efff300
JG
1268 }
1269
1270out:
1271 if (rc != TMF_RESP_FUNC_COMPLETE)
1272 dev_notice(dev, "abort task: rc=%d\n", rc);
1273 return rc;
1274}
1275
1276static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1277{
2a038131
XT
1278 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1279 struct device *dev = hisi_hba->dev;
0efff300
JG
1280 struct hisi_sas_tmf_task tmf_task;
1281 int rc = TMF_RESP_FUNC_FAILED;
2a038131
XT
1282 unsigned long flags;
1283
1284 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1285 HISI_SAS_INT_ABT_DEV, 0);
1286 if (rc < 0) {
1287 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1288 return TMF_RESP_FUNC_FAILED;
1289 }
1290 hisi_sas_dereg_device(hisi_hba, device);
0efff300
JG
1291
1292 tmf_task.tmf = TMF_ABORT_TASK_SET;
1293 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1294
2a038131
XT
1295 if (rc == TMF_RESP_FUNC_COMPLETE) {
1296 spin_lock_irqsave(&hisi_hba->lock, flags);
1297 hisi_sas_release_task(hisi_hba, device);
1298 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1299 }
1300
0efff300
JG
1301 return rc;
1302}
1303
1304static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1305{
1306 int rc = TMF_RESP_FUNC_FAILED;
1307 struct hisi_sas_tmf_task tmf_task;
1308
1309 tmf_task.tmf = TMF_CLEAR_ACA;
1310 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1311
1312 return rc;
1313}
1314
1315static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1316{
1317 struct sas_phy *phy = sas_get_local_phy(device);
1318 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1319 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1320 rc = sas_phy_reset(phy, reset_type);
1321 sas_put_local_phy(phy);
1322 msleep(2000);
1323 return rc;
1324}
1325
1326static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1327{
1328 struct hisi_sas_device *sas_dev = device->lldd_dev;
1329 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
813709f2 1330 struct device *dev = hisi_hba->dev;
0efff300 1331 int rc = TMF_RESP_FUNC_FAILED;
813709f2 1332 unsigned long flags;
0efff300
JG
1333
1334 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1335 return TMF_RESP_FUNC_FAILED;
1336 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1337
813709f2 1338 rc = hisi_sas_internal_task_abort(hisi_hba, device,
d30ff263 1339 HISI_SAS_INT_ABT_DEV, 0);
813709f2
XT
1340 if (rc < 0) {
1341 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1342 return TMF_RESP_FUNC_FAILED;
1343 }
d30ff263
XC
1344 hisi_sas_dereg_device(hisi_hba, device);
1345
0efff300
JG
1346 rc = hisi_sas_debug_I_T_nexus_reset(device);
1347
9960a24a 1348 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) {
6131243a
XC
1349 spin_lock_irqsave(&hisi_hba->lock, flags);
1350 hisi_sas_release_task(hisi_hba, device);
1351 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1352 }
1353 return rc;
0efff300
JG
1354}
1355
1356static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1357{
0efff300
JG
1358 struct hisi_sas_device *sas_dev = device->lldd_dev;
1359 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
11b75249 1360 struct device *dev = hisi_hba->dev;
0efff300
JG
1361 unsigned long flags;
1362 int rc = TMF_RESP_FUNC_FAILED;
1363
0efff300 1364 sas_dev->dev_status = HISI_SAS_DEV_EH;
055945df
JG
1365 if (dev_is_sata(device)) {
1366 struct sas_phy *phy;
1367
1368 /* Clear internal IO and then hardreset */
1369 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1370 HISI_SAS_INT_ABT_DEV, 0);
813709f2
XT
1371 if (rc < 0) {
1372 dev_err(dev, "lu_reset: internal abort failed\n");
055945df 1373 goto out;
813709f2 1374 }
d30ff263 1375 hisi_sas_dereg_device(hisi_hba, device);
0efff300 1376
055945df
JG
1377 phy = sas_get_local_phy(device);
1378
1379 rc = sas_phy_reset(phy, 1);
1380
1381 if (rc == 0) {
1382 spin_lock_irqsave(&hisi_hba->lock, flags);
1383 hisi_sas_release_task(hisi_hba, device);
1384 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1385 }
1386 sas_put_local_phy(phy);
1387 } else {
1388 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1389
2a038131
XT
1390 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1391 HISI_SAS_INT_ABT_DEV, 0);
1392 if (rc < 0) {
1393 dev_err(dev, "lu_reset: internal abort failed\n");
1394 goto out;
1395 }
1396 hisi_sas_dereg_device(hisi_hba, device);
1397
055945df
JG
1398 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1399 if (rc == TMF_RESP_FUNC_COMPLETE) {
1400 spin_lock_irqsave(&hisi_hba->lock, flags);
1401 hisi_sas_release_task(hisi_hba, device);
1402 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1403 }
1404 }
1405out:
14d3f397 1406 if (rc != TMF_RESP_FUNC_COMPLETE)
ad604832 1407 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
14d3f397 1408 sas_dev->device_id, rc);
0efff300
JG
1409 return rc;
1410}
1411
8b05ad6a
JG
1412static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1413{
1414 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
e402acdb 1415 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
8b05ad6a 1416
e402acdb
XT
1417 queue_work(hisi_hba->wq, &r.work);
1418 wait_for_completion(r.completion);
1419 if (r.done)
1420 return TMF_RESP_FUNC_COMPLETE;
1421
1422 return TMF_RESP_FUNC_FAILED;
8b05ad6a
JG
1423}
1424
0efff300
JG
1425static int hisi_sas_query_task(struct sas_task *task)
1426{
1427 struct scsi_lun lun;
1428 struct hisi_sas_tmf_task tmf_task;
1429 int rc = TMF_RESP_FUNC_FAILED;
1430
1431 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1432 struct scsi_cmnd *cmnd = task->uldd_task;
1433 struct domain_device *device = task->dev;
1434 struct hisi_sas_slot *slot = task->lldd_task;
1435 u32 tag = slot->idx;
1436
1437 int_to_scsilun(cmnd->device->lun, &lun);
1438 tmf_task.tmf = TMF_QUERY_TASK;
1439 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1440
1441 rc = hisi_sas_debug_issue_ssp_tmf(device,
1442 lun.scsi_lun,
1443 &tmf_task);
1444 switch (rc) {
1445 /* The task is still in Lun, release it then */
1446 case TMF_RESP_FUNC_SUCC:
1447 /* The task is not in Lun or failed, reset the phy */
1448 case TMF_RESP_FUNC_FAILED:
1449 case TMF_RESP_FUNC_COMPLETE:
1450 break;
997ee43c
XC
1451 default:
1452 rc = TMF_RESP_FUNC_FAILED;
1453 break;
0efff300
JG
1454 }
1455 }
1456 return rc;
1457}
1458
441c2740 1459static int
ad604832 1460hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
441c2740
JG
1461 struct sas_task *task, int abort_flag,
1462 int task_tag)
1463{
1464 struct domain_device *device = task->dev;
1465 struct hisi_sas_device *sas_dev = device->lldd_dev;
11b75249 1466 struct device *dev = hisi_hba->dev;
441c2740
JG
1467 struct hisi_sas_port *port;
1468 struct hisi_sas_slot *slot;
2e244f0f 1469 struct asd_sas_port *sas_port = device->port;
441c2740 1470 struct hisi_sas_cmd_hdr *cmd_hdr_base;
b1a49412 1471 struct hisi_sas_dq *dq = sas_dev->dq;
441c2740 1472 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
b1a49412 1473 unsigned long flags, flags_dq;
441c2740 1474
917d3bda 1475 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
06ec0fb9
XC
1476 return -EINVAL;
1477
441c2740
JG
1478 if (!device->port)
1479 return -1;
1480
2e244f0f 1481 port = to_hisi_sas_port(sas_port);
441c2740
JG
1482
1483 /* simply get a slot and send abort command */
b1a49412 1484 spin_lock_irqsave(&hisi_hba->lock, flags);
441c2740 1485 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
b1a49412
XC
1486 if (rc) {
1487 spin_unlock_irqrestore(&hisi_hba->lock, flags);
441c2740 1488 goto err_out;
b1a49412
XC
1489 }
1490 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1491
1492 spin_lock_irqsave(&dq->lock, flags_dq);
1493 rc = hisi_hba->hw->get_free_slot(hisi_hba, dq);
441c2740
JG
1494 if (rc)
1495 goto err_out_tag;
1496
b1a49412
XC
1497 dlvry_queue = dq->id;
1498 dlvry_queue_slot = dq->wr_point;
1499
441c2740
JG
1500 slot = &hisi_hba->slot_info[slot_idx];
1501 memset(slot, 0, sizeof(struct hisi_sas_slot));
1502
1503 slot->idx = slot_idx;
1504 slot->n_elem = n_elem;
1505 slot->dlvry_queue = dlvry_queue;
1506 slot->dlvry_queue_slot = dlvry_queue_slot;
1507 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1508 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1509 slot->task = task;
1510 slot->port = port;
cd938e53 1511 slot->is_internal = true;
441c2740
JG
1512 task->lldd_task = slot;
1513
031da09c
XC
1514 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1515 GFP_ATOMIC, &slot->buf_dma);
1516 if (!slot->buf) {
1517 rc = -ENOMEM;
1518 goto err_out_tag;
1519 }
1520
441c2740 1521 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
031da09c
XC
1522 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1523 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
441c2740
JG
1524
1525 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1526 abort_flag, task_tag);
1527 if (rc)
031da09c 1528 goto err_out_buf;
441c2740 1529
9feaf909 1530 spin_lock_irqsave(&hisi_hba->lock, flags);
405314df 1531 list_add_tail(&slot->entry, &sas_dev->list);
9feaf909 1532 spin_unlock_irqrestore(&hisi_hba->lock, flags);
54c9dd2d 1533 spin_lock_irqsave(&task->task_state_lock, flags);
441c2740 1534 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
54c9dd2d 1535 spin_unlock_irqrestore(&task->task_state_lock, flags);
441c2740 1536
b1a49412 1537 dq->slot_prep = slot;
441c2740 1538
b1a49412
XC
1539 /* send abort command to the chip */
1540 hisi_hba->hw->start_delivery(dq);
1541 spin_unlock_irqrestore(&dq->lock, flags_dq);
441c2740
JG
1542
1543 return 0;
1544
031da09c
XC
1545err_out_buf:
1546 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1547 slot->buf_dma);
441c2740 1548err_out_tag:
b1a49412 1549 spin_lock_irqsave(&hisi_hba->lock, flags);
441c2740 1550 hisi_sas_slot_index_free(hisi_hba, slot_idx);
b1a49412
XC
1551 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1552 spin_unlock_irqrestore(&dq->lock, flags_dq);
441c2740
JG
1553err_out:
1554 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1555
1556 return rc;
1557}
1558
1559/**
1560 * hisi_sas_internal_task_abort -- execute an internal
1561 * abort command for single IO command or a device
1562 * @hisi_hba: host controller struct
1563 * @device: domain device
1564 * @abort_flag: mode of operation, device or single IO
1565 * @tag: tag of IO to be aborted (only relevant to single
1566 * IO mode)
1567 */
1568static int
1569hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1570 struct domain_device *device,
1571 int abort_flag, int tag)
1572{
1573 struct sas_task *task;
1574 struct hisi_sas_device *sas_dev = device->lldd_dev;
11b75249 1575 struct device *dev = hisi_hba->dev;
441c2740 1576 int res;
441c2740 1577
813709f2
XT
1578 /*
1579 * The interface is not realized means this HW don't support internal
1580 * abort, or don't need to do internal abort. Then here, we return
1581 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1582 * the internal abort has been executed and returned CQ.
1583 */
441c2740 1584 if (!hisi_hba->hw->prep_abort)
813709f2 1585 return TMF_RESP_FUNC_FAILED;
441c2740
JG
1586
1587 task = sas_alloc_slow_task(GFP_KERNEL);
1588 if (!task)
1589 return -ENOMEM;
1590
1591 task->dev = device;
1592 task->task_proto = device->tproto;
1593 task->task_done = hisi_sas_task_done;
841b86f3 1594 task->slow_task->timer.function = hisi_sas_tmf_timedout;
bb9abc4a 1595 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
441c2740
JG
1596 add_timer(&task->slow_task->timer);
1597
441c2740
JG
1598 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1599 task, abort_flag, tag);
441c2740
JG
1600 if (res) {
1601 del_timer(&task->slow_task->timer);
1602 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1603 res);
1604 goto exit;
1605 }
1606 wait_for_completion(&task->slow_task->completion);
1607 res = TMF_RESP_FUNC_FAILED;
1608
f64a6988
XC
1609 /* Internal abort timed out */
1610 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1611 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1612 struct hisi_sas_slot *slot = task->lldd_task;
1613
1614 if (slot)
1615 slot->task = NULL;
f1c88211 1616 dev_err(dev, "internal task abort: timeout and not done.\n");
813709f2 1617 res = -EIO;
f692a677 1618 goto exit;
f1c88211
XC
1619 } else
1620 dev_err(dev, "internal task abort: timeout.\n");
f64a6988
XC
1621 }
1622
441c2740
JG
1623 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1624 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1625 res = TMF_RESP_FUNC_COMPLETE;
1626 goto exit;
1627 }
1628
c35279f2
JG
1629 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1630 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1631 res = TMF_RESP_FUNC_SUCC;
1632 goto exit;
1633 }
1634
441c2740 1635exit:
297d7302 1636 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
441c2740
JG
1637 "resp: 0x%x sts 0x%x\n",
1638 SAS_ADDR(device->sas_addr),
1639 task,
1640 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1641 task->task_status.stat);
1642 sas_free_task(task);
1643
1644 return res;
1645}
1646
184a4635
JG
1647static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1648{
1649 hisi_sas_port_notify_formed(sas_phy);
1650}
1651
336bd78b
XC
1652static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1653{
1654}
1655
6379c560
XT
1656static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1657 u8 reg_index, u8 reg_count, u8 *write_data)
1658{
1659 struct hisi_hba *hisi_hba = sha->lldd_ha;
1660
1661 if (!hisi_hba->hw->write_gpio)
1662 return -EOPNOTSUPP;
1663
1664 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1665 reg_index, reg_count, write_data);
1666}
1667
184a4635
JG
1668static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1669{
1670 phy->phy_attached = 0;
1671 phy->phy_type = 0;
1672 phy->port = NULL;
1673}
1674
1675void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1676{
1677 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1678 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1679 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1680
1681 if (rdy) {
1682 /* Phy down but ready */
1683 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1684 hisi_sas_port_notify_formed(sas_phy);
1685 } else {
1686 struct hisi_sas_port *port = phy->port;
1687
1688 /* Phy down and not ready */
1689 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1690 sas_phy_disconnected(sas_phy);
1691
1692 if (port) {
1693 if (phy->phy_type & PORT_TYPE_SAS) {
1694 int port_id = port->id;
1695
1696 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1697 port_id))
1698 port->port_attached = 0;
1699 } else if (phy->phy_type & PORT_TYPE_SATA)
1700 port->port_attached = 0;
1701 }
1702 hisi_sas_phy_disconnected(phy);
1703 }
1704}
1705EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1706
571295f8
XT
1707void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1708{
1709 int i;
1710
1711 for (i = 0; i < hisi_hba->queue_count; i++) {
1712 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1713
1714 tasklet_kill(&cq->tasklet);
1715 }
1716}
1717EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
06ec0fb9 1718
e21fe3a5
JG
1719struct scsi_transport_template *hisi_sas_stt;
1720EXPORT_SYMBOL_GPL(hisi_sas_stt);
e8899fad 1721
1e15feac 1722static struct device_attribute *host_attrs[] = {
8eea9dd8
JY
1723 &dev_attr_phy_event_threshold,
1724 NULL,
1725};
1726
e21fe3a5 1727static struct scsi_host_template _hisi_sas_sht = {
7eb7869f
JG
1728 .module = THIS_MODULE,
1729 .name = DRV_NAME,
1730 .queuecommand = sas_queuecommand,
1731 .target_alloc = sas_target_alloc,
31eec8a6 1732 .slave_configure = hisi_sas_slave_configure,
701f75ec
JG
1733 .scan_finished = hisi_sas_scan_finished,
1734 .scan_start = hisi_sas_scan_start,
7eb7869f
JG
1735 .change_queue_depth = sas_change_queue_depth,
1736 .bios_param = sas_bios_param,
1737 .can_queue = 1,
1738 .this_id = -1,
1739 .sg_tablesize = SG_ALL,
1740 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1741 .use_clustering = ENABLE_CLUSTERING,
1742 .eh_device_reset_handler = sas_eh_device_reset_handler,
cc199e78 1743 .eh_target_reset_handler = sas_eh_target_reset_handler,
7eb7869f
JG
1744 .target_destroy = sas_target_destroy,
1745 .ioctl = sas_ioctl,
8eea9dd8 1746 .shost_attrs = host_attrs,
7eb7869f 1747};
e21fe3a5
JG
1748struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1749EXPORT_SYMBOL_GPL(hisi_sas_sht);
7eb7869f 1750
e8899fad 1751static struct sas_domain_function_template hisi_sas_transport_ops = {
abda97c2
JG
1752 .lldd_dev_found = hisi_sas_dev_found,
1753 .lldd_dev_gone = hisi_sas_dev_gone,
42e7a693 1754 .lldd_execute_task = hisi_sas_queue_command,
e4189d53 1755 .lldd_control_phy = hisi_sas_control_phy,
0efff300
JG
1756 .lldd_abort_task = hisi_sas_abort_task,
1757 .lldd_abort_task_set = hisi_sas_abort_task_set,
1758 .lldd_clear_aca = hisi_sas_clear_aca,
1759 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1760 .lldd_lu_reset = hisi_sas_lu_reset,
1761 .lldd_query_task = hisi_sas_query_task,
8b05ad6a 1762 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
184a4635 1763 .lldd_port_formed = hisi_sas_port_formed,
336bd78b 1764 .lldd_port_deformed = hisi_sas_port_deformed,
6379c560 1765 .lldd_write_gpio = hisi_sas_write_gpio,
e8899fad
JG
1766};
1767
06ec0fb9
XC
1768void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1769{
1770 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1771
1772 for (i = 0; i < hisi_hba->queue_count; i++) {
1773 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1774 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1775
1776 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1777 memset(hisi_hba->cmd_hdr[i], 0, s);
1778 dq->wr_point = 0;
1779
1780 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1781 memset(hisi_hba->complete_hdr[i], 0, s);
1782 cq->rd_point = 0;
1783 }
1784
1785 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1786 memset(hisi_hba->initial_fis, 0, s);
1787
1788 s = max_command_entries * sizeof(struct hisi_sas_iost);
1789 memset(hisi_hba->iost, 0, s);
1790
1791 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1792 memset(hisi_hba->breakpoint, 0, s);
1793
3297ded1 1794 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
06ec0fb9
XC
1795 memset(hisi_hba->sata_breakpoint, 0, s);
1796}
1797EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1798
e21fe3a5 1799int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
6be6de18 1800{
11b75249 1801 struct device *dev = hisi_hba->dev;
a8d547bd 1802 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
6be6de18 1803
fa42d80d 1804 spin_lock_init(&hisi_hba->lock);
976867e6
JG
1805 for (i = 0; i < hisi_hba->n_phy; i++) {
1806 hisi_sas_phy_init(hisi_hba, i);
1807 hisi_hba->port[i].port_attached = 0;
1808 hisi_hba->port[i].id = -1;
976867e6
JG
1809 }
1810
af740dbe
JG
1811 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1812 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1813 hisi_hba->devices[i].device_id = i;
1814 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1815 }
1816
6be6de18 1817 for (i = 0; i < hisi_hba->queue_count; i++) {
9101a079 1818 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
4fde02ad 1819 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
9101a079
JG
1820
1821 /* Completion queue structure */
1822 cq->id = i;
1823 cq->hisi_hba = hisi_hba;
1824
4fde02ad 1825 /* Delivery queue structure */
39bade0c 1826 spin_lock_init(&dq->lock);
4fde02ad
JG
1827 dq->id = i;
1828 dq->hisi_hba = hisi_hba;
1829
6be6de18
JG
1830 /* Delivery queue */
1831 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1832 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1833 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1834 if (!hisi_hba->cmd_hdr[i])
1835 goto err_out;
6be6de18
JG
1836
1837 /* Completion queue */
1838 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1839 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1840 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1841 if (!hisi_hba->complete_hdr[i])
1842 goto err_out;
6be6de18
JG
1843 }
1844
f557e32c
XT
1845 s = sizeof(struct hisi_sas_slot_buf_table);
1846 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1847 if (!hisi_hba->buffer_pool)
6be6de18
JG
1848 goto err_out;
1849
1850 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
4f4e21b8 1851 hisi_hba->itct = dma_zalloc_coherent(dev, s, &hisi_hba->itct_dma,
6be6de18
JG
1852 GFP_KERNEL);
1853 if (!hisi_hba->itct)
1854 goto err_out;
1855
a8d547bd 1856 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
6be6de18
JG
1857 sizeof(struct hisi_sas_slot),
1858 GFP_KERNEL);
1859 if (!hisi_hba->slot_info)
1860 goto err_out;
1861
a8d547bd 1862 s = max_command_entries * sizeof(struct hisi_sas_iost);
6be6de18
JG
1863 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1864 GFP_KERNEL);
1865 if (!hisi_hba->iost)
1866 goto err_out;
1867
a8d547bd 1868 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
6be6de18
JG
1869 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1870 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1871 if (!hisi_hba->breakpoint)
1872 goto err_out;
1873
a8d547bd 1874 hisi_hba->slot_index_count = max_command_entries;
433f5696 1875 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
257efd1f
JG
1876 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1877 if (!hisi_hba->slot_index_tags)
1878 goto err_out;
1879
6be6de18
JG
1880 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1881 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1882 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1883 if (!hisi_hba->initial_fis)
1884 goto err_out;
6be6de18 1885
3297ded1 1886 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
6be6de18
JG
1887 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1888 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1889 if (!hisi_hba->sata_breakpoint)
1890 goto err_out;
06ec0fb9 1891 hisi_sas_init_mem(hisi_hba);
6be6de18 1892
257efd1f
JG
1893 hisi_sas_slot_index_init(hisi_hba);
1894
7e9080e1
JG
1895 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1896 if (!hisi_hba->wq) {
1897 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1898 goto err_out;
1899 }
1900
6be6de18
JG
1901 return 0;
1902err_out:
1903 return -ENOMEM;
1904}
e21fe3a5 1905EXPORT_SYMBOL_GPL(hisi_sas_alloc);
6be6de18 1906
e21fe3a5 1907void hisi_sas_free(struct hisi_hba *hisi_hba)
89d53322 1908{
11b75249 1909 struct device *dev = hisi_hba->dev;
a8d547bd 1910 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
89d53322
JG
1911
1912 for (i = 0; i < hisi_hba->queue_count; i++) {
1913 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1914 if (hisi_hba->cmd_hdr[i])
1915 dma_free_coherent(dev, s,
1916 hisi_hba->cmd_hdr[i],
1917 hisi_hba->cmd_hdr_dma[i]);
1918
1919 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1920 if (hisi_hba->complete_hdr[i])
1921 dma_free_coherent(dev, s,
1922 hisi_hba->complete_hdr[i],
1923 hisi_hba->complete_hdr_dma[i]);
1924 }
1925
f557e32c 1926 dma_pool_destroy(hisi_hba->buffer_pool);
89d53322
JG
1927
1928 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1929 if (hisi_hba->itct)
1930 dma_free_coherent(dev, s,
1931 hisi_hba->itct, hisi_hba->itct_dma);
1932
a8d547bd 1933 s = max_command_entries * sizeof(struct hisi_sas_iost);
89d53322
JG
1934 if (hisi_hba->iost)
1935 dma_free_coherent(dev, s,
1936 hisi_hba->iost, hisi_hba->iost_dma);
1937
a8d547bd 1938 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
89d53322
JG
1939 if (hisi_hba->breakpoint)
1940 dma_free_coherent(dev, s,
1941 hisi_hba->breakpoint,
1942 hisi_hba->breakpoint_dma);
1943
1944
1945 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1946 if (hisi_hba->initial_fis)
1947 dma_free_coherent(dev, s,
1948 hisi_hba->initial_fis,
1949 hisi_hba->initial_fis_dma);
1950
3297ded1 1951 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
89d53322
JG
1952 if (hisi_hba->sata_breakpoint)
1953 dma_free_coherent(dev, s,
1954 hisi_hba->sata_breakpoint,
1955 hisi_hba->sata_breakpoint_dma);
1956
7e9080e1
JG
1957 if (hisi_hba->wq)
1958 destroy_workqueue(hisi_hba->wq);
89d53322 1959}
e21fe3a5 1960EXPORT_SYMBOL_GPL(hisi_sas_free);
6be6de18 1961
b4241f0f 1962void hisi_sas_rst_work_handler(struct work_struct *work)
06ec0fb9
XC
1963{
1964 struct hisi_hba *hisi_hba =
1965 container_of(work, struct hisi_hba, rst_work);
1966
1967 hisi_sas_controller_reset(hisi_hba);
1968}
b4241f0f 1969EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
06ec0fb9 1970
e402acdb
XT
1971void hisi_sas_sync_rst_work_handler(struct work_struct *work)
1972{
1973 struct hisi_sas_rst *rst =
1974 container_of(work, struct hisi_sas_rst, work);
1975
1976 if (!hisi_sas_controller_reset(rst->hisi_hba))
1977 rst->done = true;
1978 complete(rst->completion);
1979}
1980EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
1981
0fa24c19 1982int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
7eb7869f 1983{
0fa24c19
JG
1984 struct device *dev = hisi_hba->dev;
1985 struct platform_device *pdev = hisi_hba->platform_dev;
1986 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
3bc45af8 1987 struct clk *refclk;
7eb7869f 1988
4d558c77 1989 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
0fa24c19
JG
1990 SAS_ADDR_SIZE)) {
1991 dev_err(dev, "could not get property sas-addr\n");
1992 return -ENOENT;
1993 }
e26b2f40 1994
4d558c77 1995 if (np) {
0fa24c19
JG
1996 /*
1997 * These properties are only required for platform device-based
1998 * controller with DT firmware.
1999 */
4d558c77
JG
2000 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2001 "hisilicon,sas-syscon");
0fa24c19
JG
2002 if (IS_ERR(hisi_hba->ctrl)) {
2003 dev_err(dev, "could not get syscon\n");
2004 return -ENOENT;
2005 }
e26b2f40 2006
4d558c77 2007 if (device_property_read_u32(dev, "ctrl-reset-reg",
0fa24c19
JG
2008 &hisi_hba->ctrl_reset_reg)) {
2009 dev_err(dev,
2010 "could not get property ctrl-reset-reg\n");
2011 return -ENOENT;
2012 }
e26b2f40 2013
4d558c77 2014 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
0fa24c19
JG
2015 &hisi_hba->ctrl_reset_sts_reg)) {
2016 dev_err(dev,
2017 "could not get property ctrl-reset-sts-reg\n");
2018 return -ENOENT;
2019 }
e26b2f40 2020
4d558c77 2021 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
0fa24c19
JG
2022 &hisi_hba->ctrl_clock_ena_reg)) {
2023 dev_err(dev,
2024 "could not get property ctrl-clock-ena-reg\n");
2025 return -ENOENT;
2026 }
4d558c77
JG
2027 }
2028
0fa24c19 2029 refclk = devm_clk_get(dev, NULL);
3bc45af8 2030 if (IS_ERR(refclk))
87e287c1 2031 dev_dbg(dev, "no ref clk property\n");
3bc45af8
JG
2032 else
2033 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2034
0fa24c19
JG
2035 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2036 dev_err(dev, "could not get property phy-count\n");
2037 return -ENOENT;
2038 }
e26b2f40 2039
4d558c77 2040 if (device_property_read_u32(dev, "queue-count",
0fa24c19
JG
2041 &hisi_hba->queue_count)) {
2042 dev_err(dev, "could not get property queue-count\n");
2043 return -ENOENT;
2044 }
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2049
2050static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2051 const struct hisi_sas_hw *hw)
2052{
2053 struct resource *res;
2054 struct Scsi_Host *shost;
2055 struct hisi_hba *hisi_hba;
2056 struct device *dev = &pdev->dev;
2057
e21fe3a5 2058 shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
0fa24c19
JG
2059 if (!shost) {
2060 dev_err(dev, "scsi host alloc failed\n");
2061 return NULL;
2062 }
2063 hisi_hba = shost_priv(shost);
2064
2065 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2066 hisi_hba->hw = hw;
2067 hisi_hba->dev = dev;
2068 hisi_hba->platform_dev = pdev;
2069 hisi_hba->shost = shost;
2070 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2071
77570eed 2072 timer_setup(&hisi_hba->timer, NULL, 0);
0fa24c19
JG
2073
2074 if (hisi_sas_get_fw_info(hisi_hba) < 0)
e26b2f40
JG
2075 goto err_out;
2076
a6f2c7ff
JG
2077 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2078 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2079 dev_err(dev, "No usable DMA addressing method\n");
2080 goto err_out;
2081 }
2082
e26b2f40
JG
2083 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2084 hisi_hba->regs = devm_ioremap_resource(dev, res);
2085 if (IS_ERR(hisi_hba->regs))
2086 goto err_out;
2087
6379c560
XT
2088 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2089 if (res) {
2090 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2091 if (IS_ERR(hisi_hba->sgpio_regs))
2092 goto err_out;
2093 }
2094
89d53322
JG
2095 if (hisi_sas_alloc(hisi_hba, shost)) {
2096 hisi_sas_free(hisi_hba);
6be6de18 2097 goto err_out;
89d53322 2098 }
6be6de18 2099
7eb7869f
JG
2100 return shost;
2101err_out:
76aae5f6 2102 scsi_host_put(shost);
7eb7869f
JG
2103 dev_err(dev, "shost alloc failed\n");
2104 return NULL;
2105}
2106
2107int hisi_sas_probe(struct platform_device *pdev,
2108 const struct hisi_sas_hw *hw)
2109{
2110 struct Scsi_Host *shost;
2111 struct hisi_hba *hisi_hba;
2112 struct device *dev = &pdev->dev;
2113 struct asd_sas_phy **arr_phy;
2114 struct asd_sas_port **arr_port;
2115 struct sas_ha_struct *sha;
2116 int rc, phy_nr, port_nr, i;
2117
2118 shost = hisi_sas_shost_alloc(pdev, hw);
d37a0082
XT
2119 if (!shost)
2120 return -ENOMEM;
7eb7869f
JG
2121
2122 sha = SHOST_TO_SAS_HA(shost);
2123 hisi_hba = shost_priv(shost);
2124 platform_set_drvdata(pdev, sha);
50cb916f 2125
7eb7869f
JG
2126 phy_nr = port_nr = hisi_hba->n_phy;
2127
2128 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2129 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
d37a0082
XT
2130 if (!arr_phy || !arr_port) {
2131 rc = -ENOMEM;
2132 goto err_out_ha;
2133 }
7eb7869f
JG
2134
2135 sha->sas_phy = arr_phy;
2136 sha->sas_port = arr_port;
7eb7869f
JG
2137 sha->lldd_ha = hisi_hba;
2138
2139 shost->transportt = hisi_sas_stt;
2140 shost->max_id = HISI_SAS_MAX_DEVICES;
2141 shost->max_lun = ~0;
2142 shost->max_channel = 1;
2143 shost->max_cmd_len = 16;
2144 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
a8d547bd
JG
2145 shost->can_queue = hisi_hba->hw->max_command_entries;
2146 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
7eb7869f
JG
2147
2148 sha->sas_ha_name = DRV_NAME;
11b75249 2149 sha->dev = hisi_hba->dev;
7eb7869f
JG
2150 sha->lldd_module = THIS_MODULE;
2151 sha->sas_addr = &hisi_hba->sas_addr[0];
2152 sha->num_phys = hisi_hba->n_phy;
2153 sha->core.shost = hisi_hba->shost;
2154
2155 for (i = 0; i < hisi_hba->n_phy; i++) {
2156 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2157 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2158 }
2159
2160 rc = scsi_add_host(shost, &pdev->dev);
2161 if (rc)
2162 goto err_out_ha;
2163
2164 rc = sas_register_ha(sha);
2165 if (rc)
2166 goto err_out_register_ha;
2167
0757f041
XC
2168 rc = hisi_hba->hw->hw_init(hisi_hba);
2169 if (rc)
2170 goto err_out_register_ha;
2171
7eb7869f
JG
2172 scsi_scan_host(shost);
2173
2174 return 0;
2175
2176err_out_register_ha:
2177 scsi_remove_host(shost);
2178err_out_ha:
d37a0082 2179 hisi_sas_free(hisi_hba);
76aae5f6 2180 scsi_host_put(shost);
7eb7869f
JG
2181 return rc;
2182}
2183EXPORT_SYMBOL_GPL(hisi_sas_probe);
2184
89d53322
JG
2185int hisi_sas_remove(struct platform_device *pdev)
2186{
2187 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2188 struct hisi_hba *hisi_hba = sha->lldd_ha;
d37a0082 2189 struct Scsi_Host *shost = sha->core.shost;
89d53322 2190
5df41af4
XC
2191 if (timer_pending(&hisi_hba->timer))
2192 del_timer(&hisi_hba->timer);
2193
89d53322
JG
2194 sas_unregister_ha(sha);
2195 sas_remove_host(sha->core.shost);
2196
2197 hisi_sas_free(hisi_hba);
76aae5f6 2198 scsi_host_put(shost);
89d53322
JG
2199 return 0;
2200}
2201EXPORT_SYMBOL_GPL(hisi_sas_remove);
2202
e8899fad
JG
2203static __init int hisi_sas_init(void)
2204{
e8899fad
JG
2205 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2206 if (!hisi_sas_stt)
2207 return -ENOMEM;
2208
2209 return 0;
2210}
2211
2212static __exit void hisi_sas_exit(void)
2213{
2214 sas_release_transport(hisi_sas_stt);
2215}
2216
2217module_init(hisi_sas_init);
2218module_exit(hisi_sas_exit);
2219
e8899fad
JG
2220MODULE_LICENSE("GPL");
2221MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2222MODULE_DESCRIPTION("HISILICON SAS controller driver");
2223MODULE_ALIAS("platform:" DRV_NAME);