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