1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2015 Linaro Ltd.
4  * Copyright (c) 2015 Hisilicon Limited.
5  */
6 
7 #include "hisi_sas.h"
8 #define DRV_NAME "hisi_sas"
9 
10 #define DEV_IS_GONE(dev) \
11 	((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
12 
13 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
14 				u8 *lun, struct hisi_sas_tmf_task *tmf);
15 static int
16 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
17 			     struct domain_device *device,
18 			     int abort_flag, int tag);
19 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
20 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
21 				void *funcdata);
22 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
23 				  struct domain_device *device);
24 static void hisi_sas_dev_gone(struct domain_device *device);
25 
26 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
27 {
28 	switch (fis->command) {
29 	case ATA_CMD_FPDMA_WRITE:
30 	case ATA_CMD_FPDMA_READ:
31 	case ATA_CMD_FPDMA_RECV:
32 	case ATA_CMD_FPDMA_SEND:
33 	case ATA_CMD_NCQ_NON_DATA:
34 		return HISI_SAS_SATA_PROTOCOL_FPDMA;
35 
36 	case ATA_CMD_DOWNLOAD_MICRO:
37 	case ATA_CMD_ID_ATA:
38 	case ATA_CMD_PMP_READ:
39 	case ATA_CMD_READ_LOG_EXT:
40 	case ATA_CMD_PIO_READ:
41 	case ATA_CMD_PIO_READ_EXT:
42 	case ATA_CMD_PMP_WRITE:
43 	case ATA_CMD_WRITE_LOG_EXT:
44 	case ATA_CMD_PIO_WRITE:
45 	case ATA_CMD_PIO_WRITE_EXT:
46 		return HISI_SAS_SATA_PROTOCOL_PIO;
47 
48 	case ATA_CMD_DSM:
49 	case ATA_CMD_DOWNLOAD_MICRO_DMA:
50 	case ATA_CMD_PMP_READ_DMA:
51 	case ATA_CMD_PMP_WRITE_DMA:
52 	case ATA_CMD_READ:
53 	case ATA_CMD_READ_EXT:
54 	case ATA_CMD_READ_LOG_DMA_EXT:
55 	case ATA_CMD_READ_STREAM_DMA_EXT:
56 	case ATA_CMD_TRUSTED_RCV_DMA:
57 	case ATA_CMD_TRUSTED_SND_DMA:
58 	case ATA_CMD_WRITE:
59 	case ATA_CMD_WRITE_EXT:
60 	case ATA_CMD_WRITE_FUA_EXT:
61 	case ATA_CMD_WRITE_QUEUED:
62 	case ATA_CMD_WRITE_LOG_DMA_EXT:
63 	case ATA_CMD_WRITE_STREAM_DMA_EXT:
64 	case ATA_CMD_ZAC_MGMT_IN:
65 		return HISI_SAS_SATA_PROTOCOL_DMA;
66 
67 	case ATA_CMD_CHK_POWER:
68 	case ATA_CMD_DEV_RESET:
69 	case ATA_CMD_EDD:
70 	case ATA_CMD_FLUSH:
71 	case ATA_CMD_FLUSH_EXT:
72 	case ATA_CMD_VERIFY:
73 	case ATA_CMD_VERIFY_EXT:
74 	case ATA_CMD_SET_FEATURES:
75 	case ATA_CMD_STANDBY:
76 	case ATA_CMD_STANDBYNOW1:
77 	case ATA_CMD_ZAC_MGMT_OUT:
78 		return HISI_SAS_SATA_PROTOCOL_NONDATA;
79 
80 	case ATA_CMD_SET_MAX:
81 		switch (fis->features) {
82 		case ATA_SET_MAX_PASSWD:
83 		case ATA_SET_MAX_LOCK:
84 			return HISI_SAS_SATA_PROTOCOL_PIO;
85 
86 		case ATA_SET_MAX_PASSWD_DMA:
87 		case ATA_SET_MAX_UNLOCK_DMA:
88 			return HISI_SAS_SATA_PROTOCOL_DMA;
89 
90 		default:
91 			return HISI_SAS_SATA_PROTOCOL_NONDATA;
92 		}
93 
94 	default:
95 	{
96 		if (direction == DMA_NONE)
97 			return HISI_SAS_SATA_PROTOCOL_NONDATA;
98 		return HISI_SAS_SATA_PROTOCOL_PIO;
99 	}
100 	}
101 }
102 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
103 
104 void hisi_sas_sata_done(struct sas_task *task,
105 			    struct hisi_sas_slot *slot)
106 {
107 	struct task_status_struct *ts = &task->task_status;
108 	struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
109 	struct hisi_sas_status_buffer *status_buf =
110 			hisi_sas_status_buf_addr_mem(slot);
111 	u8 *iu = &status_buf->iu[0];
112 	struct dev_to_host_fis *d2h =  (struct dev_to_host_fis *)iu;
113 
114 	resp->frame_len = sizeof(struct dev_to_host_fis);
115 	memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
116 
117 	ts->buf_valid_size = sizeof(*resp);
118 }
119 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
120 
121 /*
122  * This function assumes linkrate mask fits in 8 bits, which it
123  * does for all HW versions supported.
124  */
125 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
126 {
127 	u8 rate = 0;
128 	int i;
129 
130 	max -= SAS_LINK_RATE_1_5_GBPS;
131 	for (i = 0; i <= max; i++)
132 		rate |= 1 << (i * 2);
133 	return rate;
134 }
135 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
136 
137 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
138 {
139 	return device->port->ha->lldd_ha;
140 }
141 
142 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
143 {
144 	return container_of(sas_port, struct hisi_sas_port, sas_port);
145 }
146 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
147 
148 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
149 {
150 	int phy_no;
151 
152 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
153 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
154 }
155 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
156 
157 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
158 {
159 	void *bitmap = hisi_hba->slot_index_tags;
160 
161 	clear_bit(slot_idx, bitmap);
162 }
163 
164 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
165 {
166 	if (hisi_hba->hw->slot_index_alloc ||
167 	    slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
168 		spin_lock(&hisi_hba->lock);
169 		hisi_sas_slot_index_clear(hisi_hba, slot_idx);
170 		spin_unlock(&hisi_hba->lock);
171 	}
172 }
173 
174 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
175 {
176 	void *bitmap = hisi_hba->slot_index_tags;
177 
178 	set_bit(slot_idx, bitmap);
179 }
180 
181 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
182 				     struct scsi_cmnd *scsi_cmnd)
183 {
184 	int index;
185 	void *bitmap = hisi_hba->slot_index_tags;
186 
187 	if (scsi_cmnd)
188 		return scsi_cmnd->request->tag;
189 
190 	spin_lock(&hisi_hba->lock);
191 	index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
192 				   hisi_hba->last_slot_index + 1);
193 	if (index >= hisi_hba->slot_index_count) {
194 		index = find_next_zero_bit(bitmap,
195 				hisi_hba->slot_index_count,
196 				HISI_SAS_UNRESERVED_IPTT);
197 		if (index >= hisi_hba->slot_index_count) {
198 			spin_unlock(&hisi_hba->lock);
199 			return -SAS_QUEUE_FULL;
200 		}
201 	}
202 	hisi_sas_slot_index_set(hisi_hba, index);
203 	hisi_hba->last_slot_index = index;
204 	spin_unlock(&hisi_hba->lock);
205 
206 	return index;
207 }
208 
209 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
210 {
211 	int i;
212 
213 	for (i = 0; i < hisi_hba->slot_index_count; ++i)
214 		hisi_sas_slot_index_clear(hisi_hba, i);
215 }
216 
217 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
218 			     struct hisi_sas_slot *slot)
219 {
220 	int device_id = slot->device_id;
221 	struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
222 
223 	if (task) {
224 		struct device *dev = hisi_hba->dev;
225 
226 		if (!task->lldd_task)
227 			return;
228 
229 		task->lldd_task = NULL;
230 
231 		if (!sas_protocol_ata(task->task_proto)) {
232 			if (slot->n_elem)
233 				dma_unmap_sg(dev, task->scatter,
234 					     task->num_scatter,
235 					     task->data_dir);
236 			if (slot->n_elem_dif) {
237 				struct sas_ssp_task *ssp_task = &task->ssp_task;
238 				struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
239 
240 				dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
241 					     scsi_prot_sg_count(scsi_cmnd),
242 					     task->data_dir);
243 			}
244 		}
245 	}
246 
247 	spin_lock(&sas_dev->lock);
248 	list_del_init(&slot->entry);
249 	spin_unlock(&sas_dev->lock);
250 
251 	memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
252 
253 	hisi_sas_slot_index_free(hisi_hba, slot->idx);
254 }
255 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
256 
257 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
258 				  struct hisi_sas_slot *slot)
259 {
260 	hisi_hba->hw->prep_smp(hisi_hba, slot);
261 }
262 
263 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
264 				  struct hisi_sas_slot *slot)
265 {
266 	hisi_hba->hw->prep_ssp(hisi_hba, slot);
267 }
268 
269 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
270 				  struct hisi_sas_slot *slot)
271 {
272 	hisi_hba->hw->prep_stp(hisi_hba, slot);
273 }
274 
275 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
276 		struct hisi_sas_slot *slot,
277 		int device_id, int abort_flag, int tag_to_abort)
278 {
279 	hisi_hba->hw->prep_abort(hisi_hba, slot,
280 			device_id, abort_flag, tag_to_abort);
281 }
282 
283 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
284 			       struct sas_task *task, int n_elem,
285 			       int n_elem_req)
286 {
287 	struct device *dev = hisi_hba->dev;
288 
289 	if (!sas_protocol_ata(task->task_proto)) {
290 		if (task->num_scatter) {
291 			if (n_elem)
292 				dma_unmap_sg(dev, task->scatter,
293 					     task->num_scatter,
294 					     task->data_dir);
295 		} else if (task->task_proto & SAS_PROTOCOL_SMP) {
296 			if (n_elem_req)
297 				dma_unmap_sg(dev, &task->smp_task.smp_req,
298 					     1, DMA_TO_DEVICE);
299 		}
300 	}
301 }
302 
303 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
304 			    struct sas_task *task, int *n_elem,
305 			    int *n_elem_req)
306 {
307 	struct device *dev = hisi_hba->dev;
308 	int rc;
309 
310 	if (sas_protocol_ata(task->task_proto)) {
311 		*n_elem = task->num_scatter;
312 	} else {
313 		unsigned int req_len;
314 
315 		if (task->num_scatter) {
316 			*n_elem = dma_map_sg(dev, task->scatter,
317 					     task->num_scatter, task->data_dir);
318 			if (!*n_elem) {
319 				rc = -ENOMEM;
320 				goto prep_out;
321 			}
322 		} else if (task->task_proto & SAS_PROTOCOL_SMP) {
323 			*n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
324 						 1, DMA_TO_DEVICE);
325 			if (!*n_elem_req) {
326 				rc = -ENOMEM;
327 				goto prep_out;
328 			}
329 			req_len = sg_dma_len(&task->smp_task.smp_req);
330 			if (req_len & 0x3) {
331 				rc = -EINVAL;
332 				goto err_out_dma_unmap;
333 			}
334 		}
335 	}
336 
337 	if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
338 		dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
339 			*n_elem);
340 		rc = -EINVAL;
341 		goto err_out_dma_unmap;
342 	}
343 	return 0;
344 
345 err_out_dma_unmap:
346 	/* It would be better to call dma_unmap_sg() here, but it's messy */
347 	hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
348 			   *n_elem_req);
349 prep_out:
350 	return rc;
351 }
352 
353 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
354 				   struct sas_task *task, int n_elem_dif)
355 {
356 	struct device *dev = hisi_hba->dev;
357 
358 	if (n_elem_dif) {
359 		struct sas_ssp_task *ssp_task = &task->ssp_task;
360 		struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
361 
362 		dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
363 			     scsi_prot_sg_count(scsi_cmnd),
364 			     task->data_dir);
365 	}
366 }
367 
368 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
369 				int *n_elem_dif, struct sas_task *task)
370 {
371 	struct device *dev = hisi_hba->dev;
372 	struct sas_ssp_task *ssp_task;
373 	struct scsi_cmnd *scsi_cmnd;
374 	int rc;
375 
376 	if (task->num_scatter) {
377 		ssp_task = &task->ssp_task;
378 		scsi_cmnd = ssp_task->cmd;
379 
380 		if (scsi_prot_sg_count(scsi_cmnd)) {
381 			*n_elem_dif = dma_map_sg(dev,
382 						 scsi_prot_sglist(scsi_cmnd),
383 						 scsi_prot_sg_count(scsi_cmnd),
384 						 task->data_dir);
385 
386 			if (!*n_elem_dif)
387 				return -ENOMEM;
388 
389 			if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
390 				dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
391 					*n_elem_dif);
392 				rc = -EINVAL;
393 				goto err_out_dif_dma_unmap;
394 			}
395 		}
396 	}
397 
398 	return 0;
399 
400 err_out_dif_dma_unmap:
401 	dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
402 		     scsi_prot_sg_count(scsi_cmnd), task->data_dir);
403 	return rc;
404 }
405 
406 static int hisi_sas_task_prep(struct sas_task *task,
407 			      struct hisi_sas_dq **dq_pointer,
408 			      bool is_tmf, struct hisi_sas_tmf_task *tmf,
409 			      int *pass)
410 {
411 	struct domain_device *device = task->dev;
412 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
413 	struct hisi_sas_device *sas_dev = device->lldd_dev;
414 	struct hisi_sas_port *port;
415 	struct hisi_sas_slot *slot;
416 	struct hisi_sas_cmd_hdr	*cmd_hdr_base;
417 	struct asd_sas_port *sas_port = device->port;
418 	struct device *dev = hisi_hba->dev;
419 	int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
420 	int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
421 	struct scsi_cmnd *scmd = NULL;
422 	struct hisi_sas_dq *dq;
423 	unsigned long flags;
424 	int wr_q_index;
425 
426 	if (DEV_IS_GONE(sas_dev)) {
427 		if (sas_dev)
428 			dev_info(dev, "task prep: device %d not ready\n",
429 				 sas_dev->device_id);
430 		else
431 			dev_info(dev, "task prep: device %016llx not ready\n",
432 				 SAS_ADDR(device->sas_addr));
433 
434 		return -ECOMM;
435 	}
436 
437 	if (task->uldd_task) {
438 		struct ata_queued_cmd *qc;
439 
440 		if (dev_is_sata(device)) {
441 			qc = task->uldd_task;
442 			scmd = qc->scsicmd;
443 		} else {
444 			scmd = task->uldd_task;
445 		}
446 	}
447 
448 	if (scmd) {
449 		unsigned int dq_index;
450 		u32 blk_tag;
451 
452 		blk_tag = blk_mq_unique_tag(scmd->request);
453 		dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
454 		*dq_pointer = dq = &hisi_hba->dq[dq_index];
455 	} else {
456 		struct Scsi_Host *shost = hisi_hba->shost;
457 		struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
458 		int queue = qmap->mq_map[raw_smp_processor_id()];
459 
460 		*dq_pointer = dq = &hisi_hba->dq[queue];
461 	}
462 
463 	port = to_hisi_sas_port(sas_port);
464 	if (port && !port->port_attached) {
465 		dev_info(dev, "task prep: %s port%d not attach device\n",
466 			 (dev_is_sata(device)) ?
467 			 "SATA/STP" : "SAS",
468 			 device->port->id);
469 
470 		return -ECOMM;
471 	}
472 
473 	rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
474 			      &n_elem_req);
475 	if (rc < 0)
476 		goto prep_out;
477 
478 	if (!sas_protocol_ata(task->task_proto)) {
479 		rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
480 		if (rc < 0)
481 			goto err_out_dma_unmap;
482 	}
483 
484 	if (hisi_hba->hw->slot_index_alloc)
485 		rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
486 	else
487 		rc = hisi_sas_slot_index_alloc(hisi_hba, scmd);
488 
489 	if (rc < 0)
490 		goto err_out_dif_dma_unmap;
491 
492 	slot_idx = rc;
493 	slot = &hisi_hba->slot_info[slot_idx];
494 
495 	spin_lock(&dq->lock);
496 	wr_q_index = dq->wr_point;
497 	dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
498 	list_add_tail(&slot->delivery, &dq->list);
499 	spin_unlock(&dq->lock);
500 	spin_lock(&sas_dev->lock);
501 	list_add_tail(&slot->entry, &sas_dev->list);
502 	spin_unlock(&sas_dev->lock);
503 
504 	dlvry_queue = dq->id;
505 	dlvry_queue_slot = wr_q_index;
506 
507 	slot->device_id = sas_dev->device_id;
508 	slot->n_elem = n_elem;
509 	slot->n_elem_dif = n_elem_dif;
510 	slot->dlvry_queue = dlvry_queue;
511 	slot->dlvry_queue_slot = dlvry_queue_slot;
512 	cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
513 	slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
514 	slot->task = task;
515 	slot->port = port;
516 	slot->tmf = tmf;
517 	slot->is_internal = is_tmf;
518 	task->lldd_task = slot;
519 
520 	memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
521 	memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
522 	memset(hisi_sas_status_buf_addr_mem(slot), 0,
523 	       sizeof(struct hisi_sas_err_record));
524 
525 	switch (task->task_proto) {
526 	case SAS_PROTOCOL_SMP:
527 		hisi_sas_task_prep_smp(hisi_hba, slot);
528 		break;
529 	case SAS_PROTOCOL_SSP:
530 		hisi_sas_task_prep_ssp(hisi_hba, slot);
531 		break;
532 	case SAS_PROTOCOL_SATA:
533 	case SAS_PROTOCOL_STP:
534 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
535 		hisi_sas_task_prep_ata(hisi_hba, slot);
536 		break;
537 	default:
538 		dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
539 			task->task_proto);
540 		break;
541 	}
542 
543 	spin_lock_irqsave(&task->task_state_lock, flags);
544 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
545 	spin_unlock_irqrestore(&task->task_state_lock, flags);
546 
547 	++(*pass);
548 	WRITE_ONCE(slot->ready, 1);
549 
550 	return 0;
551 
552 err_out_dif_dma_unmap:
553 	if (!sas_protocol_ata(task->task_proto))
554 		hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
555 err_out_dma_unmap:
556 	hisi_sas_dma_unmap(hisi_hba, task, n_elem,
557 			   n_elem_req);
558 prep_out:
559 	dev_err(dev, "task prep: failed[%d]!\n", rc);
560 	return rc;
561 }
562 
563 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
564 			      bool is_tmf, struct hisi_sas_tmf_task *tmf)
565 {
566 	u32 rc;
567 	u32 pass = 0;
568 	struct hisi_hba *hisi_hba;
569 	struct device *dev;
570 	struct domain_device *device = task->dev;
571 	struct asd_sas_port *sas_port = device->port;
572 	struct hisi_sas_dq *dq = NULL;
573 
574 	if (!sas_port) {
575 		struct task_status_struct *ts = &task->task_status;
576 
577 		ts->resp = SAS_TASK_UNDELIVERED;
578 		ts->stat = SAS_PHY_DOWN;
579 		/*
580 		 * libsas will use dev->port, should
581 		 * not call task_done for sata
582 		 */
583 		if (device->dev_type != SAS_SATA_DEV)
584 			task->task_done(task);
585 		return -ECOMM;
586 	}
587 
588 	hisi_hba = dev_to_hisi_hba(device);
589 	dev = hisi_hba->dev;
590 
591 	if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
592 		if (!gfpflags_allow_blocking(gfp_flags))
593 			return -EINVAL;
594 
595 		down(&hisi_hba->sem);
596 		up(&hisi_hba->sem);
597 	}
598 
599 	/* protect task_prep and start_delivery sequence */
600 	rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
601 	if (rc)
602 		dev_err(dev, "task exec: failed[%d]!\n", rc);
603 
604 	if (likely(pass)) {
605 		spin_lock(&dq->lock);
606 		hisi_hba->hw->start_delivery(dq);
607 		spin_unlock(&dq->lock);
608 	}
609 
610 	return rc;
611 }
612 
613 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no,
614 				 gfp_t gfp_flags)
615 {
616 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
617 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
618 
619 	if (!phy->phy_attached)
620 		return;
621 
622 	if (test_bit(HISI_SAS_PM_BIT, &hisi_hba->flags) &&
623 	    !sas_phy->suspended) {
624 		dev_warn(hisi_hba->dev, "phy%d during suspend filtered out\n", phy_no);
625 		return;
626 	}
627 
628 	sas_notify_phy_event(sas_phy, PHYE_OOB_DONE, gfp_flags);
629 
630 	if (sas_phy->phy) {
631 		struct sas_phy *sphy = sas_phy->phy;
632 
633 		sphy->negotiated_linkrate = sas_phy->linkrate;
634 		sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
635 		sphy->maximum_linkrate_hw =
636 			hisi_hba->hw->phy_get_max_linkrate();
637 		if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
638 			sphy->minimum_linkrate = phy->minimum_linkrate;
639 
640 		if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
641 			sphy->maximum_linkrate = phy->maximum_linkrate;
642 	}
643 
644 	if (phy->phy_type & PORT_TYPE_SAS) {
645 		struct sas_identify_frame *id;
646 
647 		id = (struct sas_identify_frame *)phy->frame_rcvd;
648 		id->dev_type = phy->identify.device_type;
649 		id->initiator_bits = SAS_PROTOCOL_ALL;
650 		id->target_bits = phy->identify.target_port_protocols;
651 	} else if (phy->phy_type & PORT_TYPE_SATA) {
652 		/* Nothing */
653 	}
654 
655 	sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
656 	sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED, gfp_flags);
657 }
658 
659 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
660 {
661 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
662 	struct hisi_sas_device *sas_dev = NULL;
663 	int last = hisi_hba->last_dev_id;
664 	int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
665 	int i;
666 
667 	spin_lock(&hisi_hba->lock);
668 	for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
669 		if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
670 			int queue = i % hisi_hba->queue_count;
671 			struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
672 
673 			hisi_hba->devices[i].device_id = i;
674 			sas_dev = &hisi_hba->devices[i];
675 			sas_dev->dev_status = HISI_SAS_DEV_INIT;
676 			sas_dev->dev_type = device->dev_type;
677 			sas_dev->hisi_hba = hisi_hba;
678 			sas_dev->sas_device = device;
679 			sas_dev->dq = dq;
680 			spin_lock_init(&sas_dev->lock);
681 			INIT_LIST_HEAD(&hisi_hba->devices[i].list);
682 			break;
683 		}
684 		i++;
685 	}
686 	hisi_hba->last_dev_id = i;
687 	spin_unlock(&hisi_hba->lock);
688 
689 	return sas_dev;
690 }
691 
692 #define HISI_SAS_DISK_RECOVER_CNT 3
693 static int hisi_sas_init_device(struct domain_device *device)
694 {
695 	int rc = TMF_RESP_FUNC_COMPLETE;
696 	struct scsi_lun lun;
697 	struct hisi_sas_tmf_task tmf_task;
698 	int retry = HISI_SAS_DISK_RECOVER_CNT;
699 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
700 	struct device *dev = hisi_hba->dev;
701 	struct sas_phy *local_phy;
702 
703 	switch (device->dev_type) {
704 	case SAS_END_DEVICE:
705 		int_to_scsilun(0, &lun);
706 
707 		tmf_task.tmf = TMF_CLEAR_TASK_SET;
708 		while (retry-- > 0) {
709 			rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
710 							  &tmf_task);
711 			if (rc == TMF_RESP_FUNC_COMPLETE) {
712 				hisi_sas_release_task(hisi_hba, device);
713 				break;
714 			}
715 		}
716 		break;
717 	case SAS_SATA_DEV:
718 	case SAS_SATA_PM:
719 	case SAS_SATA_PM_PORT:
720 	case SAS_SATA_PENDING:
721 		/*
722 		 * send HARD RESET to clear previous affiliation of
723 		 * STP target port
724 		 */
725 		local_phy = sas_get_local_phy(device);
726 		if (!scsi_is_sas_phy_local(local_phy) &&
727 		    !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
728 			unsigned long deadline = ata_deadline(jiffies, 20000);
729 			struct sata_device *sata_dev = &device->sata_dev;
730 			struct ata_host *ata_host = sata_dev->ata_host;
731 			struct ata_port_operations *ops = ata_host->ops;
732 			struct ata_port *ap = sata_dev->ap;
733 			struct ata_link *link;
734 			unsigned int classes;
735 
736 			ata_for_each_link(link, ap, EDGE)
737 				rc = ops->hardreset(link, &classes,
738 						    deadline);
739 		}
740 		sas_put_local_phy(local_phy);
741 		if (rc) {
742 			dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
743 			return rc;
744 		}
745 
746 		while (retry-- > 0) {
747 			rc = hisi_sas_softreset_ata_disk(device);
748 			if (!rc)
749 				break;
750 		}
751 		break;
752 	default:
753 		break;
754 	}
755 
756 	return rc;
757 }
758 
759 static int hisi_sas_dev_found(struct domain_device *device)
760 {
761 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
762 	struct domain_device *parent_dev = device->parent;
763 	struct hisi_sas_device *sas_dev;
764 	struct device *dev = hisi_hba->dev;
765 	int rc;
766 
767 	if (hisi_hba->hw->alloc_dev)
768 		sas_dev = hisi_hba->hw->alloc_dev(device);
769 	else
770 		sas_dev = hisi_sas_alloc_dev(device);
771 	if (!sas_dev) {
772 		dev_err(dev, "fail alloc dev: max support %d devices\n",
773 			HISI_SAS_MAX_DEVICES);
774 		return -EINVAL;
775 	}
776 
777 	device->lldd_dev = sas_dev;
778 	hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
779 
780 	if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
781 		int phy_no;
782 		u8 phy_num = parent_dev->ex_dev.num_phys;
783 		struct ex_phy *phy;
784 
785 		for (phy_no = 0; phy_no < phy_num; phy_no++) {
786 			phy = &parent_dev->ex_dev.ex_phy[phy_no];
787 			if (SAS_ADDR(phy->attached_sas_addr) ==
788 				SAS_ADDR(device->sas_addr))
789 				break;
790 		}
791 
792 		if (phy_no == phy_num) {
793 			dev_info(dev, "dev found: no attached "
794 				 "dev:%016llx at ex:%016llx\n",
795 				 SAS_ADDR(device->sas_addr),
796 				 SAS_ADDR(parent_dev->sas_addr));
797 			rc = -EINVAL;
798 			goto err_out;
799 		}
800 	}
801 
802 	dev_info(dev, "dev[%d:%x] found\n",
803 		sas_dev->device_id, sas_dev->dev_type);
804 
805 	rc = hisi_sas_init_device(device);
806 	if (rc)
807 		goto err_out;
808 	sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
809 	return 0;
810 
811 err_out:
812 	hisi_sas_dev_gone(device);
813 	return rc;
814 }
815 
816 int hisi_sas_slave_configure(struct scsi_device *sdev)
817 {
818 	struct domain_device *dev = sdev_to_domain_dev(sdev);
819 	int ret = sas_slave_configure(sdev);
820 
821 	if (ret)
822 		return ret;
823 	if (!dev_is_sata(dev))
824 		sas_change_queue_depth(sdev, 64);
825 
826 	return 0;
827 }
828 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
829 
830 void hisi_sas_scan_start(struct Scsi_Host *shost)
831 {
832 	struct hisi_hba *hisi_hba = shost_priv(shost);
833 
834 	hisi_hba->hw->phys_init(hisi_hba);
835 }
836 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
837 
838 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
839 {
840 	struct hisi_hba *hisi_hba = shost_priv(shost);
841 	struct sas_ha_struct *sha = &hisi_hba->sha;
842 
843 	/* Wait for PHY up interrupt to occur */
844 	if (time < HZ)
845 		return 0;
846 
847 	sas_drain_work(sha);
848 	return 1;
849 }
850 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
851 
852 static void hisi_sas_phyup_work(struct work_struct *work)
853 {
854 	struct hisi_sas_phy *phy =
855 		container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
856 	struct hisi_hba *hisi_hba = phy->hisi_hba;
857 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
858 	int phy_no = sas_phy->id;
859 
860 	if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
861 		hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
862 	hisi_sas_bytes_dmaed(hisi_hba, phy_no, GFP_KERNEL);
863 }
864 
865 static void hisi_sas_linkreset_work(struct work_struct *work)
866 {
867 	struct hisi_sas_phy *phy =
868 		container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
869 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
870 
871 	hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
872 }
873 
874 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
875 	[HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
876 	[HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
877 };
878 
879 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
880 				enum hisi_sas_phy_event event)
881 {
882 	struct hisi_hba *hisi_hba = phy->hisi_hba;
883 
884 	if (WARN_ON(event >= HISI_PHYES_NUM))
885 		return false;
886 
887 	return queue_work(hisi_hba->wq, &phy->works[event]);
888 }
889 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
890 
891 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
892 {
893 	struct hisi_sas_phy *phy = from_timer(phy, t, timer);
894 	struct hisi_hba *hisi_hba = phy->hisi_hba;
895 	struct device *dev = hisi_hba->dev;
896 	int phy_no = phy->sas_phy.id;
897 
898 	dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
899 	hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
900 }
901 
902 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
903 {
904 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
905 	struct device *dev = hisi_hba->dev;
906 
907 	dev_dbg(dev, "phy%d OOB ready\n", phy_no);
908 	if (phy->phy_attached)
909 		return;
910 
911 	if (!timer_pending(&phy->timer)) {
912 		phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
913 		add_timer(&phy->timer);
914 	}
915 }
916 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
917 
918 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
919 {
920 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
921 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
922 	int i;
923 
924 	phy->hisi_hba = hisi_hba;
925 	phy->port = NULL;
926 	phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
927 	phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
928 	sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
929 	sas_phy->class = SAS;
930 	sas_phy->iproto = SAS_PROTOCOL_ALL;
931 	sas_phy->tproto = 0;
932 	sas_phy->type = PHY_TYPE_PHYSICAL;
933 	sas_phy->role = PHY_ROLE_INITIATOR;
934 	sas_phy->oob_mode = OOB_NOT_CONNECTED;
935 	sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
936 	sas_phy->id = phy_no;
937 	sas_phy->sas_addr = &hisi_hba->sas_addr[0];
938 	sas_phy->frame_rcvd = &phy->frame_rcvd[0];
939 	sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
940 	sas_phy->lldd_phy = phy;
941 
942 	for (i = 0; i < HISI_PHYES_NUM; i++)
943 		INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
944 
945 	spin_lock_init(&phy->lock);
946 
947 	timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
948 }
949 
950 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
951 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
952 {
953 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
954 	struct asd_sas_phy *aphy = &phy->sas_phy;
955 	struct sas_phy *sphy = aphy->phy;
956 	unsigned long flags;
957 
958 	spin_lock_irqsave(&phy->lock, flags);
959 
960 	if (enable) {
961 		/* We may have been enabled already; if so, don't touch */
962 		if (!phy->enable)
963 			sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
964 		hisi_hba->hw->phy_start(hisi_hba, phy_no);
965 	} else {
966 		sphy->negotiated_linkrate = SAS_PHY_DISABLED;
967 		hisi_hba->hw->phy_disable(hisi_hba, phy_no);
968 	}
969 	phy->enable = enable;
970 	spin_unlock_irqrestore(&phy->lock, flags);
971 }
972 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
973 
974 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
975 {
976 	struct sas_ha_struct *sas_ha = sas_phy->ha;
977 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
978 	struct hisi_sas_phy *phy = sas_phy->lldd_phy;
979 	struct asd_sas_port *sas_port = sas_phy->port;
980 	struct hisi_sas_port *port;
981 	unsigned long flags;
982 
983 	if (!sas_port)
984 		return;
985 
986 	port = to_hisi_sas_port(sas_port);
987 	spin_lock_irqsave(&hisi_hba->lock, flags);
988 	port->port_attached = 1;
989 	port->id = phy->port_id;
990 	phy->port = port;
991 	sas_port->lldd_port = port;
992 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
993 }
994 
995 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
996 				     struct hisi_sas_slot *slot)
997 {
998 	if (task) {
999 		unsigned long flags;
1000 		struct task_status_struct *ts;
1001 
1002 		ts = &task->task_status;
1003 
1004 		ts->resp = SAS_TASK_COMPLETE;
1005 		ts->stat = SAS_ABORTED_TASK;
1006 		spin_lock_irqsave(&task->task_state_lock, flags);
1007 		task->task_state_flags &=
1008 			~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1009 		if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1010 			task->task_state_flags |= SAS_TASK_STATE_DONE;
1011 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1012 	}
1013 
1014 	hisi_sas_slot_task_free(hisi_hba, task, slot);
1015 }
1016 
1017 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1018 			struct domain_device *device)
1019 {
1020 	struct hisi_sas_slot *slot, *slot2;
1021 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1022 
1023 	list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1024 		hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1025 }
1026 
1027 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1028 {
1029 	struct hisi_sas_device *sas_dev;
1030 	struct domain_device *device;
1031 	int i;
1032 
1033 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1034 		sas_dev = &hisi_hba->devices[i];
1035 		device = sas_dev->sas_device;
1036 
1037 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1038 		    !device)
1039 			continue;
1040 
1041 		hisi_sas_release_task(hisi_hba, device);
1042 	}
1043 }
1044 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1045 
1046 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1047 				struct domain_device *device)
1048 {
1049 	if (hisi_hba->hw->dereg_device)
1050 		hisi_hba->hw->dereg_device(hisi_hba, device);
1051 }
1052 
1053 static void hisi_sas_dev_gone(struct domain_device *device)
1054 {
1055 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1056 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1057 	struct device *dev = hisi_hba->dev;
1058 	int ret = 0;
1059 
1060 	dev_info(dev, "dev[%d:%x] is gone\n",
1061 		 sas_dev->device_id, sas_dev->dev_type);
1062 
1063 	down(&hisi_hba->sem);
1064 	if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1065 		hisi_sas_internal_task_abort(hisi_hba, device,
1066 					     HISI_SAS_INT_ABT_DEV, 0);
1067 
1068 		hisi_sas_dereg_device(hisi_hba, device);
1069 
1070 		ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1071 		device->lldd_dev = NULL;
1072 	}
1073 
1074 	if (hisi_hba->hw->free_device)
1075 		hisi_hba->hw->free_device(sas_dev);
1076 
1077 	/* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1078 	if (!ret)
1079 		sas_dev->dev_type = SAS_PHY_UNUSED;
1080 	sas_dev->sas_device = NULL;
1081 	up(&hisi_hba->sem);
1082 }
1083 
1084 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1085 {
1086 	return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1087 }
1088 
1089 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1090 			struct sas_phy_linkrates *r)
1091 {
1092 	struct sas_phy_linkrates _r;
1093 
1094 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1095 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1096 	enum sas_linkrate min, max;
1097 
1098 	if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1099 		return -EINVAL;
1100 
1101 	if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1102 		max = sas_phy->phy->maximum_linkrate;
1103 		min = r->minimum_linkrate;
1104 	} else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1105 		max = r->maximum_linkrate;
1106 		min = sas_phy->phy->minimum_linkrate;
1107 	} else
1108 		return -EINVAL;
1109 
1110 	_r.maximum_linkrate = max;
1111 	_r.minimum_linkrate = min;
1112 
1113 	sas_phy->phy->maximum_linkrate = max;
1114 	sas_phy->phy->minimum_linkrate = min;
1115 
1116 	hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1117 	msleep(100);
1118 	hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1119 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1120 
1121 	return 0;
1122 }
1123 
1124 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1125 				void *funcdata)
1126 {
1127 	struct sas_ha_struct *sas_ha = sas_phy->ha;
1128 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1129 	int phy_no = sas_phy->id;
1130 
1131 	switch (func) {
1132 	case PHY_FUNC_HARD_RESET:
1133 		hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1134 		break;
1135 
1136 	case PHY_FUNC_LINK_RESET:
1137 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1138 		msleep(100);
1139 		hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1140 		break;
1141 
1142 	case PHY_FUNC_DISABLE:
1143 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1144 		break;
1145 
1146 	case PHY_FUNC_SET_LINK_RATE:
1147 		return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1148 	case PHY_FUNC_GET_EVENTS:
1149 		if (hisi_hba->hw->get_events) {
1150 			hisi_hba->hw->get_events(hisi_hba, phy_no);
1151 			break;
1152 		}
1153 		fallthrough;
1154 	case PHY_FUNC_RELEASE_SPINUP_HOLD:
1155 	default:
1156 		return -EOPNOTSUPP;
1157 	}
1158 	return 0;
1159 }
1160 
1161 static void hisi_sas_task_done(struct sas_task *task)
1162 {
1163 	del_timer(&task->slow_task->timer);
1164 	complete(&task->slow_task->completion);
1165 }
1166 
1167 static void hisi_sas_tmf_timedout(struct timer_list *t)
1168 {
1169 	struct sas_task_slow *slow = from_timer(slow, t, timer);
1170 	struct sas_task *task = slow->task;
1171 	unsigned long flags;
1172 	bool is_completed = true;
1173 
1174 	spin_lock_irqsave(&task->task_state_lock, flags);
1175 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1176 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1177 		is_completed = false;
1178 	}
1179 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1180 
1181 	if (!is_completed)
1182 		complete(&task->slow_task->completion);
1183 }
1184 
1185 #define TASK_TIMEOUT 20
1186 #define TASK_RETRY 3
1187 #define INTERNAL_ABORT_TIMEOUT 6
1188 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1189 					   void *parameter, u32 para_len,
1190 					   struct hisi_sas_tmf_task *tmf)
1191 {
1192 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1193 	struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1194 	struct device *dev = hisi_hba->dev;
1195 	struct sas_task *task;
1196 	int res, retry;
1197 
1198 	for (retry = 0; retry < TASK_RETRY; retry++) {
1199 		task = sas_alloc_slow_task(GFP_KERNEL);
1200 		if (!task)
1201 			return -ENOMEM;
1202 
1203 		task->dev = device;
1204 		task->task_proto = device->tproto;
1205 
1206 		if (dev_is_sata(device)) {
1207 			task->ata_task.device_control_reg_update = 1;
1208 			memcpy(&task->ata_task.fis, parameter, para_len);
1209 		} else {
1210 			memcpy(&task->ssp_task, parameter, para_len);
1211 		}
1212 		task->task_done = hisi_sas_task_done;
1213 
1214 		task->slow_task->timer.function = hisi_sas_tmf_timedout;
1215 		task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1216 		add_timer(&task->slow_task->timer);
1217 
1218 		res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1219 
1220 		if (res) {
1221 			del_timer(&task->slow_task->timer);
1222 			dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1223 				res);
1224 			goto ex_err;
1225 		}
1226 
1227 		wait_for_completion(&task->slow_task->completion);
1228 		res = TMF_RESP_FUNC_FAILED;
1229 		/* Even TMF timed out, return direct. */
1230 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1231 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1232 				struct hisi_sas_slot *slot = task->lldd_task;
1233 
1234 				dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1235 				if (slot) {
1236 					struct hisi_sas_cq *cq =
1237 					       &hisi_hba->cq[slot->dlvry_queue];
1238 					/*
1239 					 * sync irq to avoid free'ing task
1240 					 * before using task in IO completion
1241 					 */
1242 					synchronize_irq(cq->irq_no);
1243 					slot->task = NULL;
1244 				}
1245 
1246 				goto ex_err;
1247 			} else
1248 				dev_err(dev, "abort tmf: TMF task timeout\n");
1249 		}
1250 
1251 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1252 		     task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1253 			res = TMF_RESP_FUNC_COMPLETE;
1254 			break;
1255 		}
1256 
1257 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1258 			task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1259 			res = TMF_RESP_FUNC_SUCC;
1260 			break;
1261 		}
1262 
1263 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1264 		      task->task_status.stat == SAS_DATA_UNDERRUN) {
1265 			/* no error, but return the number of bytes of
1266 			 * underrun
1267 			 */
1268 			dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1269 				 SAS_ADDR(device->sas_addr),
1270 				 task->task_status.resp,
1271 				 task->task_status.stat);
1272 			res = task->task_status.residual;
1273 			break;
1274 		}
1275 
1276 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1277 			task->task_status.stat == SAS_DATA_OVERRUN) {
1278 			dev_warn(dev, "abort tmf: blocked task error\n");
1279 			res = -EMSGSIZE;
1280 			break;
1281 		}
1282 
1283 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1284 		    task->task_status.stat == SAS_OPEN_REJECT) {
1285 			dev_warn(dev, "abort tmf: open reject failed\n");
1286 			res = -EIO;
1287 		} else {
1288 			dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1289 				 SAS_ADDR(device->sas_addr),
1290 				 task->task_status.resp,
1291 				 task->task_status.stat);
1292 		}
1293 		sas_free_task(task);
1294 		task = NULL;
1295 	}
1296 ex_err:
1297 	if (retry == TASK_RETRY)
1298 		dev_warn(dev, "abort tmf: executing internal task failed!\n");
1299 	sas_free_task(task);
1300 	return res;
1301 }
1302 
1303 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1304 		bool reset, int pmp, u8 *fis)
1305 {
1306 	struct ata_taskfile tf;
1307 
1308 	ata_tf_init(dev, &tf);
1309 	if (reset)
1310 		tf.ctl |= ATA_SRST;
1311 	else
1312 		tf.ctl &= ~ATA_SRST;
1313 	tf.command = ATA_CMD_DEV_RESET;
1314 	ata_tf_to_fis(&tf, pmp, 0, fis);
1315 }
1316 
1317 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1318 {
1319 	u8 fis[20] = {0};
1320 	struct ata_port *ap = device->sata_dev.ap;
1321 	struct ata_link *link;
1322 	int rc = TMF_RESP_FUNC_FAILED;
1323 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1324 	struct device *dev = hisi_hba->dev;
1325 	int s = sizeof(struct host_to_dev_fis);
1326 
1327 	ata_for_each_link(link, ap, EDGE) {
1328 		int pmp = sata_srst_pmp(link);
1329 
1330 		hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1331 		rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1332 		if (rc != TMF_RESP_FUNC_COMPLETE)
1333 			break;
1334 	}
1335 
1336 	if (rc == TMF_RESP_FUNC_COMPLETE) {
1337 		ata_for_each_link(link, ap, EDGE) {
1338 			int pmp = sata_srst_pmp(link);
1339 
1340 			hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1341 			rc = hisi_sas_exec_internal_tmf_task(device, fis,
1342 							     s, NULL);
1343 			if (rc != TMF_RESP_FUNC_COMPLETE)
1344 				dev_err(dev, "ata disk %016llx de-reset failed\n",
1345 					SAS_ADDR(device->sas_addr));
1346 		}
1347 	} else {
1348 		dev_err(dev, "ata disk %016llx reset failed\n",
1349 			SAS_ADDR(device->sas_addr));
1350 	}
1351 
1352 	if (rc == TMF_RESP_FUNC_COMPLETE)
1353 		hisi_sas_release_task(hisi_hba, device);
1354 
1355 	return rc;
1356 }
1357 
1358 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1359 				u8 *lun, struct hisi_sas_tmf_task *tmf)
1360 {
1361 	struct sas_ssp_task ssp_task;
1362 
1363 	if (!(device->tproto & SAS_PROTOCOL_SSP))
1364 		return TMF_RESP_FUNC_ESUPP;
1365 
1366 	memcpy(ssp_task.LUN, lun, 8);
1367 
1368 	return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1369 				sizeof(ssp_task), tmf);
1370 }
1371 
1372 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1373 {
1374 	u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1375 	int i;
1376 
1377 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1378 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1379 		struct domain_device *device = sas_dev->sas_device;
1380 		struct asd_sas_port *sas_port;
1381 		struct hisi_sas_port *port;
1382 		struct hisi_sas_phy *phy = NULL;
1383 		struct asd_sas_phy *sas_phy;
1384 
1385 		if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1386 				|| !device || !device->port)
1387 			continue;
1388 
1389 		sas_port = device->port;
1390 		port = to_hisi_sas_port(sas_port);
1391 
1392 		list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1393 			if (state & BIT(sas_phy->id)) {
1394 				phy = sas_phy->lldd_phy;
1395 				break;
1396 			}
1397 
1398 		if (phy) {
1399 			port->id = phy->port_id;
1400 
1401 			/* Update linkrate of directly attached device. */
1402 			if (!device->parent)
1403 				device->linkrate = phy->sas_phy.linkrate;
1404 
1405 			hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1406 		} else
1407 			port->id = 0xff;
1408 	}
1409 }
1410 
1411 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1412 {
1413 	struct asd_sas_port *_sas_port = NULL;
1414 	int phy_no;
1415 
1416 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1417 		struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1418 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1419 		struct asd_sas_port *sas_port = sas_phy->port;
1420 		bool do_port_check = _sas_port != sas_port;
1421 
1422 		if (!sas_phy->phy->enabled)
1423 			continue;
1424 
1425 		/* Report PHY state change to libsas */
1426 		if (state & BIT(phy_no)) {
1427 			if (do_port_check && sas_port && sas_port->port_dev) {
1428 				struct domain_device *dev = sas_port->port_dev;
1429 
1430 				_sas_port = sas_port;
1431 
1432 				if (dev_is_expander(dev->dev_type))
1433 					sas_notify_port_event(sas_phy,
1434 							PORTE_BROADCAST_RCVD,
1435 							GFP_KERNEL);
1436 			}
1437 		} else {
1438 			hisi_sas_phy_down(hisi_hba, phy_no, 0, GFP_KERNEL);
1439 		}
1440 	}
1441 }
1442 
1443 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1444 {
1445 	struct hisi_sas_device *sas_dev;
1446 	struct domain_device *device;
1447 	int i;
1448 
1449 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1450 		sas_dev = &hisi_hba->devices[i];
1451 		device = sas_dev->sas_device;
1452 
1453 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1454 			continue;
1455 
1456 		hisi_sas_init_device(device);
1457 	}
1458 }
1459 
1460 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1461 					     struct asd_sas_port *sas_port,
1462 					     struct domain_device *device)
1463 {
1464 	struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1465 	struct ata_port *ap = device->sata_dev.ap;
1466 	struct device *dev = hisi_hba->dev;
1467 	int s = sizeof(struct host_to_dev_fis);
1468 	int rc = TMF_RESP_FUNC_FAILED;
1469 	struct asd_sas_phy *sas_phy;
1470 	struct ata_link *link;
1471 	u8 fis[20] = {0};
1472 	u32 state;
1473 
1474 	state = hisi_hba->hw->get_phys_state(hisi_hba);
1475 	list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1476 		if (!(state & BIT(sas_phy->id)))
1477 			continue;
1478 
1479 		ata_for_each_link(link, ap, EDGE) {
1480 			int pmp = sata_srst_pmp(link);
1481 
1482 			tmf_task.phy_id = sas_phy->id;
1483 			hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1484 			rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1485 							     &tmf_task);
1486 			if (rc != TMF_RESP_FUNC_COMPLETE) {
1487 				dev_err(dev, "phy%d ata reset failed rc=%d\n",
1488 					sas_phy->id, rc);
1489 				break;
1490 			}
1491 		}
1492 	}
1493 }
1494 
1495 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1496 {
1497 	struct device *dev = hisi_hba->dev;
1498 	int port_no, rc, i;
1499 
1500 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1501 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1502 		struct domain_device *device = sas_dev->sas_device;
1503 
1504 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1505 			continue;
1506 
1507 		rc = hisi_sas_internal_task_abort(hisi_hba, device,
1508 						  HISI_SAS_INT_ABT_DEV, 0);
1509 		if (rc < 0)
1510 			dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1511 	}
1512 
1513 	for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1514 		struct hisi_sas_port *port = &hisi_hba->port[port_no];
1515 		struct asd_sas_port *sas_port = &port->sas_port;
1516 		struct domain_device *port_dev = sas_port->port_dev;
1517 		struct domain_device *device;
1518 
1519 		if (!port_dev || !dev_is_expander(port_dev->dev_type))
1520 			continue;
1521 
1522 		/* Try to find a SATA device */
1523 		list_for_each_entry(device, &sas_port->dev_list,
1524 				    dev_list_node) {
1525 			if (dev_is_sata(device)) {
1526 				hisi_sas_send_ata_reset_each_phy(hisi_hba,
1527 								 sas_port,
1528 								 device);
1529 				break;
1530 			}
1531 		}
1532 	}
1533 }
1534 
1535 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1536 {
1537 	struct Scsi_Host *shost = hisi_hba->shost;
1538 
1539 	down(&hisi_hba->sem);
1540 	hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1541 
1542 	scsi_block_requests(shost);
1543 	hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1544 
1545 	if (timer_pending(&hisi_hba->timer))
1546 		del_timer_sync(&hisi_hba->timer);
1547 
1548 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1549 }
1550 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1551 
1552 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1553 {
1554 	struct Scsi_Host *shost = hisi_hba->shost;
1555 
1556 	/* Init and wait for PHYs to come up and all libsas event finished. */
1557 	hisi_hba->hw->phys_init(hisi_hba);
1558 	msleep(1000);
1559 	hisi_sas_refresh_port_id(hisi_hba);
1560 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1561 
1562 	if (hisi_hba->reject_stp_links_msk)
1563 		hisi_sas_terminate_stp_reject(hisi_hba);
1564 	hisi_sas_reset_init_all_devices(hisi_hba);
1565 	up(&hisi_hba->sem);
1566 	scsi_unblock_requests(shost);
1567 	clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1568 
1569 	hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1570 }
1571 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1572 
1573 static int hisi_sas_controller_prereset(struct hisi_hba *hisi_hba)
1574 {
1575 	if (!hisi_hba->hw->soft_reset)
1576 		return -1;
1577 
1578 	if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1579 		return -1;
1580 
1581 	if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1582 		hisi_hba->hw->debugfs_snapshot_regs(hisi_hba);
1583 
1584 	return 0;
1585 }
1586 
1587 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1588 {
1589 	struct device *dev = hisi_hba->dev;
1590 	struct Scsi_Host *shost = hisi_hba->shost;
1591 	int rc;
1592 
1593 	dev_info(dev, "controller resetting...\n");
1594 	hisi_sas_controller_reset_prepare(hisi_hba);
1595 
1596 	rc = hisi_hba->hw->soft_reset(hisi_hba);
1597 	if (rc) {
1598 		dev_warn(dev, "controller reset failed (%d)\n", rc);
1599 		clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1600 		up(&hisi_hba->sem);
1601 		scsi_unblock_requests(shost);
1602 		clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1603 		return rc;
1604 	}
1605 
1606 	hisi_sas_controller_reset_done(hisi_hba);
1607 	dev_info(dev, "controller reset complete\n");
1608 
1609 	return 0;
1610 }
1611 
1612 static int hisi_sas_abort_task(struct sas_task *task)
1613 {
1614 	struct scsi_lun lun;
1615 	struct hisi_sas_tmf_task tmf_task;
1616 	struct domain_device *device = task->dev;
1617 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1618 	struct hisi_hba *hisi_hba;
1619 	struct device *dev;
1620 	int rc = TMF_RESP_FUNC_FAILED;
1621 	unsigned long flags;
1622 
1623 	if (!sas_dev)
1624 		return TMF_RESP_FUNC_FAILED;
1625 
1626 	hisi_hba = dev_to_hisi_hba(task->dev);
1627 	dev = hisi_hba->dev;
1628 
1629 	spin_lock_irqsave(&task->task_state_lock, flags);
1630 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1631 		struct hisi_sas_slot *slot = task->lldd_task;
1632 		struct hisi_sas_cq *cq;
1633 
1634 		if (slot) {
1635 			/*
1636 			 * sync irq to avoid free'ing task
1637 			 * before using task in IO completion
1638 			 */
1639 			cq = &hisi_hba->cq[slot->dlvry_queue];
1640 			synchronize_irq(cq->irq_no);
1641 		}
1642 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1643 		rc = TMF_RESP_FUNC_COMPLETE;
1644 		goto out;
1645 	}
1646 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1647 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1648 
1649 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1650 		struct scsi_cmnd *cmnd = task->uldd_task;
1651 		struct hisi_sas_slot *slot = task->lldd_task;
1652 		u16 tag = slot->idx;
1653 		int rc2;
1654 
1655 		int_to_scsilun(cmnd->device->lun, &lun);
1656 		tmf_task.tmf = TMF_ABORT_TASK;
1657 		tmf_task.tag_of_task_to_be_managed = tag;
1658 
1659 		rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1660 						  &tmf_task);
1661 
1662 		rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1663 						   HISI_SAS_INT_ABT_CMD, tag);
1664 		if (rc2 < 0) {
1665 			dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1666 			return TMF_RESP_FUNC_FAILED;
1667 		}
1668 
1669 		/*
1670 		 * If the TMF finds that the IO is not in the device and also
1671 		 * the internal abort does not succeed, then it is safe to
1672 		 * free the slot.
1673 		 * Note: if the internal abort succeeds then the slot
1674 		 * will have already been completed
1675 		 */
1676 		if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1677 			if (task->lldd_task)
1678 				hisi_sas_do_release_task(hisi_hba, task, slot);
1679 		}
1680 	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1681 		task->task_proto & SAS_PROTOCOL_STP) {
1682 		if (task->dev->dev_type == SAS_SATA_DEV) {
1683 			rc = hisi_sas_internal_task_abort(hisi_hba, device,
1684 							  HISI_SAS_INT_ABT_DEV,
1685 							  0);
1686 			if (rc < 0) {
1687 				dev_err(dev, "abort task: internal abort failed\n");
1688 				goto out;
1689 			}
1690 			hisi_sas_dereg_device(hisi_hba, device);
1691 			rc = hisi_sas_softreset_ata_disk(device);
1692 		}
1693 	} else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1694 		/* SMP */
1695 		struct hisi_sas_slot *slot = task->lldd_task;
1696 		u32 tag = slot->idx;
1697 		struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1698 
1699 		rc = hisi_sas_internal_task_abort(hisi_hba, device,
1700 						  HISI_SAS_INT_ABT_CMD, tag);
1701 		if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1702 					task->lldd_task) {
1703 			/*
1704 			 * sync irq to avoid free'ing task
1705 			 * before using task in IO completion
1706 			 */
1707 			synchronize_irq(cq->irq_no);
1708 			slot->task = NULL;
1709 		}
1710 	}
1711 
1712 out:
1713 	if (rc != TMF_RESP_FUNC_COMPLETE)
1714 		dev_notice(dev, "abort task: rc=%d\n", rc);
1715 	return rc;
1716 }
1717 
1718 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1719 {
1720 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1721 	struct device *dev = hisi_hba->dev;
1722 	struct hisi_sas_tmf_task tmf_task;
1723 	int rc;
1724 
1725 	rc = hisi_sas_internal_task_abort(hisi_hba, device,
1726 					  HISI_SAS_INT_ABT_DEV, 0);
1727 	if (rc < 0) {
1728 		dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1729 		return TMF_RESP_FUNC_FAILED;
1730 	}
1731 	hisi_sas_dereg_device(hisi_hba, device);
1732 
1733 	tmf_task.tmf = TMF_ABORT_TASK_SET;
1734 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1735 
1736 	if (rc == TMF_RESP_FUNC_COMPLETE)
1737 		hisi_sas_release_task(hisi_hba, device);
1738 
1739 	return rc;
1740 }
1741 
1742 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1743 {
1744 	struct hisi_sas_tmf_task tmf_task;
1745 	int rc;
1746 
1747 	tmf_task.tmf = TMF_CLEAR_ACA;
1748 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1749 
1750 	return rc;
1751 }
1752 
1753 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1754 {
1755 	struct sas_phy *local_phy = sas_get_local_phy(device);
1756 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1757 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1758 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1759 	DECLARE_COMPLETION_ONSTACK(phyreset);
1760 	int rc, reset_type;
1761 
1762 	if (!local_phy->enabled) {
1763 		sas_put_local_phy(local_phy);
1764 		return -ENODEV;
1765 	}
1766 
1767 	if (scsi_is_sas_phy_local(local_phy)) {
1768 		struct asd_sas_phy *sas_phy =
1769 			sas_ha->sas_phy[local_phy->number];
1770 		struct hisi_sas_phy *phy =
1771 			container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1772 		phy->in_reset = 1;
1773 		phy->reset_completion = &phyreset;
1774 	}
1775 
1776 	reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1777 		      !dev_is_sata(device)) ? true : false;
1778 
1779 	rc = sas_phy_reset(local_phy, reset_type);
1780 	sas_put_local_phy(local_phy);
1781 
1782 	if (scsi_is_sas_phy_local(local_phy)) {
1783 		struct asd_sas_phy *sas_phy =
1784 			sas_ha->sas_phy[local_phy->number];
1785 		struct hisi_sas_phy *phy =
1786 			container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1787 		int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1788 		unsigned long flags;
1789 
1790 		spin_lock_irqsave(&phy->lock, flags);
1791 		phy->reset_completion = NULL;
1792 		phy->in_reset = 0;
1793 		spin_unlock_irqrestore(&phy->lock, flags);
1794 
1795 		/* report PHY down if timed out */
1796 		if (!ret)
1797 			hisi_sas_phy_down(hisi_hba, sas_phy->id, 0, GFP_KERNEL);
1798 	} else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1799 		/*
1800 		 * If in init state, we rely on caller to wait for link to be
1801 		 * ready; otherwise, except phy reset is fail, delay.
1802 		 */
1803 		if (!rc)
1804 			msleep(2000);
1805 	}
1806 
1807 	return rc;
1808 }
1809 
1810 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1811 {
1812 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1813 	struct device *dev = hisi_hba->dev;
1814 	int rc;
1815 
1816 	rc = hisi_sas_internal_task_abort(hisi_hba, device,
1817 					  HISI_SAS_INT_ABT_DEV, 0);
1818 	if (rc < 0) {
1819 		dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1820 		return TMF_RESP_FUNC_FAILED;
1821 	}
1822 	hisi_sas_dereg_device(hisi_hba, device);
1823 
1824 	if (dev_is_sata(device)) {
1825 		rc = hisi_sas_softreset_ata_disk(device);
1826 		if (rc == TMF_RESP_FUNC_FAILED)
1827 			return TMF_RESP_FUNC_FAILED;
1828 	}
1829 
1830 	rc = hisi_sas_debug_I_T_nexus_reset(device);
1831 
1832 	if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1833 		hisi_sas_release_task(hisi_hba, device);
1834 
1835 	return rc;
1836 }
1837 
1838 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1839 {
1840 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1841 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1842 	struct device *dev = hisi_hba->dev;
1843 	int rc = TMF_RESP_FUNC_FAILED;
1844 
1845 	/* Clear internal IO and then lu reset */
1846 	rc = hisi_sas_internal_task_abort(hisi_hba, device,
1847 					  HISI_SAS_INT_ABT_DEV, 0);
1848 	if (rc < 0) {
1849 		dev_err(dev, "lu_reset: internal abort failed\n");
1850 		goto out;
1851 	}
1852 	hisi_sas_dereg_device(hisi_hba, device);
1853 
1854 	if (dev_is_sata(device)) {
1855 		struct sas_phy *phy;
1856 
1857 		phy = sas_get_local_phy(device);
1858 
1859 		rc = sas_phy_reset(phy, true);
1860 
1861 		if (rc == 0)
1862 			hisi_sas_release_task(hisi_hba, device);
1863 		sas_put_local_phy(phy);
1864 	} else {
1865 		struct hisi_sas_tmf_task tmf_task = { .tmf =  TMF_LU_RESET };
1866 
1867 		rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1868 		if (rc == TMF_RESP_FUNC_COMPLETE)
1869 			hisi_sas_release_task(hisi_hba, device);
1870 	}
1871 out:
1872 	if (rc != TMF_RESP_FUNC_COMPLETE)
1873 		dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1874 			     sas_dev->device_id, rc);
1875 	return rc;
1876 }
1877 
1878 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1879 {
1880 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1881 	struct device *dev = hisi_hba->dev;
1882 	HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1883 	int rc, i;
1884 
1885 	queue_work(hisi_hba->wq, &r.work);
1886 	wait_for_completion(r.completion);
1887 	if (!r.done)
1888 		return TMF_RESP_FUNC_FAILED;
1889 
1890 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1891 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1892 		struct domain_device *device = sas_dev->sas_device;
1893 
1894 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1895 		    dev_is_expander(device->dev_type))
1896 			continue;
1897 
1898 		rc = hisi_sas_debug_I_T_nexus_reset(device);
1899 		if (rc != TMF_RESP_FUNC_COMPLETE)
1900 			dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1901 				 sas_dev->device_id, rc);
1902 	}
1903 
1904 	hisi_sas_release_tasks(hisi_hba);
1905 
1906 	return TMF_RESP_FUNC_COMPLETE;
1907 }
1908 
1909 static int hisi_sas_query_task(struct sas_task *task)
1910 {
1911 	struct scsi_lun lun;
1912 	struct hisi_sas_tmf_task tmf_task;
1913 	int rc = TMF_RESP_FUNC_FAILED;
1914 
1915 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1916 		struct scsi_cmnd *cmnd = task->uldd_task;
1917 		struct domain_device *device = task->dev;
1918 		struct hisi_sas_slot *slot = task->lldd_task;
1919 		u32 tag = slot->idx;
1920 
1921 		int_to_scsilun(cmnd->device->lun, &lun);
1922 		tmf_task.tmf = TMF_QUERY_TASK;
1923 		tmf_task.tag_of_task_to_be_managed = tag;
1924 
1925 		rc = hisi_sas_debug_issue_ssp_tmf(device,
1926 						  lun.scsi_lun,
1927 						  &tmf_task);
1928 		switch (rc) {
1929 		/* The task is still in Lun, release it then */
1930 		case TMF_RESP_FUNC_SUCC:
1931 		/* The task is not in Lun or failed, reset the phy */
1932 		case TMF_RESP_FUNC_FAILED:
1933 		case TMF_RESP_FUNC_COMPLETE:
1934 			break;
1935 		default:
1936 			rc = TMF_RESP_FUNC_FAILED;
1937 			break;
1938 		}
1939 	}
1940 	return rc;
1941 }
1942 
1943 static int
1944 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1945 				  struct sas_task *task, int abort_flag,
1946 				  int task_tag, struct hisi_sas_dq *dq)
1947 {
1948 	struct domain_device *device = task->dev;
1949 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1950 	struct device *dev = hisi_hba->dev;
1951 	struct hisi_sas_port *port;
1952 	struct hisi_sas_slot *slot;
1953 	struct asd_sas_port *sas_port = device->port;
1954 	struct hisi_sas_cmd_hdr *cmd_hdr_base;
1955 	int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1956 	unsigned long flags;
1957 	int wr_q_index;
1958 
1959 	if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1960 		return -EINVAL;
1961 
1962 	if (!device->port)
1963 		return -1;
1964 
1965 	port = to_hisi_sas_port(sas_port);
1966 
1967 	/* simply get a slot and send abort command */
1968 	rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1969 	if (rc < 0)
1970 		goto err_out;
1971 
1972 	slot_idx = rc;
1973 	slot = &hisi_hba->slot_info[slot_idx];
1974 
1975 	spin_lock(&dq->lock);
1976 	wr_q_index = dq->wr_point;
1977 	dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1978 	list_add_tail(&slot->delivery, &dq->list);
1979 	spin_unlock(&dq->lock);
1980 	spin_lock(&sas_dev->lock);
1981 	list_add_tail(&slot->entry, &sas_dev->list);
1982 	spin_unlock(&sas_dev->lock);
1983 
1984 	dlvry_queue = dq->id;
1985 	dlvry_queue_slot = wr_q_index;
1986 
1987 	slot->device_id = sas_dev->device_id;
1988 	slot->n_elem = n_elem;
1989 	slot->dlvry_queue = dlvry_queue;
1990 	slot->dlvry_queue_slot = dlvry_queue_slot;
1991 	cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1992 	slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1993 	slot->task = task;
1994 	slot->port = port;
1995 	slot->is_internal = true;
1996 	task->lldd_task = slot;
1997 
1998 	memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1999 	memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
2000 	memset(hisi_sas_status_buf_addr_mem(slot), 0,
2001 	       sizeof(struct hisi_sas_err_record));
2002 
2003 	hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
2004 				      abort_flag, task_tag);
2005 
2006 	spin_lock_irqsave(&task->task_state_lock, flags);
2007 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
2008 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2009 	WRITE_ONCE(slot->ready, 1);
2010 	/* send abort command to the chip */
2011 	spin_lock(&dq->lock);
2012 	hisi_hba->hw->start_delivery(dq);
2013 	spin_unlock(&dq->lock);
2014 
2015 	return 0;
2016 
2017 err_out:
2018 	dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2019 
2020 	return rc;
2021 }
2022 
2023 /**
2024  * _hisi_sas_internal_task_abort -- execute an internal
2025  * abort command for single IO command or a device
2026  * @hisi_hba: host controller struct
2027  * @device: domain device
2028  * @abort_flag: mode of operation, device or single IO
2029  * @tag: tag of IO to be aborted (only relevant to single
2030  *       IO mode)
2031  * @dq: delivery queue for this internal abort command
2032  */
2033 static int
2034 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2035 			      struct domain_device *device, int abort_flag,
2036 			      int tag, struct hisi_sas_dq *dq)
2037 {
2038 	struct sas_task *task;
2039 	struct hisi_sas_device *sas_dev = device->lldd_dev;
2040 	struct device *dev = hisi_hba->dev;
2041 	int res;
2042 
2043 	/*
2044 	 * The interface is not realized means this HW don't support internal
2045 	 * abort, or don't need to do internal abort. Then here, we return
2046 	 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2047 	 * the internal abort has been executed and returned CQ.
2048 	 */
2049 	if (!hisi_hba->hw->prep_abort)
2050 		return TMF_RESP_FUNC_FAILED;
2051 
2052 	task = sas_alloc_slow_task(GFP_KERNEL);
2053 	if (!task)
2054 		return -ENOMEM;
2055 
2056 	task->dev = device;
2057 	task->task_proto = device->tproto;
2058 	task->task_done = hisi_sas_task_done;
2059 	task->slow_task->timer.function = hisi_sas_tmf_timedout;
2060 	task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2061 	add_timer(&task->slow_task->timer);
2062 
2063 	res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2064 						task, abort_flag, tag, dq);
2065 	if (res) {
2066 		del_timer(&task->slow_task->timer);
2067 		dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2068 			res);
2069 		goto exit;
2070 	}
2071 	wait_for_completion(&task->slow_task->completion);
2072 	res = TMF_RESP_FUNC_FAILED;
2073 
2074 	/* Internal abort timed out */
2075 	if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2076 		if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
2077 			queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2078 
2079 		if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2080 			struct hisi_sas_slot *slot = task->lldd_task;
2081 
2082 			if (slot) {
2083 				struct hisi_sas_cq *cq =
2084 					&hisi_hba->cq[slot->dlvry_queue];
2085 				/*
2086 				 * sync irq to avoid free'ing task
2087 				 * before using task in IO completion
2088 				 */
2089 				synchronize_irq(cq->irq_no);
2090 				slot->task = NULL;
2091 			}
2092 			dev_err(dev, "internal task abort: timeout and not done.\n");
2093 
2094 			res = -EIO;
2095 			goto exit;
2096 		} else
2097 			dev_err(dev, "internal task abort: timeout.\n");
2098 	}
2099 
2100 	if (task->task_status.resp == SAS_TASK_COMPLETE &&
2101 		task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2102 		res = TMF_RESP_FUNC_COMPLETE;
2103 		goto exit;
2104 	}
2105 
2106 	if (task->task_status.resp == SAS_TASK_COMPLETE &&
2107 		task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2108 		res = TMF_RESP_FUNC_SUCC;
2109 		goto exit;
2110 	}
2111 
2112 exit:
2113 	dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2114 		SAS_ADDR(device->sas_addr), task,
2115 		task->task_status.resp, /* 0 is complete, -1 is undelivered */
2116 		task->task_status.stat);
2117 	sas_free_task(task);
2118 
2119 	return res;
2120 }
2121 
2122 static int
2123 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2124 			     struct domain_device *device,
2125 			     int abort_flag, int tag)
2126 {
2127 	struct hisi_sas_slot *slot;
2128 	struct device *dev = hisi_hba->dev;
2129 	struct hisi_sas_dq *dq;
2130 	int i, rc;
2131 
2132 	switch (abort_flag) {
2133 	case HISI_SAS_INT_ABT_CMD:
2134 		slot = &hisi_hba->slot_info[tag];
2135 		dq = &hisi_hba->dq[slot->dlvry_queue];
2136 		return _hisi_sas_internal_task_abort(hisi_hba, device,
2137 						     abort_flag, tag, dq);
2138 	case HISI_SAS_INT_ABT_DEV:
2139 		for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2140 			struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2141 			const struct cpumask *mask = cq->irq_mask;
2142 
2143 			if (mask && !cpumask_intersects(cpu_online_mask, mask))
2144 				continue;
2145 			dq = &hisi_hba->dq[i];
2146 			rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2147 							   abort_flag, tag,
2148 							   dq);
2149 			if (rc)
2150 				return rc;
2151 		}
2152 		break;
2153 	default:
2154 		dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2155 			abort_flag);
2156 		return -EINVAL;
2157 	}
2158 
2159 	return 0;
2160 }
2161 
2162 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2163 {
2164 	hisi_sas_port_notify_formed(sas_phy);
2165 }
2166 
2167 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2168 			u8 reg_index, u8 reg_count, u8 *write_data)
2169 {
2170 	struct hisi_hba *hisi_hba = sha->lldd_ha;
2171 
2172 	if (!hisi_hba->hw->write_gpio)
2173 		return -EOPNOTSUPP;
2174 
2175 	return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2176 				reg_index, reg_count, write_data);
2177 }
2178 
2179 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2180 {
2181 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2182 	struct sas_phy *sphy = sas_phy->phy;
2183 	unsigned long flags;
2184 
2185 	phy->phy_attached = 0;
2186 	phy->phy_type = 0;
2187 	phy->port = NULL;
2188 
2189 	spin_lock_irqsave(&phy->lock, flags);
2190 	if (phy->enable)
2191 		sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2192 	else
2193 		sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2194 	spin_unlock_irqrestore(&phy->lock, flags);
2195 }
2196 
2197 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy,
2198 		       gfp_t gfp_flags)
2199 {
2200 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2201 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2202 	struct device *dev = hisi_hba->dev;
2203 
2204 	if (rdy) {
2205 		/* Phy down but ready */
2206 		hisi_sas_bytes_dmaed(hisi_hba, phy_no, gfp_flags);
2207 		hisi_sas_port_notify_formed(sas_phy);
2208 	} else {
2209 		struct hisi_sas_port *port  = phy->port;
2210 
2211 		if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2212 		    phy->in_reset) {
2213 			dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2214 			return;
2215 		}
2216 		/* Phy down and not ready */
2217 		sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL, gfp_flags);
2218 		sas_phy_disconnected(sas_phy);
2219 
2220 		if (port) {
2221 			if (phy->phy_type & PORT_TYPE_SAS) {
2222 				int port_id = port->id;
2223 
2224 				if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2225 								       port_id))
2226 					port->port_attached = 0;
2227 			} else if (phy->phy_type & PORT_TYPE_SATA)
2228 				port->port_attached = 0;
2229 		}
2230 		hisi_sas_phy_disconnected(phy);
2231 	}
2232 }
2233 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2234 
2235 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
2236 {
2237 	int i;
2238 
2239 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2240 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2241 
2242 		synchronize_irq(cq->irq_no);
2243 	}
2244 }
2245 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
2246 
2247 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2248 {
2249 	struct hisi_hba *hisi_hba = shost_priv(shost);
2250 
2251 	if (reset_type != SCSI_ADAPTER_RESET)
2252 		return -EOPNOTSUPP;
2253 
2254 	queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2255 
2256 	return 0;
2257 }
2258 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2259 
2260 struct scsi_transport_template *hisi_sas_stt;
2261 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2262 
2263 static struct sas_domain_function_template hisi_sas_transport_ops = {
2264 	.lldd_dev_found		= hisi_sas_dev_found,
2265 	.lldd_dev_gone		= hisi_sas_dev_gone,
2266 	.lldd_execute_task	= hisi_sas_queue_command,
2267 	.lldd_control_phy	= hisi_sas_control_phy,
2268 	.lldd_abort_task	= hisi_sas_abort_task,
2269 	.lldd_abort_task_set	= hisi_sas_abort_task_set,
2270 	.lldd_clear_aca		= hisi_sas_clear_aca,
2271 	.lldd_I_T_nexus_reset	= hisi_sas_I_T_nexus_reset,
2272 	.lldd_lu_reset		= hisi_sas_lu_reset,
2273 	.lldd_query_task	= hisi_sas_query_task,
2274 	.lldd_clear_nexus_ha	= hisi_sas_clear_nexus_ha,
2275 	.lldd_port_formed	= hisi_sas_port_formed,
2276 	.lldd_write_gpio	= hisi_sas_write_gpio,
2277 };
2278 
2279 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2280 {
2281 	int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2282 	struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2283 
2284 	for (i = 0; i < hisi_hba->queue_count; i++) {
2285 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2286 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2287 		struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2288 
2289 		s = sizeof(struct hisi_sas_cmd_hdr);
2290 		for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2291 			memset(&cmd_hdr[j], 0, s);
2292 
2293 		dq->wr_point = 0;
2294 
2295 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2296 		memset(hisi_hba->complete_hdr[i], 0, s);
2297 		cq->rd_point = 0;
2298 	}
2299 
2300 	s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2301 	memset(hisi_hba->initial_fis, 0, s);
2302 
2303 	s = max_command_entries * sizeof(struct hisi_sas_iost);
2304 	memset(hisi_hba->iost, 0, s);
2305 
2306 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2307 	memset(hisi_hba->breakpoint, 0, s);
2308 
2309 	s = sizeof(struct hisi_sas_sata_breakpoint);
2310 	for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2311 		memset(&sata_breakpoint[j], 0, s);
2312 }
2313 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2314 
2315 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2316 {
2317 	struct device *dev = hisi_hba->dev;
2318 	int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2319 	int max_command_entries_ru, sz_slot_buf_ru;
2320 	int blk_cnt, slots_per_blk;
2321 
2322 	sema_init(&hisi_hba->sem, 1);
2323 	spin_lock_init(&hisi_hba->lock);
2324 	for (i = 0; i < hisi_hba->n_phy; i++) {
2325 		hisi_sas_phy_init(hisi_hba, i);
2326 		hisi_hba->port[i].port_attached = 0;
2327 		hisi_hba->port[i].id = -1;
2328 	}
2329 
2330 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2331 		hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2332 		hisi_hba->devices[i].device_id = i;
2333 		hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2334 	}
2335 
2336 	for (i = 0; i < hisi_hba->queue_count; i++) {
2337 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2338 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2339 
2340 		/* Completion queue structure */
2341 		cq->id = i;
2342 		cq->hisi_hba = hisi_hba;
2343 
2344 		/* Delivery queue structure */
2345 		spin_lock_init(&dq->lock);
2346 		INIT_LIST_HEAD(&dq->list);
2347 		dq->id = i;
2348 		dq->hisi_hba = hisi_hba;
2349 
2350 		/* Delivery queue */
2351 		s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2352 		hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2353 						&hisi_hba->cmd_hdr_dma[i],
2354 						GFP_KERNEL);
2355 		if (!hisi_hba->cmd_hdr[i])
2356 			goto err_out;
2357 
2358 		/* Completion queue */
2359 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2360 		hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2361 						&hisi_hba->complete_hdr_dma[i],
2362 						GFP_KERNEL);
2363 		if (!hisi_hba->complete_hdr[i])
2364 			goto err_out;
2365 	}
2366 
2367 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2368 	hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2369 					     GFP_KERNEL);
2370 	if (!hisi_hba->itct)
2371 		goto err_out;
2372 
2373 	hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2374 					   sizeof(struct hisi_sas_slot),
2375 					   GFP_KERNEL);
2376 	if (!hisi_hba->slot_info)
2377 		goto err_out;
2378 
2379 	/* roundup to avoid overly large block size */
2380 	max_command_entries_ru = roundup(max_command_entries, 64);
2381 	if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2382 		sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2383 	else
2384 		sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2385 	sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2386 	s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2387 	blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2388 	slots_per_blk = s / sz_slot_buf_ru;
2389 
2390 	for (i = 0; i < blk_cnt; i++) {
2391 		int slot_index = i * slots_per_blk;
2392 		dma_addr_t buf_dma;
2393 		void *buf;
2394 
2395 		buf = dmam_alloc_coherent(dev, s, &buf_dma,
2396 					  GFP_KERNEL);
2397 		if (!buf)
2398 			goto err_out;
2399 
2400 		for (j = 0; j < slots_per_blk; j++, slot_index++) {
2401 			struct hisi_sas_slot *slot;
2402 
2403 			slot = &hisi_hba->slot_info[slot_index];
2404 			slot->buf = buf;
2405 			slot->buf_dma = buf_dma;
2406 			slot->idx = slot_index;
2407 
2408 			buf += sz_slot_buf_ru;
2409 			buf_dma += sz_slot_buf_ru;
2410 		}
2411 	}
2412 
2413 	s = max_command_entries * sizeof(struct hisi_sas_iost);
2414 	hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2415 					     GFP_KERNEL);
2416 	if (!hisi_hba->iost)
2417 		goto err_out;
2418 
2419 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2420 	hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2421 						   &hisi_hba->breakpoint_dma,
2422 						   GFP_KERNEL);
2423 	if (!hisi_hba->breakpoint)
2424 		goto err_out;
2425 
2426 	hisi_hba->slot_index_count = max_command_entries;
2427 	s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2428 	hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2429 	if (!hisi_hba->slot_index_tags)
2430 		goto err_out;
2431 
2432 	s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2433 	hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2434 						    &hisi_hba->initial_fis_dma,
2435 						    GFP_KERNEL);
2436 	if (!hisi_hba->initial_fis)
2437 		goto err_out;
2438 
2439 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2440 	hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2441 					&hisi_hba->sata_breakpoint_dma,
2442 					GFP_KERNEL);
2443 	if (!hisi_hba->sata_breakpoint)
2444 		goto err_out;
2445 
2446 	hisi_sas_slot_index_init(hisi_hba);
2447 	hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2448 
2449 	hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2450 	if (!hisi_hba->wq) {
2451 		dev_err(dev, "sas_alloc: failed to create workqueue\n");
2452 		goto err_out;
2453 	}
2454 
2455 	return 0;
2456 err_out:
2457 	return -ENOMEM;
2458 }
2459 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2460 
2461 void hisi_sas_free(struct hisi_hba *hisi_hba)
2462 {
2463 	int i;
2464 
2465 	for (i = 0; i < hisi_hba->n_phy; i++) {
2466 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2467 
2468 		del_timer_sync(&phy->timer);
2469 	}
2470 
2471 	if (hisi_hba->wq)
2472 		destroy_workqueue(hisi_hba->wq);
2473 }
2474 EXPORT_SYMBOL_GPL(hisi_sas_free);
2475 
2476 void hisi_sas_rst_work_handler(struct work_struct *work)
2477 {
2478 	struct hisi_hba *hisi_hba =
2479 		container_of(work, struct hisi_hba, rst_work);
2480 
2481 	if (hisi_sas_controller_prereset(hisi_hba))
2482 		return;
2483 
2484 	hisi_sas_controller_reset(hisi_hba);
2485 }
2486 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2487 
2488 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2489 {
2490 	struct hisi_sas_rst *rst =
2491 		container_of(work, struct hisi_sas_rst, work);
2492 
2493 	if (hisi_sas_controller_prereset(rst->hisi_hba))
2494 		goto rst_complete;
2495 
2496 	if (!hisi_sas_controller_reset(rst->hisi_hba))
2497 		rst->done = true;
2498 rst_complete:
2499 	complete(rst->completion);
2500 }
2501 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2502 
2503 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2504 {
2505 	struct device *dev = hisi_hba->dev;
2506 	struct platform_device *pdev = hisi_hba->platform_dev;
2507 	struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2508 	struct clk *refclk;
2509 
2510 	if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2511 					  SAS_ADDR_SIZE)) {
2512 		dev_err(dev, "could not get property sas-addr\n");
2513 		return -ENOENT;
2514 	}
2515 
2516 	if (np) {
2517 		/*
2518 		 * These properties are only required for platform device-based
2519 		 * controller with DT firmware.
2520 		 */
2521 		hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2522 					"hisilicon,sas-syscon");
2523 		if (IS_ERR(hisi_hba->ctrl)) {
2524 			dev_err(dev, "could not get syscon\n");
2525 			return -ENOENT;
2526 		}
2527 
2528 		if (device_property_read_u32(dev, "ctrl-reset-reg",
2529 					     &hisi_hba->ctrl_reset_reg)) {
2530 			dev_err(dev, "could not get property ctrl-reset-reg\n");
2531 			return -ENOENT;
2532 		}
2533 
2534 		if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2535 					     &hisi_hba->ctrl_reset_sts_reg)) {
2536 			dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2537 			return -ENOENT;
2538 		}
2539 
2540 		if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2541 					     &hisi_hba->ctrl_clock_ena_reg)) {
2542 			dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2543 			return -ENOENT;
2544 		}
2545 	}
2546 
2547 	refclk = devm_clk_get(dev, NULL);
2548 	if (IS_ERR(refclk))
2549 		dev_dbg(dev, "no ref clk property\n");
2550 	else
2551 		hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2552 
2553 	if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2554 		dev_err(dev, "could not get property phy-count\n");
2555 		return -ENOENT;
2556 	}
2557 
2558 	if (device_property_read_u32(dev, "queue-count",
2559 				     &hisi_hba->queue_count)) {
2560 		dev_err(dev, "could not get property queue-count\n");
2561 		return -ENOENT;
2562 	}
2563 
2564 	return 0;
2565 }
2566 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2567 
2568 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2569 					      const struct hisi_sas_hw *hw)
2570 {
2571 	struct resource *res;
2572 	struct Scsi_Host *shost;
2573 	struct hisi_hba *hisi_hba;
2574 	struct device *dev = &pdev->dev;
2575 	int error;
2576 
2577 	shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2578 	if (!shost) {
2579 		dev_err(dev, "scsi host alloc failed\n");
2580 		return NULL;
2581 	}
2582 	hisi_hba = shost_priv(shost);
2583 
2584 	INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2585 	hisi_hba->hw = hw;
2586 	hisi_hba->dev = dev;
2587 	hisi_hba->platform_dev = pdev;
2588 	hisi_hba->shost = shost;
2589 	SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2590 
2591 	timer_setup(&hisi_hba->timer, NULL, 0);
2592 
2593 	if (hisi_sas_get_fw_info(hisi_hba) < 0)
2594 		goto err_out;
2595 
2596 	error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2597 	if (error)
2598 		error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2599 
2600 	if (error) {
2601 		dev_err(dev, "No usable DMA addressing method\n");
2602 		goto err_out;
2603 	}
2604 
2605 	hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2606 	if (IS_ERR(hisi_hba->regs))
2607 		goto err_out;
2608 
2609 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2610 	if (res) {
2611 		hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2612 		if (IS_ERR(hisi_hba->sgpio_regs))
2613 			goto err_out;
2614 	}
2615 
2616 	if (hisi_sas_alloc(hisi_hba)) {
2617 		hisi_sas_free(hisi_hba);
2618 		goto err_out;
2619 	}
2620 
2621 	return shost;
2622 err_out:
2623 	scsi_host_put(shost);
2624 	dev_err(dev, "shost alloc failed\n");
2625 	return NULL;
2626 }
2627 
2628 static int hisi_sas_interrupt_preinit(struct hisi_hba *hisi_hba)
2629 {
2630 	if (hisi_hba->hw->interrupt_preinit)
2631 		return hisi_hba->hw->interrupt_preinit(hisi_hba);
2632 	return 0;
2633 }
2634 
2635 int hisi_sas_probe(struct platform_device *pdev,
2636 		   const struct hisi_sas_hw *hw)
2637 {
2638 	struct Scsi_Host *shost;
2639 	struct hisi_hba *hisi_hba;
2640 	struct device *dev = &pdev->dev;
2641 	struct asd_sas_phy **arr_phy;
2642 	struct asd_sas_port **arr_port;
2643 	struct sas_ha_struct *sha;
2644 	int rc, phy_nr, port_nr, i;
2645 
2646 	shost = hisi_sas_shost_alloc(pdev, hw);
2647 	if (!shost)
2648 		return -ENOMEM;
2649 
2650 	sha = SHOST_TO_SAS_HA(shost);
2651 	hisi_hba = shost_priv(shost);
2652 	platform_set_drvdata(pdev, sha);
2653 
2654 	phy_nr = port_nr = hisi_hba->n_phy;
2655 
2656 	arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2657 	arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2658 	if (!arr_phy || !arr_port) {
2659 		rc = -ENOMEM;
2660 		goto err_out_ha;
2661 	}
2662 
2663 	sha->sas_phy = arr_phy;
2664 	sha->sas_port = arr_port;
2665 	sha->lldd_ha = hisi_hba;
2666 
2667 	shost->transportt = hisi_sas_stt;
2668 	shost->max_id = HISI_SAS_MAX_DEVICES;
2669 	shost->max_lun = ~0;
2670 	shost->max_channel = 1;
2671 	shost->max_cmd_len = 16;
2672 	if (hisi_hba->hw->slot_index_alloc) {
2673 		shost->can_queue = HISI_SAS_MAX_COMMANDS;
2674 		shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2675 	} else {
2676 		shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2677 		shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2678 	}
2679 
2680 	sha->sas_ha_name = DRV_NAME;
2681 	sha->dev = hisi_hba->dev;
2682 	sha->lldd_module = THIS_MODULE;
2683 	sha->sas_addr = &hisi_hba->sas_addr[0];
2684 	sha->num_phys = hisi_hba->n_phy;
2685 	sha->core.shost = hisi_hba->shost;
2686 
2687 	for (i = 0; i < hisi_hba->n_phy; i++) {
2688 		sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2689 		sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2690 	}
2691 
2692 	rc = hisi_sas_interrupt_preinit(hisi_hba);
2693 	if (rc)
2694 		goto err_out_ha;
2695 
2696 	rc = scsi_add_host(shost, &pdev->dev);
2697 	if (rc)
2698 		goto err_out_ha;
2699 
2700 	rc = sas_register_ha(sha);
2701 	if (rc)
2702 		goto err_out_register_ha;
2703 
2704 	rc = hisi_hba->hw->hw_init(hisi_hba);
2705 	if (rc)
2706 		goto err_out_hw_init;
2707 
2708 	scsi_scan_host(shost);
2709 
2710 	return 0;
2711 
2712 err_out_hw_init:
2713 	sas_unregister_ha(sha);
2714 err_out_register_ha:
2715 	scsi_remove_host(shost);
2716 err_out_ha:
2717 	hisi_sas_free(hisi_hba);
2718 	scsi_host_put(shost);
2719 	return rc;
2720 }
2721 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2722 
2723 int hisi_sas_remove(struct platform_device *pdev)
2724 {
2725 	struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2726 	struct hisi_hba *hisi_hba = sha->lldd_ha;
2727 	struct Scsi_Host *shost = sha->core.shost;
2728 
2729 	if (timer_pending(&hisi_hba->timer))
2730 		del_timer(&hisi_hba->timer);
2731 
2732 	sas_unregister_ha(sha);
2733 	sas_remove_host(sha->core.shost);
2734 
2735 	hisi_sas_free(hisi_hba);
2736 	scsi_host_put(shost);
2737 	return 0;
2738 }
2739 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2740 
2741 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE)
2742 #define DEBUGFS_ENABLE_DEFAULT  "enabled"
2743 bool hisi_sas_debugfs_enable = true;
2744 u32 hisi_sas_debugfs_dump_count = 50;
2745 #else
2746 #define DEBUGFS_ENABLE_DEFAULT "disabled"
2747 bool hisi_sas_debugfs_enable;
2748 u32 hisi_sas_debugfs_dump_count = 1;
2749 #endif
2750 
2751 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2752 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2753 MODULE_PARM_DESC(hisi_sas_debugfs_enable,
2754 		 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT")");
2755 
2756 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
2757 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
2758 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
2759 
2760 struct dentry *hisi_sas_debugfs_dir;
2761 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir);
2762 
2763 static __init int hisi_sas_init(void)
2764 {
2765 	hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2766 	if (!hisi_sas_stt)
2767 		return -ENOMEM;
2768 
2769 	if (hisi_sas_debugfs_enable) {
2770 		hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2771 		if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
2772 			pr_info("hisi_sas: Limiting debugfs dump count\n");
2773 			hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
2774 		}
2775 	}
2776 
2777 	return 0;
2778 }
2779 
2780 static __exit void hisi_sas_exit(void)
2781 {
2782 	sas_release_transport(hisi_sas_stt);
2783 
2784 	debugfs_remove(hisi_sas_debugfs_dir);
2785 }
2786 
2787 module_init(hisi_sas_init);
2788 module_exit(hisi_sas_exit);
2789 
2790 MODULE_LICENSE("GPL");
2791 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2792 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2793 MODULE_ALIAS("platform:" DRV_NAME);
2794