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 	unsigned long flags;
167 
168 	if (hisi_hba->hw->slot_index_alloc ||
169 	    slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
170 		spin_lock_irqsave(&hisi_hba->lock, flags);
171 		hisi_sas_slot_index_clear(hisi_hba, slot_idx);
172 		spin_unlock_irqrestore(&hisi_hba->lock, flags);
173 	}
174 }
175 
176 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
177 {
178 	void *bitmap = hisi_hba->slot_index_tags;
179 
180 	set_bit(slot_idx, bitmap);
181 }
182 
183 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
184 				     struct scsi_cmnd *scsi_cmnd)
185 {
186 	int index;
187 	void *bitmap = hisi_hba->slot_index_tags;
188 	unsigned long flags;
189 
190 	if (scsi_cmnd)
191 		return scsi_cmnd->request->tag;
192 
193 	spin_lock_irqsave(&hisi_hba->lock, flags);
194 	index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
195 				   hisi_hba->last_slot_index + 1);
196 	if (index >= hisi_hba->slot_index_count) {
197 		index = find_next_zero_bit(bitmap,
198 				hisi_hba->slot_index_count,
199 				HISI_SAS_UNRESERVED_IPTT);
200 		if (index >= hisi_hba->slot_index_count) {
201 			spin_unlock_irqrestore(&hisi_hba->lock, flags);
202 			return -SAS_QUEUE_FULL;
203 		}
204 	}
205 	hisi_sas_slot_index_set(hisi_hba, index);
206 	hisi_hba->last_slot_index = index;
207 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
208 
209 	return index;
210 }
211 
212 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
213 {
214 	int i;
215 
216 	for (i = 0; i < hisi_hba->slot_index_count; ++i)
217 		hisi_sas_slot_index_clear(hisi_hba, i);
218 }
219 
220 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
221 			     struct hisi_sas_slot *slot)
222 {
223 	unsigned long flags;
224 	int device_id = slot->device_id;
225 	struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
226 
227 	if (task) {
228 		struct device *dev = hisi_hba->dev;
229 
230 		if (!task->lldd_task)
231 			return;
232 
233 		task->lldd_task = NULL;
234 
235 		if (!sas_protocol_ata(task->task_proto)) {
236 			struct sas_ssp_task *ssp_task = &task->ssp_task;
237 			struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
238 
239 			if (slot->n_elem)
240 				dma_unmap_sg(dev, task->scatter,
241 					     task->num_scatter,
242 					     task->data_dir);
243 			if (slot->n_elem_dif)
244 				dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
245 					     scsi_prot_sg_count(scsi_cmnd),
246 					     task->data_dir);
247 		}
248 	}
249 
250 	spin_lock_irqsave(&sas_dev->lock, flags);
251 	list_del_init(&slot->entry);
252 	spin_unlock_irqrestore(&sas_dev->lock, flags);
253 
254 	memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
255 
256 	hisi_sas_slot_index_free(hisi_hba, slot->idx);
257 }
258 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
259 
260 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
261 				  struct hisi_sas_slot *slot)
262 {
263 	hisi_hba->hw->prep_smp(hisi_hba, slot);
264 }
265 
266 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
267 				  struct hisi_sas_slot *slot)
268 {
269 	hisi_hba->hw->prep_ssp(hisi_hba, slot);
270 }
271 
272 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
273 				  struct hisi_sas_slot *slot)
274 {
275 	hisi_hba->hw->prep_stp(hisi_hba, slot);
276 }
277 
278 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
279 		struct hisi_sas_slot *slot,
280 		int device_id, int abort_flag, int tag_to_abort)
281 {
282 	hisi_hba->hw->prep_abort(hisi_hba, slot,
283 			device_id, abort_flag, tag_to_abort);
284 }
285 
286 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
287 			       struct sas_task *task, int n_elem,
288 			       int n_elem_req)
289 {
290 	struct device *dev = hisi_hba->dev;
291 
292 	if (!sas_protocol_ata(task->task_proto)) {
293 		if (task->num_scatter) {
294 			if (n_elem)
295 				dma_unmap_sg(dev, task->scatter,
296 					     task->num_scatter,
297 					     task->data_dir);
298 		} else if (task->task_proto & SAS_PROTOCOL_SMP) {
299 			if (n_elem_req)
300 				dma_unmap_sg(dev, &task->smp_task.smp_req,
301 					     1, DMA_TO_DEVICE);
302 		}
303 	}
304 }
305 
306 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
307 			    struct sas_task *task, int *n_elem,
308 			    int *n_elem_req)
309 {
310 	struct device *dev = hisi_hba->dev;
311 	int rc;
312 
313 	if (sas_protocol_ata(task->task_proto)) {
314 		*n_elem = task->num_scatter;
315 	} else {
316 		unsigned int req_len;
317 
318 		if (task->num_scatter) {
319 			*n_elem = dma_map_sg(dev, task->scatter,
320 					     task->num_scatter, task->data_dir);
321 			if (!*n_elem) {
322 				rc = -ENOMEM;
323 				goto prep_out;
324 			}
325 		} else if (task->task_proto & SAS_PROTOCOL_SMP) {
326 			*n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
327 						 1, DMA_TO_DEVICE);
328 			if (!*n_elem_req) {
329 				rc = -ENOMEM;
330 				goto prep_out;
331 			}
332 			req_len = sg_dma_len(&task->smp_task.smp_req);
333 			if (req_len & 0x3) {
334 				rc = -EINVAL;
335 				goto err_out_dma_unmap;
336 			}
337 		}
338 	}
339 
340 	if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
341 		dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
342 			*n_elem);
343 		rc = -EINVAL;
344 		goto err_out_dma_unmap;
345 	}
346 	return 0;
347 
348 err_out_dma_unmap:
349 	/* It would be better to call dma_unmap_sg() here, but it's messy */
350 	hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
351 			   *n_elem_req);
352 prep_out:
353 	return rc;
354 }
355 
356 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
357 				   struct sas_task *task, int n_elem_dif)
358 {
359 	struct device *dev = hisi_hba->dev;
360 
361 	if (n_elem_dif) {
362 		struct sas_ssp_task *ssp_task = &task->ssp_task;
363 		struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
364 
365 		dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
366 			     scsi_prot_sg_count(scsi_cmnd),
367 			     task->data_dir);
368 	}
369 }
370 
371 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
372 				int *n_elem_dif, struct sas_task *task)
373 {
374 	struct device *dev = hisi_hba->dev;
375 	struct sas_ssp_task *ssp_task;
376 	struct scsi_cmnd *scsi_cmnd;
377 	int rc;
378 
379 	if (task->num_scatter) {
380 		ssp_task = &task->ssp_task;
381 		scsi_cmnd = ssp_task->cmd;
382 
383 		if (scsi_prot_sg_count(scsi_cmnd)) {
384 			*n_elem_dif = dma_map_sg(dev,
385 						 scsi_prot_sglist(scsi_cmnd),
386 						 scsi_prot_sg_count(scsi_cmnd),
387 						 task->data_dir);
388 
389 			if (!*n_elem_dif)
390 				return -ENOMEM;
391 
392 			if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
393 				dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
394 					*n_elem_dif);
395 				rc = -EINVAL;
396 				goto err_out_dif_dma_unmap;
397 			}
398 		}
399 	}
400 
401 	return 0;
402 
403 err_out_dif_dma_unmap:
404 	dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
405 		     scsi_prot_sg_count(scsi_cmnd), task->data_dir);
406 	return rc;
407 }
408 
409 static int hisi_sas_task_prep(struct sas_task *task,
410 			      struct hisi_sas_dq **dq_pointer,
411 			      bool is_tmf, struct hisi_sas_tmf_task *tmf,
412 			      int *pass)
413 {
414 	struct domain_device *device = task->dev;
415 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
416 	struct hisi_sas_device *sas_dev = device->lldd_dev;
417 	struct hisi_sas_port *port;
418 	struct hisi_sas_slot *slot;
419 	struct hisi_sas_cmd_hdr	*cmd_hdr_base;
420 	struct asd_sas_port *sas_port = device->port;
421 	struct device *dev = hisi_hba->dev;
422 	int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
423 	int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
424 	struct hisi_sas_dq *dq;
425 	unsigned long flags;
426 	int wr_q_index;
427 
428 	if (DEV_IS_GONE(sas_dev)) {
429 		if (sas_dev)
430 			dev_info(dev, "task prep: device %d not ready\n",
431 				 sas_dev->device_id);
432 		else
433 			dev_info(dev, "task prep: device %016llx not ready\n",
434 				 SAS_ADDR(device->sas_addr));
435 
436 		return -ECOMM;
437 	}
438 
439 	if (hisi_hba->reply_map) {
440 		int cpu = raw_smp_processor_id();
441 		unsigned int dq_index = hisi_hba->reply_map[cpu];
442 
443 		*dq_pointer = dq = &hisi_hba->dq[dq_index];
444 	} else {
445 		*dq_pointer = dq = sas_dev->dq;
446 	}
447 
448 	port = to_hisi_sas_port(sas_port);
449 	if (port && !port->port_attached) {
450 		dev_info(dev, "task prep: %s port%d not attach device\n",
451 			 (dev_is_sata(device)) ?
452 			 "SATA/STP" : "SAS",
453 			 device->port->id);
454 
455 		return -ECOMM;
456 	}
457 
458 	rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
459 			      &n_elem_req);
460 	if (rc < 0)
461 		goto prep_out;
462 
463 	if (!sas_protocol_ata(task->task_proto)) {
464 		rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
465 		if (rc < 0)
466 			goto err_out_dma_unmap;
467 	}
468 
469 	if (hisi_hba->hw->slot_index_alloc)
470 		rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
471 	else {
472 		struct scsi_cmnd *scsi_cmnd = NULL;
473 
474 		if (task->uldd_task) {
475 			struct ata_queued_cmd *qc;
476 
477 			if (dev_is_sata(device)) {
478 				qc = task->uldd_task;
479 				scsi_cmnd = qc->scsicmd;
480 			} else {
481 				scsi_cmnd = task->uldd_task;
482 			}
483 		}
484 		rc  = hisi_sas_slot_index_alloc(hisi_hba, scsi_cmnd);
485 	}
486 	if (rc < 0)
487 		goto err_out_dif_dma_unmap;
488 
489 	slot_idx = rc;
490 	slot = &hisi_hba->slot_info[slot_idx];
491 
492 	spin_lock_irqsave(&dq->lock, flags);
493 	wr_q_index = dq->wr_point;
494 	dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
495 	list_add_tail(&slot->delivery, &dq->list);
496 	spin_unlock_irqrestore(&dq->lock, flags);
497 	spin_lock_irqsave(&sas_dev->lock, flags);
498 	list_add_tail(&slot->entry, &sas_dev->list);
499 	spin_unlock_irqrestore(&sas_dev->lock, flags);
500 
501 	dlvry_queue = dq->id;
502 	dlvry_queue_slot = wr_q_index;
503 
504 	slot->device_id = sas_dev->device_id;
505 	slot->n_elem = n_elem;
506 	slot->n_elem_dif = n_elem_dif;
507 	slot->dlvry_queue = dlvry_queue;
508 	slot->dlvry_queue_slot = dlvry_queue_slot;
509 	cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
510 	slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
511 	slot->task = task;
512 	slot->port = port;
513 	slot->tmf = tmf;
514 	slot->is_internal = is_tmf;
515 	task->lldd_task = slot;
516 
517 	memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
518 	memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
519 	memset(hisi_sas_status_buf_addr_mem(slot), 0,
520 	       sizeof(struct hisi_sas_err_record));
521 
522 	switch (task->task_proto) {
523 	case SAS_PROTOCOL_SMP:
524 		hisi_sas_task_prep_smp(hisi_hba, slot);
525 		break;
526 	case SAS_PROTOCOL_SSP:
527 		hisi_sas_task_prep_ssp(hisi_hba, slot);
528 		break;
529 	case SAS_PROTOCOL_SATA:
530 	case SAS_PROTOCOL_STP:
531 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
532 		hisi_sas_task_prep_ata(hisi_hba, slot);
533 		break;
534 	default:
535 		dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
536 			task->task_proto);
537 		break;
538 	}
539 
540 	spin_lock_irqsave(&task->task_state_lock, flags);
541 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
542 	spin_unlock_irqrestore(&task->task_state_lock, flags);
543 
544 	++(*pass);
545 	WRITE_ONCE(slot->ready, 1);
546 
547 	return 0;
548 
549 err_out_dif_dma_unmap:
550 	if (!sas_protocol_ata(task->task_proto))
551 		hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
552 err_out_dma_unmap:
553 	hisi_sas_dma_unmap(hisi_hba, task, n_elem,
554 			   n_elem_req);
555 prep_out:
556 	dev_err(dev, "task prep: failed[%d]!\n", rc);
557 	return rc;
558 }
559 
560 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
561 			      bool is_tmf, struct hisi_sas_tmf_task *tmf)
562 {
563 	u32 rc;
564 	u32 pass = 0;
565 	unsigned long flags;
566 	struct hisi_hba *hisi_hba;
567 	struct device *dev;
568 	struct domain_device *device = task->dev;
569 	struct asd_sas_port *sas_port = device->port;
570 	struct hisi_sas_dq *dq = NULL;
571 
572 	if (!sas_port) {
573 		struct task_status_struct *ts = &task->task_status;
574 
575 		ts->resp = SAS_TASK_UNDELIVERED;
576 		ts->stat = SAS_PHY_DOWN;
577 		/*
578 		 * libsas will use dev->port, should
579 		 * not call task_done for sata
580 		 */
581 		if (device->dev_type != SAS_SATA_DEV)
582 			task->task_done(task);
583 		return -ECOMM;
584 	}
585 
586 	hisi_hba = dev_to_hisi_hba(device);
587 	dev = hisi_hba->dev;
588 
589 	if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
590 		/*
591 		 * For IOs from upper layer, it may already disable preempt
592 		 * in the IO path, if disable preempt again in down(),
593 		 * function schedule() will report schedule_bug(), so check
594 		 * preemptible() before goto down().
595 		 */
596 		if (!preemptible())
597 			return -EINVAL;
598 
599 		down(&hisi_hba->sem);
600 		up(&hisi_hba->sem);
601 	}
602 
603 	/* protect task_prep and start_delivery sequence */
604 	rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
605 	if (rc)
606 		dev_err(dev, "task exec: failed[%d]!\n", rc);
607 
608 	if (likely(pass)) {
609 		spin_lock_irqsave(&dq->lock, flags);
610 		hisi_hba->hw->start_delivery(dq);
611 		spin_unlock_irqrestore(&dq->lock, flags);
612 	}
613 
614 	return rc;
615 }
616 
617 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
618 {
619 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
620 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
621 	struct sas_ha_struct *sas_ha;
622 
623 	if (!phy->phy_attached)
624 		return;
625 
626 	sas_ha = &hisi_hba->sha;
627 	sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
628 
629 	if (sas_phy->phy) {
630 		struct sas_phy *sphy = sas_phy->phy;
631 
632 		sphy->negotiated_linkrate = sas_phy->linkrate;
633 		sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
634 		sphy->maximum_linkrate_hw =
635 			hisi_hba->hw->phy_get_max_linkrate();
636 		if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
637 			sphy->minimum_linkrate = phy->minimum_linkrate;
638 
639 		if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
640 			sphy->maximum_linkrate = phy->maximum_linkrate;
641 	}
642 
643 	if (phy->phy_type & PORT_TYPE_SAS) {
644 		struct sas_identify_frame *id;
645 
646 		id = (struct sas_identify_frame *)phy->frame_rcvd;
647 		id->dev_type = phy->identify.device_type;
648 		id->initiator_bits = SAS_PROTOCOL_ALL;
649 		id->target_bits = phy->identify.target_port_protocols;
650 	} else if (phy->phy_type & PORT_TYPE_SATA) {
651 		/* Nothing */
652 	}
653 
654 	sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
655 	sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
656 }
657 
658 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
659 {
660 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
661 	struct hisi_sas_device *sas_dev = NULL;
662 	unsigned long flags;
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_irqsave(&hisi_hba->lock, flags);
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_irqrestore(&hisi_hba->lock, flags);
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);
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 	if (!timer_pending(&phy->timer)) {
908 		dev_dbg(dev, "phy%d OOB ready\n", phy_no);
909 		phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
910 		add_timer(&phy->timer);
911 	}
912 }
913 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
914 
915 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
916 {
917 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
918 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
919 	int i;
920 
921 	phy->hisi_hba = hisi_hba;
922 	phy->port = NULL;
923 	phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
924 	phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
925 	sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
926 	sas_phy->class = SAS;
927 	sas_phy->iproto = SAS_PROTOCOL_ALL;
928 	sas_phy->tproto = 0;
929 	sas_phy->type = PHY_TYPE_PHYSICAL;
930 	sas_phy->role = PHY_ROLE_INITIATOR;
931 	sas_phy->oob_mode = OOB_NOT_CONNECTED;
932 	sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
933 	sas_phy->id = phy_no;
934 	sas_phy->sas_addr = &hisi_hba->sas_addr[0];
935 	sas_phy->frame_rcvd = &phy->frame_rcvd[0];
936 	sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
937 	sas_phy->lldd_phy = phy;
938 
939 	for (i = 0; i < HISI_PHYES_NUM; i++)
940 		INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
941 
942 	spin_lock_init(&phy->lock);
943 
944 	timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
945 }
946 
947 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
948 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
949 {
950 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
951 	struct asd_sas_phy *aphy = &phy->sas_phy;
952 	struct sas_phy *sphy = aphy->phy;
953 	unsigned long flags;
954 
955 	spin_lock_irqsave(&phy->lock, flags);
956 
957 	if (enable) {
958 		/* We may have been enabled already; if so, don't touch */
959 		if (!phy->enable)
960 			sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
961 		hisi_hba->hw->phy_start(hisi_hba, phy_no);
962 	} else {
963 		sphy->negotiated_linkrate = SAS_PHY_DISABLED;
964 		hisi_hba->hw->phy_disable(hisi_hba, phy_no);
965 	}
966 	phy->enable = enable;
967 	spin_unlock_irqrestore(&phy->lock, flags);
968 }
969 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
970 
971 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
972 {
973 	struct sas_ha_struct *sas_ha = sas_phy->ha;
974 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
975 	struct hisi_sas_phy *phy = sas_phy->lldd_phy;
976 	struct asd_sas_port *sas_port = sas_phy->port;
977 	struct hisi_sas_port *port;
978 	unsigned long flags;
979 
980 	if (!sas_port)
981 		return;
982 
983 	port = to_hisi_sas_port(sas_port);
984 	spin_lock_irqsave(&hisi_hba->lock, flags);
985 	port->port_attached = 1;
986 	port->id = phy->port_id;
987 	phy->port = port;
988 	sas_port->lldd_port = port;
989 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
990 }
991 
992 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
993 				     struct hisi_sas_slot *slot)
994 {
995 	if (task) {
996 		unsigned long flags;
997 		struct task_status_struct *ts;
998 
999 		ts = &task->task_status;
1000 
1001 		ts->resp = SAS_TASK_COMPLETE;
1002 		ts->stat = SAS_ABORTED_TASK;
1003 		spin_lock_irqsave(&task->task_state_lock, flags);
1004 		task->task_state_flags &=
1005 			~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1006 		if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1007 			task->task_state_flags |= SAS_TASK_STATE_DONE;
1008 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1009 	}
1010 
1011 	hisi_sas_slot_task_free(hisi_hba, task, slot);
1012 }
1013 
1014 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1015 			struct domain_device *device)
1016 {
1017 	struct hisi_sas_slot *slot, *slot2;
1018 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1019 
1020 	list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1021 		hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1022 }
1023 
1024 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1025 {
1026 	struct hisi_sas_device *sas_dev;
1027 	struct domain_device *device;
1028 	int i;
1029 
1030 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1031 		sas_dev = &hisi_hba->devices[i];
1032 		device = sas_dev->sas_device;
1033 
1034 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1035 		    !device)
1036 			continue;
1037 
1038 		hisi_sas_release_task(hisi_hba, device);
1039 	}
1040 }
1041 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1042 
1043 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1044 				struct domain_device *device)
1045 {
1046 	if (hisi_hba->hw->dereg_device)
1047 		hisi_hba->hw->dereg_device(hisi_hba, device);
1048 }
1049 
1050 static void hisi_sas_dev_gone(struct domain_device *device)
1051 {
1052 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1053 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1054 	struct device *dev = hisi_hba->dev;
1055 	int ret = 0;
1056 
1057 	dev_info(dev, "dev[%d:%x] is gone\n",
1058 		 sas_dev->device_id, sas_dev->dev_type);
1059 
1060 	down(&hisi_hba->sem);
1061 	if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1062 		hisi_sas_internal_task_abort(hisi_hba, device,
1063 					     HISI_SAS_INT_ABT_DEV, 0);
1064 
1065 		hisi_sas_dereg_device(hisi_hba, device);
1066 
1067 		ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1068 		device->lldd_dev = NULL;
1069 	}
1070 
1071 	if (hisi_hba->hw->free_device)
1072 		hisi_hba->hw->free_device(sas_dev);
1073 
1074 	/* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1075 	if (!ret)
1076 		sas_dev->dev_type = SAS_PHY_UNUSED;
1077 	sas_dev->sas_device = NULL;
1078 	up(&hisi_hba->sem);
1079 }
1080 
1081 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1082 {
1083 	return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1084 }
1085 
1086 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1087 			struct sas_phy_linkrates *r)
1088 {
1089 	struct sas_phy_linkrates _r;
1090 
1091 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1092 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1093 	enum sas_linkrate min, max;
1094 
1095 	if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1096 		return -EINVAL;
1097 
1098 	if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1099 		max = sas_phy->phy->maximum_linkrate;
1100 		min = r->minimum_linkrate;
1101 	} else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1102 		max = r->maximum_linkrate;
1103 		min = sas_phy->phy->minimum_linkrate;
1104 	} else
1105 		return -EINVAL;
1106 
1107 	_r.maximum_linkrate = max;
1108 	_r.minimum_linkrate = min;
1109 
1110 	sas_phy->phy->maximum_linkrate = max;
1111 	sas_phy->phy->minimum_linkrate = min;
1112 
1113 	hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1114 	msleep(100);
1115 	hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1116 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1117 
1118 	return 0;
1119 }
1120 
1121 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1122 				void *funcdata)
1123 {
1124 	struct sas_ha_struct *sas_ha = sas_phy->ha;
1125 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1126 	int phy_no = sas_phy->id;
1127 
1128 	switch (func) {
1129 	case PHY_FUNC_HARD_RESET:
1130 		hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1131 		break;
1132 
1133 	case PHY_FUNC_LINK_RESET:
1134 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1135 		msleep(100);
1136 		hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1137 		break;
1138 
1139 	case PHY_FUNC_DISABLE:
1140 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1141 		break;
1142 
1143 	case PHY_FUNC_SET_LINK_RATE:
1144 		return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1145 	case PHY_FUNC_GET_EVENTS:
1146 		if (hisi_hba->hw->get_events) {
1147 			hisi_hba->hw->get_events(hisi_hba, phy_no);
1148 			break;
1149 		}
1150 		/* fallthru */
1151 	case PHY_FUNC_RELEASE_SPINUP_HOLD:
1152 	default:
1153 		return -EOPNOTSUPP;
1154 	}
1155 	return 0;
1156 }
1157 
1158 static void hisi_sas_task_done(struct sas_task *task)
1159 {
1160 	del_timer(&task->slow_task->timer);
1161 	complete(&task->slow_task->completion);
1162 }
1163 
1164 static void hisi_sas_tmf_timedout(struct timer_list *t)
1165 {
1166 	struct sas_task_slow *slow = from_timer(slow, t, timer);
1167 	struct sas_task *task = slow->task;
1168 	unsigned long flags;
1169 	bool is_completed = true;
1170 
1171 	spin_lock_irqsave(&task->task_state_lock, flags);
1172 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1173 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1174 		is_completed = false;
1175 	}
1176 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1177 
1178 	if (!is_completed)
1179 		complete(&task->slow_task->completion);
1180 }
1181 
1182 #define TASK_TIMEOUT 20
1183 #define TASK_RETRY 3
1184 #define INTERNAL_ABORT_TIMEOUT 6
1185 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1186 					   void *parameter, u32 para_len,
1187 					   struct hisi_sas_tmf_task *tmf)
1188 {
1189 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1190 	struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1191 	struct device *dev = hisi_hba->dev;
1192 	struct sas_task *task;
1193 	int res, retry;
1194 
1195 	for (retry = 0; retry < TASK_RETRY; retry++) {
1196 		task = sas_alloc_slow_task(GFP_KERNEL);
1197 		if (!task)
1198 			return -ENOMEM;
1199 
1200 		task->dev = device;
1201 		task->task_proto = device->tproto;
1202 
1203 		if (dev_is_sata(device)) {
1204 			task->ata_task.device_control_reg_update = 1;
1205 			memcpy(&task->ata_task.fis, parameter, para_len);
1206 		} else {
1207 			memcpy(&task->ssp_task, parameter, para_len);
1208 		}
1209 		task->task_done = hisi_sas_task_done;
1210 
1211 		task->slow_task->timer.function = hisi_sas_tmf_timedout;
1212 		task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1213 		add_timer(&task->slow_task->timer);
1214 
1215 		res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1216 
1217 		if (res) {
1218 			del_timer(&task->slow_task->timer);
1219 			dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1220 				res);
1221 			goto ex_err;
1222 		}
1223 
1224 		wait_for_completion(&task->slow_task->completion);
1225 		res = TMF_RESP_FUNC_FAILED;
1226 		/* Even TMF timed out, return direct. */
1227 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1228 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1229 				struct hisi_sas_slot *slot = task->lldd_task;
1230 
1231 				dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1232 				if (slot) {
1233 					struct hisi_sas_cq *cq =
1234 					       &hisi_hba->cq[slot->dlvry_queue];
1235 					/*
1236 					 * flush tasklet to avoid free'ing task
1237 					 * before using task in IO completion
1238 					 */
1239 					tasklet_kill(&cq->tasklet);
1240 					slot->task = NULL;
1241 				}
1242 
1243 				goto ex_err;
1244 			} else
1245 				dev_err(dev, "abort tmf: TMF task timeout\n");
1246 		}
1247 
1248 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1249 		     task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1250 			res = TMF_RESP_FUNC_COMPLETE;
1251 			break;
1252 		}
1253 
1254 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1255 			task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1256 			res = TMF_RESP_FUNC_SUCC;
1257 			break;
1258 		}
1259 
1260 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1261 		      task->task_status.stat == SAS_DATA_UNDERRUN) {
1262 			/* no error, but return the number of bytes of
1263 			 * underrun
1264 			 */
1265 			dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1266 				 SAS_ADDR(device->sas_addr),
1267 				 task->task_status.resp,
1268 				 task->task_status.stat);
1269 			res = task->task_status.residual;
1270 			break;
1271 		}
1272 
1273 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1274 			task->task_status.stat == SAS_DATA_OVERRUN) {
1275 			dev_warn(dev, "abort tmf: blocked task error\n");
1276 			res = -EMSGSIZE;
1277 			break;
1278 		}
1279 
1280 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1281 		    task->task_status.stat == SAS_OPEN_REJECT) {
1282 			dev_warn(dev, "abort tmf: open reject failed\n");
1283 			res = -EIO;
1284 		} else {
1285 			dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1286 				 SAS_ADDR(device->sas_addr),
1287 				 task->task_status.resp,
1288 				 task->task_status.stat);
1289 		}
1290 		sas_free_task(task);
1291 		task = NULL;
1292 	}
1293 ex_err:
1294 	if (retry == TASK_RETRY)
1295 		dev_warn(dev, "abort tmf: executing internal task failed!\n");
1296 	sas_free_task(task);
1297 	return res;
1298 }
1299 
1300 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1301 		bool reset, int pmp, u8 *fis)
1302 {
1303 	struct ata_taskfile tf;
1304 
1305 	ata_tf_init(dev, &tf);
1306 	if (reset)
1307 		tf.ctl |= ATA_SRST;
1308 	else
1309 		tf.ctl &= ~ATA_SRST;
1310 	tf.command = ATA_CMD_DEV_RESET;
1311 	ata_tf_to_fis(&tf, pmp, 0, fis);
1312 }
1313 
1314 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1315 {
1316 	u8 fis[20] = {0};
1317 	struct ata_port *ap = device->sata_dev.ap;
1318 	struct ata_link *link;
1319 	int rc = TMF_RESP_FUNC_FAILED;
1320 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1321 	struct device *dev = hisi_hba->dev;
1322 	int s = sizeof(struct host_to_dev_fis);
1323 
1324 	ata_for_each_link(link, ap, EDGE) {
1325 		int pmp = sata_srst_pmp(link);
1326 
1327 		hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1328 		rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1329 		if (rc != TMF_RESP_FUNC_COMPLETE)
1330 			break;
1331 	}
1332 
1333 	if (rc == TMF_RESP_FUNC_COMPLETE) {
1334 		ata_for_each_link(link, ap, EDGE) {
1335 			int pmp = sata_srst_pmp(link);
1336 
1337 			hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1338 			rc = hisi_sas_exec_internal_tmf_task(device, fis,
1339 							     s, NULL);
1340 			if (rc != TMF_RESP_FUNC_COMPLETE)
1341 				dev_err(dev, "ata disk de-reset failed\n");
1342 		}
1343 	} else {
1344 		dev_err(dev, "ata disk reset failed\n");
1345 	}
1346 
1347 	if (rc == TMF_RESP_FUNC_COMPLETE)
1348 		hisi_sas_release_task(hisi_hba, device);
1349 
1350 	return rc;
1351 }
1352 
1353 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1354 				u8 *lun, struct hisi_sas_tmf_task *tmf)
1355 {
1356 	struct sas_ssp_task ssp_task;
1357 
1358 	if (!(device->tproto & SAS_PROTOCOL_SSP))
1359 		return TMF_RESP_FUNC_ESUPP;
1360 
1361 	memcpy(ssp_task.LUN, lun, 8);
1362 
1363 	return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1364 				sizeof(ssp_task), tmf);
1365 }
1366 
1367 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1368 {
1369 	u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1370 	int i;
1371 
1372 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1373 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1374 		struct domain_device *device = sas_dev->sas_device;
1375 		struct asd_sas_port *sas_port;
1376 		struct hisi_sas_port *port;
1377 		struct hisi_sas_phy *phy = NULL;
1378 		struct asd_sas_phy *sas_phy;
1379 
1380 		if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1381 				|| !device || !device->port)
1382 			continue;
1383 
1384 		sas_port = device->port;
1385 		port = to_hisi_sas_port(sas_port);
1386 
1387 		list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1388 			if (state & BIT(sas_phy->id)) {
1389 				phy = sas_phy->lldd_phy;
1390 				break;
1391 			}
1392 
1393 		if (phy) {
1394 			port->id = phy->port_id;
1395 
1396 			/* Update linkrate of directly attached device. */
1397 			if (!device->parent)
1398 				device->linkrate = phy->sas_phy.linkrate;
1399 
1400 			hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1401 		} else
1402 			port->id = 0xff;
1403 	}
1404 }
1405 
1406 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1407 {
1408 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1409 	struct asd_sas_port *_sas_port = NULL;
1410 	int phy_no;
1411 
1412 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1413 		struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1414 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1415 		struct asd_sas_port *sas_port = sas_phy->port;
1416 		bool do_port_check = _sas_port != sas_port;
1417 
1418 		if (!sas_phy->phy->enabled)
1419 			continue;
1420 
1421 		/* Report PHY state change to libsas */
1422 		if (state & BIT(phy_no)) {
1423 			if (do_port_check && sas_port && sas_port->port_dev) {
1424 				struct domain_device *dev = sas_port->port_dev;
1425 
1426 				_sas_port = sas_port;
1427 
1428 				if (dev_is_expander(dev->dev_type))
1429 					sas_ha->notify_port_event(sas_phy,
1430 							PORTE_BROADCAST_RCVD);
1431 			}
1432 		} else {
1433 			hisi_sas_phy_down(hisi_hba, phy_no, 0);
1434 		}
1435 
1436 	}
1437 }
1438 
1439 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1440 {
1441 	struct hisi_sas_device *sas_dev;
1442 	struct domain_device *device;
1443 	int i;
1444 
1445 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1446 		sas_dev = &hisi_hba->devices[i];
1447 		device = sas_dev->sas_device;
1448 
1449 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1450 			continue;
1451 
1452 		hisi_sas_init_device(device);
1453 	}
1454 }
1455 
1456 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1457 					     struct asd_sas_port *sas_port,
1458 					     struct domain_device *device)
1459 {
1460 	struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1461 	struct ata_port *ap = device->sata_dev.ap;
1462 	struct device *dev = hisi_hba->dev;
1463 	int s = sizeof(struct host_to_dev_fis);
1464 	int rc = TMF_RESP_FUNC_FAILED;
1465 	struct asd_sas_phy *sas_phy;
1466 	struct ata_link *link;
1467 	u8 fis[20] = {0};
1468 	u32 state;
1469 
1470 	state = hisi_hba->hw->get_phys_state(hisi_hba);
1471 	list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1472 		if (!(state & BIT(sas_phy->id)))
1473 			continue;
1474 
1475 		ata_for_each_link(link, ap, EDGE) {
1476 			int pmp = sata_srst_pmp(link);
1477 
1478 			tmf_task.phy_id = sas_phy->id;
1479 			hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1480 			rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1481 							     &tmf_task);
1482 			if (rc != TMF_RESP_FUNC_COMPLETE) {
1483 				dev_err(dev, "phy%d ata reset failed rc=%d\n",
1484 					sas_phy->id, rc);
1485 				break;
1486 			}
1487 		}
1488 	}
1489 }
1490 
1491 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1492 {
1493 	struct device *dev = hisi_hba->dev;
1494 	int port_no, rc, i;
1495 
1496 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1497 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1498 		struct domain_device *device = sas_dev->sas_device;
1499 
1500 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1501 			continue;
1502 
1503 		rc = hisi_sas_internal_task_abort(hisi_hba, device,
1504 						  HISI_SAS_INT_ABT_DEV, 0);
1505 		if (rc < 0)
1506 			dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1507 	}
1508 
1509 	for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1510 		struct hisi_sas_port *port = &hisi_hba->port[port_no];
1511 		struct asd_sas_port *sas_port = &port->sas_port;
1512 		struct domain_device *port_dev = sas_port->port_dev;
1513 		struct domain_device *device;
1514 
1515 		if (!port_dev || !dev_is_expander(port_dev->dev_type))
1516 			continue;
1517 
1518 		/* Try to find a SATA device */
1519 		list_for_each_entry(device, &sas_port->dev_list,
1520 				    dev_list_node) {
1521 			if (dev_is_sata(device)) {
1522 				hisi_sas_send_ata_reset_each_phy(hisi_hba,
1523 								 sas_port,
1524 								 device);
1525 				break;
1526 			}
1527 		}
1528 	}
1529 }
1530 
1531 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1532 {
1533 	struct Scsi_Host *shost = hisi_hba->shost;
1534 
1535 	down(&hisi_hba->sem);
1536 	hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1537 
1538 	scsi_block_requests(shost);
1539 	hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1540 
1541 	if (timer_pending(&hisi_hba->timer))
1542 		del_timer_sync(&hisi_hba->timer);
1543 
1544 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1545 }
1546 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1547 
1548 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1549 {
1550 	struct Scsi_Host *shost = hisi_hba->shost;
1551 	u32 state;
1552 
1553 	/* Init and wait for PHYs to come up and all libsas event finished. */
1554 	hisi_hba->hw->phys_init(hisi_hba);
1555 	msleep(1000);
1556 	hisi_sas_refresh_port_id(hisi_hba);
1557 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1558 
1559 	if (hisi_hba->reject_stp_links_msk)
1560 		hisi_sas_terminate_stp_reject(hisi_hba);
1561 	hisi_sas_reset_init_all_devices(hisi_hba);
1562 	up(&hisi_hba->sem);
1563 	scsi_unblock_requests(shost);
1564 	clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1565 
1566 	state = hisi_hba->hw->get_phys_state(hisi_hba);
1567 	hisi_sas_rescan_topology(hisi_hba, state);
1568 }
1569 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1570 
1571 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1572 {
1573 	struct device *dev = hisi_hba->dev;
1574 	struct Scsi_Host *shost = hisi_hba->shost;
1575 	int rc;
1576 
1577 	if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1578 		queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1579 
1580 	if (!hisi_hba->hw->soft_reset)
1581 		return -1;
1582 
1583 	if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1584 		return -1;
1585 
1586 	dev_info(dev, "controller resetting...\n");
1587 	hisi_sas_controller_reset_prepare(hisi_hba);
1588 
1589 	rc = hisi_hba->hw->soft_reset(hisi_hba);
1590 	if (rc) {
1591 		dev_warn(dev, "controller reset failed (%d)\n", rc);
1592 		clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1593 		up(&hisi_hba->sem);
1594 		scsi_unblock_requests(shost);
1595 		clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1596 		return rc;
1597 	}
1598 
1599 	hisi_sas_controller_reset_done(hisi_hba);
1600 	dev_info(dev, "controller reset complete\n");
1601 
1602 	return 0;
1603 }
1604 
1605 static int hisi_sas_abort_task(struct sas_task *task)
1606 {
1607 	struct scsi_lun lun;
1608 	struct hisi_sas_tmf_task tmf_task;
1609 	struct domain_device *device = task->dev;
1610 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1611 	struct hisi_hba *hisi_hba;
1612 	struct device *dev;
1613 	int rc = TMF_RESP_FUNC_FAILED;
1614 	unsigned long flags;
1615 
1616 	if (!sas_dev)
1617 		return TMF_RESP_FUNC_FAILED;
1618 
1619 	hisi_hba = dev_to_hisi_hba(task->dev);
1620 	dev = hisi_hba->dev;
1621 
1622 	spin_lock_irqsave(&task->task_state_lock, flags);
1623 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1624 		struct hisi_sas_slot *slot = task->lldd_task;
1625 		struct hisi_sas_cq *cq;
1626 
1627 		if (slot) {
1628 			/*
1629 			 * flush tasklet to avoid free'ing task
1630 			 * before using task in IO completion
1631 			 */
1632 			cq = &hisi_hba->cq[slot->dlvry_queue];
1633 			tasklet_kill(&cq->tasklet);
1634 		}
1635 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1636 		rc = TMF_RESP_FUNC_COMPLETE;
1637 		goto out;
1638 	}
1639 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1640 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1641 
1642 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1643 		struct scsi_cmnd *cmnd = task->uldd_task;
1644 		struct hisi_sas_slot *slot = task->lldd_task;
1645 		u16 tag = slot->idx;
1646 		int rc2;
1647 
1648 		int_to_scsilun(cmnd->device->lun, &lun);
1649 		tmf_task.tmf = TMF_ABORT_TASK;
1650 		tmf_task.tag_of_task_to_be_managed = tag;
1651 
1652 		rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1653 						  &tmf_task);
1654 
1655 		rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1656 						   HISI_SAS_INT_ABT_CMD, tag);
1657 		if (rc2 < 0) {
1658 			dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1659 			return TMF_RESP_FUNC_FAILED;
1660 		}
1661 
1662 		/*
1663 		 * If the TMF finds that the IO is not in the device and also
1664 		 * the internal abort does not succeed, then it is safe to
1665 		 * free the slot.
1666 		 * Note: if the internal abort succeeds then the slot
1667 		 * will have already been completed
1668 		 */
1669 		if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1670 			if (task->lldd_task)
1671 				hisi_sas_do_release_task(hisi_hba, task, slot);
1672 		}
1673 	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1674 		task->task_proto & SAS_PROTOCOL_STP) {
1675 		if (task->dev->dev_type == SAS_SATA_DEV) {
1676 			rc = hisi_sas_internal_task_abort(hisi_hba, device,
1677 							  HISI_SAS_INT_ABT_DEV,
1678 							  0);
1679 			if (rc < 0) {
1680 				dev_err(dev, "abort task: internal abort failed\n");
1681 				goto out;
1682 			}
1683 			hisi_sas_dereg_device(hisi_hba, device);
1684 			rc = hisi_sas_softreset_ata_disk(device);
1685 		}
1686 	} else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1687 		/* SMP */
1688 		struct hisi_sas_slot *slot = task->lldd_task;
1689 		u32 tag = slot->idx;
1690 		struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1691 
1692 		rc = hisi_sas_internal_task_abort(hisi_hba, device,
1693 						  HISI_SAS_INT_ABT_CMD, tag);
1694 		if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1695 					task->lldd_task) {
1696 			/*
1697 			 * flush tasklet to avoid free'ing task
1698 			 * before using task in IO completion
1699 			 */
1700 			tasklet_kill(&cq->tasklet);
1701 			slot->task = NULL;
1702 		}
1703 	}
1704 
1705 out:
1706 	if (rc != TMF_RESP_FUNC_COMPLETE)
1707 		dev_notice(dev, "abort task: rc=%d\n", rc);
1708 	return rc;
1709 }
1710 
1711 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1712 {
1713 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1714 	struct device *dev = hisi_hba->dev;
1715 	struct hisi_sas_tmf_task tmf_task;
1716 	int rc;
1717 
1718 	rc = hisi_sas_internal_task_abort(hisi_hba, device,
1719 					  HISI_SAS_INT_ABT_DEV, 0);
1720 	if (rc < 0) {
1721 		dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1722 		return TMF_RESP_FUNC_FAILED;
1723 	}
1724 	hisi_sas_dereg_device(hisi_hba, device);
1725 
1726 	tmf_task.tmf = TMF_ABORT_TASK_SET;
1727 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1728 
1729 	if (rc == TMF_RESP_FUNC_COMPLETE)
1730 		hisi_sas_release_task(hisi_hba, device);
1731 
1732 	return rc;
1733 }
1734 
1735 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1736 {
1737 	struct hisi_sas_tmf_task tmf_task;
1738 	int rc;
1739 
1740 	tmf_task.tmf = TMF_CLEAR_ACA;
1741 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1742 
1743 	return rc;
1744 }
1745 
1746 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1747 {
1748 	struct sas_phy *local_phy = sas_get_local_phy(device);
1749 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1750 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1751 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1752 	DECLARE_COMPLETION_ONSTACK(phyreset);
1753 	int rc, reset_type;
1754 
1755 	if (!local_phy->enabled) {
1756 		sas_put_local_phy(local_phy);
1757 		return -ENODEV;
1758 	}
1759 
1760 	if (scsi_is_sas_phy_local(local_phy)) {
1761 		struct asd_sas_phy *sas_phy =
1762 			sas_ha->sas_phy[local_phy->number];
1763 		struct hisi_sas_phy *phy =
1764 			container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1765 		phy->in_reset = 1;
1766 		phy->reset_completion = &phyreset;
1767 	}
1768 
1769 	reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1770 		      !dev_is_sata(device)) ? true : false;
1771 
1772 	rc = sas_phy_reset(local_phy, reset_type);
1773 	sas_put_local_phy(local_phy);
1774 
1775 	if (scsi_is_sas_phy_local(local_phy)) {
1776 		struct asd_sas_phy *sas_phy =
1777 			sas_ha->sas_phy[local_phy->number];
1778 		struct hisi_sas_phy *phy =
1779 			container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1780 		int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1781 		unsigned long flags;
1782 
1783 		spin_lock_irqsave(&phy->lock, flags);
1784 		phy->reset_completion = NULL;
1785 		phy->in_reset = 0;
1786 		spin_unlock_irqrestore(&phy->lock, flags);
1787 
1788 		/* report PHY down if timed out */
1789 		if (!ret)
1790 			hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1791 	} else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1792 		/*
1793 		 * If in init state, we rely on caller to wait for link to be
1794 		 * ready; otherwise, except phy reset is fail, delay.
1795 		 */
1796 		if (!rc)
1797 			msleep(2000);
1798 	}
1799 
1800 	return rc;
1801 }
1802 
1803 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1804 {
1805 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1806 	struct device *dev = hisi_hba->dev;
1807 	int rc;
1808 
1809 	rc = hisi_sas_internal_task_abort(hisi_hba, device,
1810 					  HISI_SAS_INT_ABT_DEV, 0);
1811 	if (rc < 0) {
1812 		dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1813 		return TMF_RESP_FUNC_FAILED;
1814 	}
1815 	hisi_sas_dereg_device(hisi_hba, device);
1816 
1817 	if (dev_is_sata(device)) {
1818 		rc = hisi_sas_softreset_ata_disk(device);
1819 		if (rc == TMF_RESP_FUNC_FAILED)
1820 			return TMF_RESP_FUNC_FAILED;
1821 	}
1822 
1823 	rc = hisi_sas_debug_I_T_nexus_reset(device);
1824 
1825 	if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1826 		hisi_sas_release_task(hisi_hba, device);
1827 
1828 	return rc;
1829 }
1830 
1831 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1832 {
1833 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1834 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1835 	struct device *dev = hisi_hba->dev;
1836 	int rc = TMF_RESP_FUNC_FAILED;
1837 
1838 	/* Clear internal IO and then lu reset */
1839 	rc = hisi_sas_internal_task_abort(hisi_hba, device,
1840 					  HISI_SAS_INT_ABT_DEV, 0);
1841 	if (rc < 0) {
1842 		dev_err(dev, "lu_reset: internal abort failed\n");
1843 		goto out;
1844 	}
1845 	hisi_sas_dereg_device(hisi_hba, device);
1846 
1847 	if (dev_is_sata(device)) {
1848 		struct sas_phy *phy;
1849 
1850 		phy = sas_get_local_phy(device);
1851 
1852 		rc = sas_phy_reset(phy, true);
1853 
1854 		if (rc == 0)
1855 			hisi_sas_release_task(hisi_hba, device);
1856 		sas_put_local_phy(phy);
1857 	} else {
1858 		struct hisi_sas_tmf_task tmf_task = { .tmf =  TMF_LU_RESET };
1859 
1860 		rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1861 		if (rc == TMF_RESP_FUNC_COMPLETE)
1862 			hisi_sas_release_task(hisi_hba, device);
1863 	}
1864 out:
1865 	if (rc != TMF_RESP_FUNC_COMPLETE)
1866 		dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1867 			     sas_dev->device_id, rc);
1868 	return rc;
1869 }
1870 
1871 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1872 {
1873 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1874 	struct device *dev = hisi_hba->dev;
1875 	HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1876 	int rc, i;
1877 
1878 	queue_work(hisi_hba->wq, &r.work);
1879 	wait_for_completion(r.completion);
1880 	if (!r.done)
1881 		return TMF_RESP_FUNC_FAILED;
1882 
1883 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1884 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1885 		struct domain_device *device = sas_dev->sas_device;
1886 
1887 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1888 		    dev_is_expander(device->dev_type))
1889 			continue;
1890 
1891 		rc = hisi_sas_debug_I_T_nexus_reset(device);
1892 		if (rc != TMF_RESP_FUNC_COMPLETE)
1893 			dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1894 				 sas_dev->device_id, rc);
1895 	}
1896 
1897 	hisi_sas_release_tasks(hisi_hba);
1898 
1899 	return TMF_RESP_FUNC_COMPLETE;
1900 }
1901 
1902 static int hisi_sas_query_task(struct sas_task *task)
1903 {
1904 	struct scsi_lun lun;
1905 	struct hisi_sas_tmf_task tmf_task;
1906 	int rc = TMF_RESP_FUNC_FAILED;
1907 
1908 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1909 		struct scsi_cmnd *cmnd = task->uldd_task;
1910 		struct domain_device *device = task->dev;
1911 		struct hisi_sas_slot *slot = task->lldd_task;
1912 		u32 tag = slot->idx;
1913 
1914 		int_to_scsilun(cmnd->device->lun, &lun);
1915 		tmf_task.tmf = TMF_QUERY_TASK;
1916 		tmf_task.tag_of_task_to_be_managed = tag;
1917 
1918 		rc = hisi_sas_debug_issue_ssp_tmf(device,
1919 						  lun.scsi_lun,
1920 						  &tmf_task);
1921 		switch (rc) {
1922 		/* The task is still in Lun, release it then */
1923 		case TMF_RESP_FUNC_SUCC:
1924 		/* The task is not in Lun or failed, reset the phy */
1925 		case TMF_RESP_FUNC_FAILED:
1926 		case TMF_RESP_FUNC_COMPLETE:
1927 			break;
1928 		default:
1929 			rc = TMF_RESP_FUNC_FAILED;
1930 			break;
1931 		}
1932 	}
1933 	return rc;
1934 }
1935 
1936 static int
1937 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1938 				  struct sas_task *task, int abort_flag,
1939 				  int task_tag, struct hisi_sas_dq *dq)
1940 {
1941 	struct domain_device *device = task->dev;
1942 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1943 	struct device *dev = hisi_hba->dev;
1944 	struct hisi_sas_port *port;
1945 	struct hisi_sas_slot *slot;
1946 	struct asd_sas_port *sas_port = device->port;
1947 	struct hisi_sas_cmd_hdr *cmd_hdr_base;
1948 	int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1949 	unsigned long flags;
1950 	int wr_q_index;
1951 
1952 	if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1953 		return -EINVAL;
1954 
1955 	if (!device->port)
1956 		return -1;
1957 
1958 	port = to_hisi_sas_port(sas_port);
1959 
1960 	/* simply get a slot and send abort command */
1961 	rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1962 	if (rc < 0)
1963 		goto err_out;
1964 
1965 	slot_idx = rc;
1966 	slot = &hisi_hba->slot_info[slot_idx];
1967 
1968 	spin_lock_irqsave(&dq->lock, flags);
1969 	wr_q_index = dq->wr_point;
1970 	dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1971 	list_add_tail(&slot->delivery, &dq->list);
1972 	spin_unlock_irqrestore(&dq->lock, flags);
1973 	spin_lock_irqsave(&sas_dev->lock, flags);
1974 	list_add_tail(&slot->entry, &sas_dev->list);
1975 	spin_unlock_irqrestore(&sas_dev->lock, flags);
1976 
1977 	dlvry_queue = dq->id;
1978 	dlvry_queue_slot = wr_q_index;
1979 
1980 	slot->device_id = sas_dev->device_id;
1981 	slot->n_elem = n_elem;
1982 	slot->dlvry_queue = dlvry_queue;
1983 	slot->dlvry_queue_slot = dlvry_queue_slot;
1984 	cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1985 	slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1986 	slot->task = task;
1987 	slot->port = port;
1988 	slot->is_internal = true;
1989 	task->lldd_task = slot;
1990 
1991 	memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1992 	memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1993 	memset(hisi_sas_status_buf_addr_mem(slot), 0,
1994 	       sizeof(struct hisi_sas_err_record));
1995 
1996 	hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1997 				      abort_flag, task_tag);
1998 
1999 	spin_lock_irqsave(&task->task_state_lock, flags);
2000 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
2001 	spin_unlock_irqrestore(&task->task_state_lock, flags);
2002 	WRITE_ONCE(slot->ready, 1);
2003 	/* send abort command to the chip */
2004 	spin_lock_irqsave(&dq->lock, flags);
2005 	hisi_hba->hw->start_delivery(dq);
2006 	spin_unlock_irqrestore(&dq->lock, flags);
2007 
2008 	return 0;
2009 
2010 err_out:
2011 	dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2012 
2013 	return rc;
2014 }
2015 
2016 /**
2017  * _hisi_sas_internal_task_abort -- execute an internal
2018  * abort command for single IO command or a device
2019  * @hisi_hba: host controller struct
2020  * @device: domain device
2021  * @abort_flag: mode of operation, device or single IO
2022  * @tag: tag of IO to be aborted (only relevant to single
2023  *       IO mode)
2024  * @dq: delivery queue for this internal abort command
2025  */
2026 static int
2027 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2028 			      struct domain_device *device, int abort_flag,
2029 			      int tag, struct hisi_sas_dq *dq)
2030 {
2031 	struct sas_task *task;
2032 	struct hisi_sas_device *sas_dev = device->lldd_dev;
2033 	struct device *dev = hisi_hba->dev;
2034 	int res;
2035 
2036 	/*
2037 	 * The interface is not realized means this HW don't support internal
2038 	 * abort, or don't need to do internal abort. Then here, we return
2039 	 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2040 	 * the internal abort has been executed and returned CQ.
2041 	 */
2042 	if (!hisi_hba->hw->prep_abort)
2043 		return TMF_RESP_FUNC_FAILED;
2044 
2045 	task = sas_alloc_slow_task(GFP_KERNEL);
2046 	if (!task)
2047 		return -ENOMEM;
2048 
2049 	task->dev = device;
2050 	task->task_proto = device->tproto;
2051 	task->task_done = hisi_sas_task_done;
2052 	task->slow_task->timer.function = hisi_sas_tmf_timedout;
2053 	task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2054 	add_timer(&task->slow_task->timer);
2055 
2056 	res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2057 						task, abort_flag, tag, dq);
2058 	if (res) {
2059 		del_timer(&task->slow_task->timer);
2060 		dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2061 			res);
2062 		goto exit;
2063 	}
2064 	wait_for_completion(&task->slow_task->completion);
2065 	res = TMF_RESP_FUNC_FAILED;
2066 
2067 	/* Internal abort timed out */
2068 	if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2069 		if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
2070 			queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2071 
2072 		if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2073 			struct hisi_sas_slot *slot = task->lldd_task;
2074 
2075 			if (slot) {
2076 				struct hisi_sas_cq *cq =
2077 					&hisi_hba->cq[slot->dlvry_queue];
2078 				/*
2079 				 * flush tasklet to avoid free'ing task
2080 				 * before using task in IO completion
2081 				 */
2082 				tasklet_kill(&cq->tasklet);
2083 				slot->task = NULL;
2084 			}
2085 			dev_err(dev, "internal task abort: timeout and not done.\n");
2086 
2087 			res = -EIO;
2088 			goto exit;
2089 		} else
2090 			dev_err(dev, "internal task abort: timeout.\n");
2091 	}
2092 
2093 	if (task->task_status.resp == SAS_TASK_COMPLETE &&
2094 		task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2095 		res = TMF_RESP_FUNC_COMPLETE;
2096 		goto exit;
2097 	}
2098 
2099 	if (task->task_status.resp == SAS_TASK_COMPLETE &&
2100 		task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2101 		res = TMF_RESP_FUNC_SUCC;
2102 		goto exit;
2103 	}
2104 
2105 exit:
2106 	dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2107 		SAS_ADDR(device->sas_addr), task,
2108 		task->task_status.resp, /* 0 is complete, -1 is undelivered */
2109 		task->task_status.stat);
2110 	sas_free_task(task);
2111 
2112 	return res;
2113 }
2114 
2115 static int
2116 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2117 			     struct domain_device *device,
2118 			     int abort_flag, int tag)
2119 {
2120 	struct hisi_sas_slot *slot;
2121 	struct device *dev = hisi_hba->dev;
2122 	struct hisi_sas_dq *dq;
2123 	int i, rc;
2124 
2125 	switch (abort_flag) {
2126 	case HISI_SAS_INT_ABT_CMD:
2127 		slot = &hisi_hba->slot_info[tag];
2128 		dq = &hisi_hba->dq[slot->dlvry_queue];
2129 		return _hisi_sas_internal_task_abort(hisi_hba, device,
2130 						     abort_flag, tag, dq);
2131 	case HISI_SAS_INT_ABT_DEV:
2132 		for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2133 			struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2134 			const struct cpumask *mask = cq->pci_irq_mask;
2135 
2136 			if (mask && !cpumask_intersects(cpu_online_mask, mask))
2137 				continue;
2138 			dq = &hisi_hba->dq[i];
2139 			rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2140 							   abort_flag, tag,
2141 							   dq);
2142 			if (rc)
2143 				return rc;
2144 		}
2145 		break;
2146 	default:
2147 		dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2148 			abort_flag);
2149 		return -EINVAL;
2150 	}
2151 
2152 	return 0;
2153 }
2154 
2155 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2156 {
2157 	hisi_sas_port_notify_formed(sas_phy);
2158 }
2159 
2160 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2161 			u8 reg_index, u8 reg_count, u8 *write_data)
2162 {
2163 	struct hisi_hba *hisi_hba = sha->lldd_ha;
2164 
2165 	if (!hisi_hba->hw->write_gpio)
2166 		return -EOPNOTSUPP;
2167 
2168 	return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2169 				reg_index, reg_count, write_data);
2170 }
2171 
2172 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2173 {
2174 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2175 	struct sas_phy *sphy = sas_phy->phy;
2176 	unsigned long flags;
2177 
2178 	phy->phy_attached = 0;
2179 	phy->phy_type = 0;
2180 	phy->port = NULL;
2181 
2182 	spin_lock_irqsave(&phy->lock, flags);
2183 	if (phy->enable)
2184 		sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2185 	else
2186 		sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2187 	spin_unlock_irqrestore(&phy->lock, flags);
2188 }
2189 
2190 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
2191 {
2192 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2193 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2194 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
2195 	struct device *dev = hisi_hba->dev;
2196 
2197 	if (rdy) {
2198 		/* Phy down but ready */
2199 		hisi_sas_bytes_dmaed(hisi_hba, phy_no);
2200 		hisi_sas_port_notify_formed(sas_phy);
2201 	} else {
2202 		struct hisi_sas_port *port  = phy->port;
2203 
2204 		if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2205 		    phy->in_reset) {
2206 			dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2207 			return;
2208 		}
2209 		/* Phy down and not ready */
2210 		sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2211 		sas_phy_disconnected(sas_phy);
2212 
2213 		if (port) {
2214 			if (phy->phy_type & PORT_TYPE_SAS) {
2215 				int port_id = port->id;
2216 
2217 				if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2218 								       port_id))
2219 					port->port_attached = 0;
2220 			} else if (phy->phy_type & PORT_TYPE_SATA)
2221 				port->port_attached = 0;
2222 		}
2223 		hisi_sas_phy_disconnected(phy);
2224 	}
2225 }
2226 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2227 
2228 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
2229 {
2230 	int i;
2231 
2232 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2233 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2234 
2235 		tasklet_kill(&cq->tasklet);
2236 	}
2237 }
2238 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
2239 
2240 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2241 {
2242 	struct hisi_hba *hisi_hba = shost_priv(shost);
2243 
2244 	if (reset_type != SCSI_ADAPTER_RESET)
2245 		return -EOPNOTSUPP;
2246 
2247 	queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2248 
2249 	return 0;
2250 }
2251 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2252 
2253 struct scsi_transport_template *hisi_sas_stt;
2254 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2255 
2256 static struct sas_domain_function_template hisi_sas_transport_ops = {
2257 	.lldd_dev_found		= hisi_sas_dev_found,
2258 	.lldd_dev_gone		= hisi_sas_dev_gone,
2259 	.lldd_execute_task	= hisi_sas_queue_command,
2260 	.lldd_control_phy	= hisi_sas_control_phy,
2261 	.lldd_abort_task	= hisi_sas_abort_task,
2262 	.lldd_abort_task_set	= hisi_sas_abort_task_set,
2263 	.lldd_clear_aca		= hisi_sas_clear_aca,
2264 	.lldd_I_T_nexus_reset	= hisi_sas_I_T_nexus_reset,
2265 	.lldd_lu_reset		= hisi_sas_lu_reset,
2266 	.lldd_query_task	= hisi_sas_query_task,
2267 	.lldd_clear_nexus_ha	= hisi_sas_clear_nexus_ha,
2268 	.lldd_port_formed	= hisi_sas_port_formed,
2269 	.lldd_write_gpio	= hisi_sas_write_gpio,
2270 };
2271 
2272 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2273 {
2274 	int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2275 	struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2276 
2277 	for (i = 0; i < hisi_hba->queue_count; i++) {
2278 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2279 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2280 		struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2281 
2282 		s = sizeof(struct hisi_sas_cmd_hdr);
2283 		for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2284 			memset(&cmd_hdr[j], 0, s);
2285 
2286 		dq->wr_point = 0;
2287 
2288 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2289 		memset(hisi_hba->complete_hdr[i], 0, s);
2290 		cq->rd_point = 0;
2291 	}
2292 
2293 	s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2294 	memset(hisi_hba->initial_fis, 0, s);
2295 
2296 	s = max_command_entries * sizeof(struct hisi_sas_iost);
2297 	memset(hisi_hba->iost, 0, s);
2298 
2299 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2300 	memset(hisi_hba->breakpoint, 0, s);
2301 
2302 	s = sizeof(struct hisi_sas_sata_breakpoint);
2303 	for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2304 		memset(&sata_breakpoint[j], 0, s);
2305 }
2306 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2307 
2308 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2309 {
2310 	struct device *dev = hisi_hba->dev;
2311 	int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2312 	int max_command_entries_ru, sz_slot_buf_ru;
2313 	int blk_cnt, slots_per_blk;
2314 
2315 	sema_init(&hisi_hba->sem, 1);
2316 	spin_lock_init(&hisi_hba->lock);
2317 	for (i = 0; i < hisi_hba->n_phy; i++) {
2318 		hisi_sas_phy_init(hisi_hba, i);
2319 		hisi_hba->port[i].port_attached = 0;
2320 		hisi_hba->port[i].id = -1;
2321 	}
2322 
2323 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2324 		hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2325 		hisi_hba->devices[i].device_id = i;
2326 		hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2327 	}
2328 
2329 	for (i = 0; i < hisi_hba->queue_count; i++) {
2330 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2331 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2332 
2333 		/* Completion queue structure */
2334 		cq->id = i;
2335 		cq->hisi_hba = hisi_hba;
2336 
2337 		/* Delivery queue structure */
2338 		spin_lock_init(&dq->lock);
2339 		INIT_LIST_HEAD(&dq->list);
2340 		dq->id = i;
2341 		dq->hisi_hba = hisi_hba;
2342 
2343 		/* Delivery queue */
2344 		s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2345 		hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2346 						&hisi_hba->cmd_hdr_dma[i],
2347 						GFP_KERNEL);
2348 		if (!hisi_hba->cmd_hdr[i])
2349 			goto err_out;
2350 
2351 		/* Completion queue */
2352 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2353 		hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2354 						&hisi_hba->complete_hdr_dma[i],
2355 						GFP_KERNEL);
2356 		if (!hisi_hba->complete_hdr[i])
2357 			goto err_out;
2358 	}
2359 
2360 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2361 	hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2362 					     GFP_KERNEL);
2363 	if (!hisi_hba->itct)
2364 		goto err_out;
2365 
2366 	hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2367 					   sizeof(struct hisi_sas_slot),
2368 					   GFP_KERNEL);
2369 	if (!hisi_hba->slot_info)
2370 		goto err_out;
2371 
2372 	/* roundup to avoid overly large block size */
2373 	max_command_entries_ru = roundup(max_command_entries, 64);
2374 	if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2375 		sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2376 	else
2377 		sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2378 	sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2379 	s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2380 	blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2381 	slots_per_blk = s / sz_slot_buf_ru;
2382 
2383 	for (i = 0; i < blk_cnt; i++) {
2384 		int slot_index = i * slots_per_blk;
2385 		dma_addr_t buf_dma;
2386 		void *buf;
2387 
2388 		buf = dmam_alloc_coherent(dev, s, &buf_dma,
2389 					  GFP_KERNEL);
2390 		if (!buf)
2391 			goto err_out;
2392 
2393 		for (j = 0; j < slots_per_blk; j++, slot_index++) {
2394 			struct hisi_sas_slot *slot;
2395 
2396 			slot = &hisi_hba->slot_info[slot_index];
2397 			slot->buf = buf;
2398 			slot->buf_dma = buf_dma;
2399 			slot->idx = slot_index;
2400 
2401 			buf += sz_slot_buf_ru;
2402 			buf_dma += sz_slot_buf_ru;
2403 		}
2404 	}
2405 
2406 	s = max_command_entries * sizeof(struct hisi_sas_iost);
2407 	hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2408 					     GFP_KERNEL);
2409 	if (!hisi_hba->iost)
2410 		goto err_out;
2411 
2412 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2413 	hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2414 						   &hisi_hba->breakpoint_dma,
2415 						   GFP_KERNEL);
2416 	if (!hisi_hba->breakpoint)
2417 		goto err_out;
2418 
2419 	hisi_hba->slot_index_count = max_command_entries;
2420 	s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2421 	hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2422 	if (!hisi_hba->slot_index_tags)
2423 		goto err_out;
2424 
2425 	s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2426 	hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2427 						    &hisi_hba->initial_fis_dma,
2428 						    GFP_KERNEL);
2429 	if (!hisi_hba->initial_fis)
2430 		goto err_out;
2431 
2432 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2433 	hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2434 					&hisi_hba->sata_breakpoint_dma,
2435 					GFP_KERNEL);
2436 	if (!hisi_hba->sata_breakpoint)
2437 		goto err_out;
2438 
2439 	hisi_sas_slot_index_init(hisi_hba);
2440 	hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2441 
2442 	hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2443 	if (!hisi_hba->wq) {
2444 		dev_err(dev, "sas_alloc: failed to create workqueue\n");
2445 		goto err_out;
2446 	}
2447 
2448 	return 0;
2449 err_out:
2450 	return -ENOMEM;
2451 }
2452 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2453 
2454 void hisi_sas_free(struct hisi_hba *hisi_hba)
2455 {
2456 	int i;
2457 
2458 	for (i = 0; i < hisi_hba->n_phy; i++) {
2459 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2460 
2461 		del_timer_sync(&phy->timer);
2462 	}
2463 
2464 	if (hisi_hba->wq)
2465 		destroy_workqueue(hisi_hba->wq);
2466 }
2467 EXPORT_SYMBOL_GPL(hisi_sas_free);
2468 
2469 void hisi_sas_rst_work_handler(struct work_struct *work)
2470 {
2471 	struct hisi_hba *hisi_hba =
2472 		container_of(work, struct hisi_hba, rst_work);
2473 
2474 	hisi_sas_controller_reset(hisi_hba);
2475 }
2476 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2477 
2478 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2479 {
2480 	struct hisi_sas_rst *rst =
2481 		container_of(work, struct hisi_sas_rst, work);
2482 
2483 	if (!hisi_sas_controller_reset(rst->hisi_hba))
2484 		rst->done = true;
2485 	complete(rst->completion);
2486 }
2487 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2488 
2489 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2490 {
2491 	struct device *dev = hisi_hba->dev;
2492 	struct platform_device *pdev = hisi_hba->platform_dev;
2493 	struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2494 	struct clk *refclk;
2495 
2496 	if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2497 					  SAS_ADDR_SIZE)) {
2498 		dev_err(dev, "could not get property sas-addr\n");
2499 		return -ENOENT;
2500 	}
2501 
2502 	if (np) {
2503 		/*
2504 		 * These properties are only required for platform device-based
2505 		 * controller with DT firmware.
2506 		 */
2507 		hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2508 					"hisilicon,sas-syscon");
2509 		if (IS_ERR(hisi_hba->ctrl)) {
2510 			dev_err(dev, "could not get syscon\n");
2511 			return -ENOENT;
2512 		}
2513 
2514 		if (device_property_read_u32(dev, "ctrl-reset-reg",
2515 					     &hisi_hba->ctrl_reset_reg)) {
2516 			dev_err(dev, "could not get property ctrl-reset-reg\n");
2517 			return -ENOENT;
2518 		}
2519 
2520 		if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2521 					     &hisi_hba->ctrl_reset_sts_reg)) {
2522 			dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2523 			return -ENOENT;
2524 		}
2525 
2526 		if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2527 					     &hisi_hba->ctrl_clock_ena_reg)) {
2528 			dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2529 			return -ENOENT;
2530 		}
2531 	}
2532 
2533 	refclk = devm_clk_get(dev, NULL);
2534 	if (IS_ERR(refclk))
2535 		dev_dbg(dev, "no ref clk property\n");
2536 	else
2537 		hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2538 
2539 	if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2540 		dev_err(dev, "could not get property phy-count\n");
2541 		return -ENOENT;
2542 	}
2543 
2544 	if (device_property_read_u32(dev, "queue-count",
2545 				     &hisi_hba->queue_count)) {
2546 		dev_err(dev, "could not get property queue-count\n");
2547 		return -ENOENT;
2548 	}
2549 
2550 	return 0;
2551 }
2552 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2553 
2554 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2555 					      const struct hisi_sas_hw *hw)
2556 {
2557 	struct resource *res;
2558 	struct Scsi_Host *shost;
2559 	struct hisi_hba *hisi_hba;
2560 	struct device *dev = &pdev->dev;
2561 	int error;
2562 
2563 	shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2564 	if (!shost) {
2565 		dev_err(dev, "scsi host alloc failed\n");
2566 		return NULL;
2567 	}
2568 	hisi_hba = shost_priv(shost);
2569 
2570 	INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2571 	hisi_hba->hw = hw;
2572 	hisi_hba->dev = dev;
2573 	hisi_hba->platform_dev = pdev;
2574 	hisi_hba->shost = shost;
2575 	SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2576 
2577 	timer_setup(&hisi_hba->timer, NULL, 0);
2578 
2579 	if (hisi_sas_get_fw_info(hisi_hba) < 0)
2580 		goto err_out;
2581 
2582 	error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2583 	if (error)
2584 		error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2585 
2586 	if (error) {
2587 		dev_err(dev, "No usable DMA addressing method\n");
2588 		goto err_out;
2589 	}
2590 
2591 	hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2592 	if (IS_ERR(hisi_hba->regs))
2593 		goto err_out;
2594 
2595 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2596 	if (res) {
2597 		hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2598 		if (IS_ERR(hisi_hba->sgpio_regs))
2599 			goto err_out;
2600 	}
2601 
2602 	if (hisi_sas_alloc(hisi_hba)) {
2603 		hisi_sas_free(hisi_hba);
2604 		goto err_out;
2605 	}
2606 
2607 	return shost;
2608 err_out:
2609 	scsi_host_put(shost);
2610 	dev_err(dev, "shost alloc failed\n");
2611 	return NULL;
2612 }
2613 
2614 int hisi_sas_probe(struct platform_device *pdev,
2615 		   const struct hisi_sas_hw *hw)
2616 {
2617 	struct Scsi_Host *shost;
2618 	struct hisi_hba *hisi_hba;
2619 	struct device *dev = &pdev->dev;
2620 	struct asd_sas_phy **arr_phy;
2621 	struct asd_sas_port **arr_port;
2622 	struct sas_ha_struct *sha;
2623 	int rc, phy_nr, port_nr, i;
2624 
2625 	shost = hisi_sas_shost_alloc(pdev, hw);
2626 	if (!shost)
2627 		return -ENOMEM;
2628 
2629 	sha = SHOST_TO_SAS_HA(shost);
2630 	hisi_hba = shost_priv(shost);
2631 	platform_set_drvdata(pdev, sha);
2632 
2633 	phy_nr = port_nr = hisi_hba->n_phy;
2634 
2635 	arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2636 	arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2637 	if (!arr_phy || !arr_port) {
2638 		rc = -ENOMEM;
2639 		goto err_out_ha;
2640 	}
2641 
2642 	sha->sas_phy = arr_phy;
2643 	sha->sas_port = arr_port;
2644 	sha->lldd_ha = hisi_hba;
2645 
2646 	shost->transportt = hisi_sas_stt;
2647 	shost->max_id = HISI_SAS_MAX_DEVICES;
2648 	shost->max_lun = ~0;
2649 	shost->max_channel = 1;
2650 	shost->max_cmd_len = 16;
2651 	if (hisi_hba->hw->slot_index_alloc) {
2652 		shost->can_queue = HISI_SAS_MAX_COMMANDS;
2653 		shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2654 	} else {
2655 		shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2656 		shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2657 	}
2658 
2659 	sha->sas_ha_name = DRV_NAME;
2660 	sha->dev = hisi_hba->dev;
2661 	sha->lldd_module = THIS_MODULE;
2662 	sha->sas_addr = &hisi_hba->sas_addr[0];
2663 	sha->num_phys = hisi_hba->n_phy;
2664 	sha->core.shost = hisi_hba->shost;
2665 
2666 	for (i = 0; i < hisi_hba->n_phy; i++) {
2667 		sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2668 		sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2669 	}
2670 
2671 	rc = scsi_add_host(shost, &pdev->dev);
2672 	if (rc)
2673 		goto err_out_ha;
2674 
2675 	rc = sas_register_ha(sha);
2676 	if (rc)
2677 		goto err_out_register_ha;
2678 
2679 	rc = hisi_hba->hw->hw_init(hisi_hba);
2680 	if (rc)
2681 		goto err_out_register_ha;
2682 
2683 	scsi_scan_host(shost);
2684 
2685 	return 0;
2686 
2687 err_out_register_ha:
2688 	scsi_remove_host(shost);
2689 err_out_ha:
2690 	hisi_sas_debugfs_exit(hisi_hba);
2691 	hisi_sas_free(hisi_hba);
2692 	scsi_host_put(shost);
2693 	return rc;
2694 }
2695 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2696 
2697 struct dentry *hisi_sas_debugfs_dir;
2698 
2699 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2700 {
2701 	int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2702 	int dump_index = hisi_hba->debugfs_dump_index;
2703 	int i;
2704 
2705 	for (i = 0; i < hisi_hba->queue_count; i++)
2706 		memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr,
2707 		       hisi_hba->complete_hdr[i],
2708 		       HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2709 }
2710 
2711 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2712 {
2713 	int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
2714 	int dump_index = hisi_hba->debugfs_dump_index;
2715 	int i;
2716 
2717 	for (i = 0; i < hisi_hba->queue_count; i++) {
2718 		struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
2719 		int j;
2720 
2721 		debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr;
2722 		cmd_hdr = hisi_hba->cmd_hdr[i];
2723 
2724 		for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2725 			memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
2726 			       queue_entry_size);
2727 	}
2728 }
2729 
2730 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2731 {
2732 	int dump_index = hisi_hba->debugfs_dump_index;
2733 	const struct hisi_sas_debugfs_reg *port =
2734 		hisi_hba->hw->debugfs_reg_port;
2735 	int i, phy_cnt;
2736 	u32 offset;
2737 	u32 *databuf;
2738 
2739 	for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2740 		databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data;
2741 		for (i = 0; i < port->count; i++, databuf++) {
2742 			offset = port->base_off + 4 * i;
2743 			*databuf = port->read_port_reg(hisi_hba, phy_cnt,
2744 						       offset);
2745 		}
2746 	}
2747 }
2748 
2749 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2750 {
2751 	int dump_index = hisi_hba->debugfs_dump_index;
2752 	u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data;
2753 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2754 	const struct hisi_sas_debugfs_reg *global =
2755 			hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2756 	int i;
2757 
2758 	for (i = 0; i < global->count; i++, databuf++)
2759 		*databuf = global->read_global_reg(hisi_hba, 4 * i);
2760 }
2761 
2762 static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba)
2763 {
2764 	int dump_index = hisi_hba->debugfs_dump_index;
2765 	u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data;
2766 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2767 	const struct hisi_sas_debugfs_reg *axi =
2768 			hw->debugfs_reg_array[DEBUGFS_AXI];
2769 	int i;
2770 
2771 	for (i = 0; i < axi->count; i++, databuf++)
2772 		*databuf = axi->read_global_reg(hisi_hba,
2773 						4 * i + axi->base_off);
2774 }
2775 
2776 static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba)
2777 {
2778 	int dump_index = hisi_hba->debugfs_dump_index;
2779 	u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data;
2780 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2781 	const struct hisi_sas_debugfs_reg *ras =
2782 			hw->debugfs_reg_array[DEBUGFS_RAS];
2783 	int i;
2784 
2785 	for (i = 0; i < ras->count; i++, databuf++)
2786 		*databuf = ras->read_global_reg(hisi_hba,
2787 						4 * i + ras->base_off);
2788 }
2789 
2790 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2791 {
2792 	int dump_index = hisi_hba->debugfs_dump_index;
2793 	void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache;
2794 	void *databuf = hisi_hba->debugfs_itct[dump_index].itct;
2795 	struct hisi_sas_itct *itct;
2796 	int i;
2797 
2798 	hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE,
2799 					   cachebuf);
2800 
2801 	itct = hisi_hba->itct;
2802 
2803 	for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2804 		memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2805 		databuf += sizeof(struct hisi_sas_itct);
2806 	}
2807 }
2808 
2809 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2810 {
2811 	int dump_index = hisi_hba->debugfs_dump_index;
2812 	int max_command_entries = HISI_SAS_MAX_COMMANDS;
2813 	void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache;
2814 	void *databuf = hisi_hba->debugfs_iost[dump_index].iost;
2815 	struct hisi_sas_iost *iost;
2816 	int i;
2817 
2818 	hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE,
2819 					   cachebuf);
2820 
2821 	iost = hisi_hba->iost;
2822 
2823 	for (i = 0; i < max_command_entries; i++, iost++) {
2824 		memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2825 		databuf += sizeof(struct hisi_sas_iost);
2826 	}
2827 }
2828 
2829 static const char *
2830 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2831 			     const struct hisi_sas_debugfs_reg_lu *lu)
2832 {
2833 	for (; lu->name; lu++) {
2834 		if (off == lu->off - base_off)
2835 			return lu->name;
2836 	}
2837 
2838 	return NULL;
2839 }
2840 
2841 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2842 				       struct seq_file *s)
2843 {
2844 	const struct hisi_sas_debugfs_reg *reg = ptr;
2845 	int i;
2846 
2847 	for (i = 0; i < reg->count; i++) {
2848 		int off = i * 4;
2849 		const char *name;
2850 
2851 		name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2852 						    reg->lu);
2853 
2854 		if (name)
2855 			seq_printf(s, "0x%08x 0x%08x %s\n", off,
2856 				   regs_val[i], name);
2857 		else
2858 			seq_printf(s, "0x%08x 0x%08x\n", off,
2859 				   regs_val[i]);
2860 	}
2861 }
2862 
2863 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2864 {
2865 	struct hisi_sas_debugfs_regs *global = s->private;
2866 	struct hisi_hba *hisi_hba = global->hisi_hba;
2867 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2868 	const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2869 
2870 	hisi_sas_debugfs_print_reg(global->data,
2871 				   reg_global, s);
2872 
2873 	return 0;
2874 }
2875 
2876 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2877 {
2878 	return single_open(filp, hisi_sas_debugfs_global_show,
2879 			   inode->i_private);
2880 }
2881 
2882 static const struct file_operations hisi_sas_debugfs_global_fops = {
2883 	.open = hisi_sas_debugfs_global_open,
2884 	.read = seq_read,
2885 	.llseek = seq_lseek,
2886 	.release = single_release,
2887 	.owner = THIS_MODULE,
2888 };
2889 
2890 static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p)
2891 {
2892 	struct hisi_sas_debugfs_regs *axi = s->private;
2893 	struct hisi_hba *hisi_hba = axi->hisi_hba;
2894 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2895 	const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI];
2896 
2897 	hisi_sas_debugfs_print_reg(axi->data,
2898 				   reg_axi, s);
2899 
2900 	return 0;
2901 }
2902 
2903 static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp)
2904 {
2905 	return single_open(filp, hisi_sas_debugfs_axi_show,
2906 			   inode->i_private);
2907 }
2908 
2909 static const struct file_operations hisi_sas_debugfs_axi_fops = {
2910 	.open = hisi_sas_debugfs_axi_open,
2911 	.read = seq_read,
2912 	.llseek = seq_lseek,
2913 	.release = single_release,
2914 	.owner = THIS_MODULE,
2915 };
2916 
2917 static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p)
2918 {
2919 	struct hisi_sas_debugfs_regs *ras = s->private;
2920 	struct hisi_hba *hisi_hba = ras->hisi_hba;
2921 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2922 	const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS];
2923 
2924 	hisi_sas_debugfs_print_reg(ras->data,
2925 				   reg_ras, s);
2926 
2927 	return 0;
2928 }
2929 
2930 static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp)
2931 {
2932 	return single_open(filp, hisi_sas_debugfs_ras_show,
2933 			   inode->i_private);
2934 }
2935 
2936 static const struct file_operations hisi_sas_debugfs_ras_fops = {
2937 	.open = hisi_sas_debugfs_ras_open,
2938 	.read = seq_read,
2939 	.llseek = seq_lseek,
2940 	.release = single_release,
2941 	.owner = THIS_MODULE,
2942 };
2943 
2944 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2945 {
2946 	struct hisi_sas_debugfs_port *port = s->private;
2947 	struct hisi_sas_phy *phy = port->phy;
2948 	struct hisi_hba *hisi_hba = phy->hisi_hba;
2949 	const struct hisi_sas_hw *hw = hisi_hba->hw;
2950 	const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2951 
2952 	hisi_sas_debugfs_print_reg(port->data, reg_port, s);
2953 
2954 	return 0;
2955 }
2956 
2957 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2958 {
2959 	return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2960 }
2961 
2962 static const struct file_operations hisi_sas_debugfs_port_fops = {
2963 	.open = hisi_sas_debugfs_port_open,
2964 	.read = seq_read,
2965 	.llseek = seq_lseek,
2966 	.release = single_release,
2967 	.owner = THIS_MODULE,
2968 };
2969 
2970 static void hisi_sas_show_row_64(struct seq_file *s, int index,
2971 				 int sz, __le64 *ptr)
2972 {
2973 	int i;
2974 
2975 	/* completion header size not fixed per HW version */
2976 	seq_printf(s, "index %04d:\n\t", index);
2977 	for (i = 1; i <= sz / 8; i++, ptr++) {
2978 		seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2979 		if (!(i % 2))
2980 			seq_puts(s, "\n\t");
2981 	}
2982 
2983 	seq_puts(s, "\n");
2984 }
2985 
2986 static void hisi_sas_show_row_32(struct seq_file *s, int index,
2987 				 int sz, __le32 *ptr)
2988 {
2989 	int i;
2990 
2991 	/* completion header size not fixed per HW version */
2992 	seq_printf(s, "index %04d:\n\t", index);
2993 	for (i = 1; i <= sz / 4; i++, ptr++) {
2994 		seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
2995 		if (!(i % 4))
2996 			seq_puts(s, "\n\t");
2997 	}
2998 	seq_puts(s, "\n");
2999 }
3000 
3001 static void hisi_sas_cq_show_slot(struct seq_file *s, int slot,
3002 				  struct hisi_sas_debugfs_cq *debugfs_cq)
3003 {
3004 	struct hisi_sas_cq *cq = debugfs_cq->cq;
3005 	struct hisi_hba *hisi_hba = cq->hisi_hba;
3006 	__le32 *complete_hdr = debugfs_cq->complete_hdr +
3007 			       (hisi_hba->hw->complete_hdr_size * slot);
3008 
3009 	hisi_sas_show_row_32(s, slot,
3010 			     hisi_hba->hw->complete_hdr_size,
3011 			     complete_hdr);
3012 }
3013 
3014 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
3015 {
3016 	struct hisi_sas_debugfs_cq *debugfs_cq = s->private;
3017 	int slot;
3018 
3019 	for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3020 		hisi_sas_cq_show_slot(s, slot, debugfs_cq);
3021 	}
3022 	return 0;
3023 }
3024 
3025 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
3026 {
3027 	return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
3028 }
3029 
3030 static const struct file_operations hisi_sas_debugfs_cq_fops = {
3031 	.open = hisi_sas_debugfs_cq_open,
3032 	.read = seq_read,
3033 	.llseek = seq_lseek,
3034 	.release = single_release,
3035 	.owner = THIS_MODULE,
3036 };
3037 
3038 static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
3039 {
3040 	struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr;
3041 	void *cmd_queue = debugfs_dq->hdr;
3042 	__le32 *cmd_hdr = cmd_queue +
3043 		sizeof(struct hisi_sas_cmd_hdr) * slot;
3044 
3045 	hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr);
3046 }
3047 
3048 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
3049 {
3050 	int slot;
3051 
3052 	for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3053 		hisi_sas_dq_show_slot(s, slot, s->private);
3054 	}
3055 	return 0;
3056 }
3057 
3058 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
3059 {
3060 	return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
3061 }
3062 
3063 static const struct file_operations hisi_sas_debugfs_dq_fops = {
3064 	.open = hisi_sas_debugfs_dq_open,
3065 	.read = seq_read,
3066 	.llseek = seq_lseek,
3067 	.release = single_release,
3068 	.owner = THIS_MODULE,
3069 };
3070 
3071 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
3072 {
3073 	struct hisi_sas_debugfs_iost *debugfs_iost = s->private;
3074 	struct hisi_sas_iost *iost = debugfs_iost->iost;
3075 	int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
3076 
3077 	for (i = 0; i < max_command_entries; i++, iost++) {
3078 		__le64 *data = &iost->qw0;
3079 
3080 		hisi_sas_show_row_64(s, i, sizeof(*iost), data);
3081 	}
3082 
3083 	return 0;
3084 }
3085 
3086 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
3087 {
3088 	return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
3089 }
3090 
3091 static const struct file_operations hisi_sas_debugfs_iost_fops = {
3092 	.open = hisi_sas_debugfs_iost_open,
3093 	.read = seq_read,
3094 	.llseek = seq_lseek,
3095 	.release = single_release,
3096 	.owner = THIS_MODULE,
3097 };
3098 
3099 static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p)
3100 {
3101 	struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private;
3102 	struct hisi_sas_iost_itct_cache *iost_cache = debugfs_iost_cache->cache;
3103 	u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3104 	int i, tab_idx;
3105 	__le64 *iost;
3106 
3107 	for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
3108 		/*
3109 		 * Data struct of IOST cache:
3110 		 * Data[1]: BIT0~15: Table index
3111 		 *	    Bit16:   Valid mask
3112 		 * Data[2]~[9]: IOST table
3113 		 */
3114 		tab_idx = (iost_cache->data[1] & 0xffff);
3115 		iost = (__le64 *)iost_cache;
3116 
3117 		hisi_sas_show_row_64(s, tab_idx, cache_size, iost);
3118 	}
3119 
3120 	return 0;
3121 }
3122 
3123 static int hisi_sas_debugfs_iost_cache_open(struct inode *inode,
3124 					    struct file *filp)
3125 {
3126 	return single_open(filp, hisi_sas_debugfs_iost_cache_show,
3127 			   inode->i_private);
3128 }
3129 
3130 static const struct file_operations hisi_sas_debugfs_iost_cache_fops = {
3131 	.open = hisi_sas_debugfs_iost_cache_open,
3132 	.read = seq_read,
3133 	.llseek = seq_lseek,
3134 	.release = single_release,
3135 	.owner = THIS_MODULE,
3136 };
3137 
3138 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
3139 {
3140 	int i;
3141 	struct hisi_sas_debugfs_itct *debugfs_itct = s->private;
3142 	struct hisi_sas_itct *itct = debugfs_itct->itct;
3143 
3144 	for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
3145 		__le64 *data = &itct->qw0;
3146 
3147 		hisi_sas_show_row_64(s, i, sizeof(*itct), data);
3148 	}
3149 
3150 	return 0;
3151 }
3152 
3153 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
3154 {
3155 	return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
3156 }
3157 
3158 static const struct file_operations hisi_sas_debugfs_itct_fops = {
3159 	.open = hisi_sas_debugfs_itct_open,
3160 	.read = seq_read,
3161 	.llseek = seq_lseek,
3162 	.release = single_release,
3163 	.owner = THIS_MODULE,
3164 };
3165 
3166 static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p)
3167 {
3168 	struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private;
3169 	struct hisi_sas_iost_itct_cache *itct_cache = debugfs_itct_cache->cache;
3170 	u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3171 	int i, tab_idx;
3172 	__le64 *itct;
3173 
3174 	for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
3175 		/*
3176 		 * Data struct of ITCT cache:
3177 		 * Data[1]: BIT0~15: Table index
3178 		 *	    Bit16:   Valid mask
3179 		 * Data[2]~[9]: ITCT table
3180 		 */
3181 		tab_idx = itct_cache->data[1] & 0xffff;
3182 		itct = (__le64 *)itct_cache;
3183 
3184 		hisi_sas_show_row_64(s, tab_idx, cache_size, itct);
3185 	}
3186 
3187 	return 0;
3188 }
3189 
3190 static int hisi_sas_debugfs_itct_cache_open(struct inode *inode,
3191 					    struct file *filp)
3192 {
3193 	return single_open(filp, hisi_sas_debugfs_itct_cache_show,
3194 			   inode->i_private);
3195 }
3196 
3197 static const struct file_operations hisi_sas_debugfs_itct_cache_fops = {
3198 	.open = hisi_sas_debugfs_itct_cache_open,
3199 	.read = seq_read,
3200 	.llseek = seq_lseek,
3201 	.release = single_release,
3202 	.owner = THIS_MODULE,
3203 };
3204 
3205 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
3206 {
3207 	u64 *debugfs_timestamp;
3208 	int dump_index = hisi_hba->debugfs_dump_index;
3209 	struct dentry *dump_dentry;
3210 	struct dentry *dentry;
3211 	char name[256];
3212 	int p;
3213 	int c;
3214 	int d;
3215 
3216 	snprintf(name, 256, "%d", dump_index);
3217 
3218 	dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry);
3219 
3220 	debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index];
3221 
3222 	debugfs_create_u64("timestamp", 0400, dump_dentry,
3223 			   debugfs_timestamp);
3224 
3225 	debugfs_create_file("global", 0400, dump_dentry,
3226 			   &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL],
3227 			   &hisi_sas_debugfs_global_fops);
3228 
3229 	/* Create port dir and files */
3230 	dentry = debugfs_create_dir("port", dump_dentry);
3231 	for (p = 0; p < hisi_hba->n_phy; p++) {
3232 		snprintf(name, 256, "%d", p);
3233 
3234 		debugfs_create_file(name, 0400, dentry,
3235 				    &hisi_hba->debugfs_port_reg[dump_index][p],
3236 				    &hisi_sas_debugfs_port_fops);
3237 	}
3238 
3239 	/* Create CQ dir and files */
3240 	dentry = debugfs_create_dir("cq", dump_dentry);
3241 	for (c = 0; c < hisi_hba->queue_count; c++) {
3242 		snprintf(name, 256, "%d", c);
3243 
3244 		debugfs_create_file(name, 0400, dentry,
3245 				    &hisi_hba->debugfs_cq[dump_index][c],
3246 				    &hisi_sas_debugfs_cq_fops);
3247 	}
3248 
3249 	/* Create DQ dir and files */
3250 	dentry = debugfs_create_dir("dq", dump_dentry);
3251 	for (d = 0; d < hisi_hba->queue_count; d++) {
3252 		snprintf(name, 256, "%d", d);
3253 
3254 		debugfs_create_file(name, 0400, dentry,
3255 				    &hisi_hba->debugfs_dq[dump_index][d],
3256 				    &hisi_sas_debugfs_dq_fops);
3257 	}
3258 
3259 	debugfs_create_file("iost", 0400, dump_dentry,
3260 			    &hisi_hba->debugfs_iost[dump_index],
3261 			    &hisi_sas_debugfs_iost_fops);
3262 
3263 	debugfs_create_file("iost_cache", 0400, dump_dentry,
3264 			    &hisi_hba->debugfs_iost_cache[dump_index],
3265 			    &hisi_sas_debugfs_iost_cache_fops);
3266 
3267 	debugfs_create_file("itct", 0400, dump_dentry,
3268 			    &hisi_hba->debugfs_itct[dump_index],
3269 			    &hisi_sas_debugfs_itct_fops);
3270 
3271 	debugfs_create_file("itct_cache", 0400, dump_dentry,
3272 			    &hisi_hba->debugfs_itct_cache[dump_index],
3273 			    &hisi_sas_debugfs_itct_cache_fops);
3274 
3275 	debugfs_create_file("axi", 0400, dump_dentry,
3276 			    &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI],
3277 			    &hisi_sas_debugfs_axi_fops);
3278 
3279 	debugfs_create_file("ras", 0400, dump_dentry,
3280 			    &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS],
3281 			    &hisi_sas_debugfs_ras_fops);
3282 
3283 	return;
3284 }
3285 
3286 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
3287 {
3288 	hisi_hba->hw->snapshot_prepare(hisi_hba);
3289 
3290 	hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
3291 	hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
3292 	hisi_sas_debugfs_snapshot_axi_reg(hisi_hba);
3293 	hisi_sas_debugfs_snapshot_ras_reg(hisi_hba);
3294 	hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
3295 	hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
3296 	hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
3297 	hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
3298 
3299 	hisi_sas_debugfs_create_files(hisi_hba);
3300 
3301 	hisi_hba->hw->snapshot_restore(hisi_hba);
3302 }
3303 
3304 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
3305 						   const char __user *user_buf,
3306 						   size_t count, loff_t *ppos)
3307 {
3308 	struct hisi_hba *hisi_hba = file->f_inode->i_private;
3309 	char buf[8];
3310 
3311 	if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count)
3312 		return -EFAULT;
3313 
3314 	if (count > 8)
3315 		return -EFAULT;
3316 
3317 	if (copy_from_user(buf, user_buf, count))
3318 		return -EFAULT;
3319 
3320 	if (buf[0] != '1')
3321 		return -EFAULT;
3322 
3323 	queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3324 
3325 	return count;
3326 }
3327 
3328 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
3329 	.write = &hisi_sas_debugfs_trigger_dump_write,
3330 	.owner = THIS_MODULE,
3331 };
3332 
3333 enum {
3334 	HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
3335 	HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
3336 	HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
3337 };
3338 
3339 enum {
3340 	HISI_SAS_BIST_CODE_MODE_PRBS7 = 0,
3341 	HISI_SAS_BIST_CODE_MODE_PRBS23,
3342 	HISI_SAS_BIST_CODE_MODE_PRBS31,
3343 	HISI_SAS_BIST_CODE_MODE_JTPAT,
3344 	HISI_SAS_BIST_CODE_MODE_CJTPAT,
3345 	HISI_SAS_BIST_CODE_MODE_SCRAMBED_0,
3346 	HISI_SAS_BIST_CODE_MODE_TRAIN,
3347 	HISI_SAS_BIST_CODE_MODE_TRAIN_DONE,
3348 	HISI_SAS_BIST_CODE_MODE_HFTP,
3349 	HISI_SAS_BIST_CODE_MODE_MFTP,
3350 	HISI_SAS_BIST_CODE_MODE_LFTP,
3351 	HISI_SAS_BIST_CODE_MODE_FIXED_DATA,
3352 };
3353 
3354 static const struct {
3355 	int		value;
3356 	char		*name;
3357 } hisi_sas_debugfs_loop_linkrate[] = {
3358 	{ SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
3359 	{ SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
3360 	{ SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
3361 	{ SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
3362 };
3363 
3364 static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p)
3365 {
3366 	struct hisi_hba *hisi_hba = s->private;
3367 	int i;
3368 
3369 	for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3370 		int match = (hisi_hba->debugfs_bist_linkrate ==
3371 			     hisi_sas_debugfs_loop_linkrate[i].value);
3372 
3373 		seq_printf(s, "%s%s%s ", match ? "[" : "",
3374 			   hisi_sas_debugfs_loop_linkrate[i].name,
3375 			   match ? "]" : "");
3376 	}
3377 	seq_puts(s, "\n");
3378 
3379 	return 0;
3380 }
3381 
3382 static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp,
3383 						    const char __user *buf,
3384 						    size_t count, loff_t *ppos)
3385 {
3386 	struct seq_file *m = filp->private_data;
3387 	struct hisi_hba *hisi_hba = m->private;
3388 	char kbuf[16] = {}, *pkbuf;
3389 	bool found = false;
3390 	int i;
3391 
3392 	if (hisi_hba->debugfs_bist_enable)
3393 		return -EPERM;
3394 
3395 	if (count >= sizeof(kbuf))
3396 		return -EOVERFLOW;
3397 
3398 	if (copy_from_user(kbuf, buf, count))
3399 		return -EINVAL;
3400 
3401 	pkbuf = strstrip(kbuf);
3402 
3403 	for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3404 		if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name,
3405 			     pkbuf, 16)) {
3406 			hisi_hba->debugfs_bist_linkrate =
3407 				hisi_sas_debugfs_loop_linkrate[i].value;
3408 			found = true;
3409 			break;
3410 		}
3411 	}
3412 
3413 	if (!found)
3414 		return -EINVAL;
3415 
3416 	return count;
3417 }
3418 
3419 static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode,
3420 					       struct file *filp)
3421 {
3422 	return single_open(filp, hisi_sas_debugfs_bist_linkrate_show,
3423 			   inode->i_private);
3424 }
3425 
3426 static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = {
3427 	.open = hisi_sas_debugfs_bist_linkrate_open,
3428 	.read = seq_read,
3429 	.write = hisi_sas_debugfs_bist_linkrate_write,
3430 	.llseek = seq_lseek,
3431 	.release = single_release,
3432 	.owner = THIS_MODULE,
3433 };
3434 
3435 static const struct {
3436 	int		value;
3437 	char		*name;
3438 } hisi_sas_debugfs_loop_code_mode[] = {
3439 	{ HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
3440 	{ HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
3441 	{ HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
3442 	{ HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
3443 	{ HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
3444 	{ HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
3445 	{ HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
3446 	{ HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
3447 	{ HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
3448 	{ HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
3449 	{ HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
3450 	{ HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
3451 };
3452 
3453 static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p)
3454 {
3455 	struct hisi_hba *hisi_hba = s->private;
3456 	int i;
3457 
3458 	for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3459 		int match = (hisi_hba->debugfs_bist_code_mode ==
3460 			     hisi_sas_debugfs_loop_code_mode[i].value);
3461 
3462 		seq_printf(s, "%s%s%s ", match ? "[" : "",
3463 			   hisi_sas_debugfs_loop_code_mode[i].name,
3464 			   match ? "]" : "");
3465 	}
3466 	seq_puts(s, "\n");
3467 
3468 	return 0;
3469 }
3470 
3471 static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp,
3472 						     const char __user *buf,
3473 						     size_t count,
3474 						     loff_t *ppos)
3475 {
3476 	struct seq_file *m = filp->private_data;
3477 	struct hisi_hba *hisi_hba = m->private;
3478 	char kbuf[16] = {}, *pkbuf;
3479 	bool found = false;
3480 	int i;
3481 
3482 	if (hisi_hba->debugfs_bist_enable)
3483 		return -EPERM;
3484 
3485 	if (count >= sizeof(kbuf))
3486 		return -EINVAL;
3487 
3488 	if (copy_from_user(kbuf, buf, count))
3489 		return -EOVERFLOW;
3490 
3491 	pkbuf = strstrip(kbuf);
3492 
3493 	for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3494 		if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name,
3495 			     pkbuf, 16)) {
3496 			hisi_hba->debugfs_bist_code_mode =
3497 				hisi_sas_debugfs_loop_code_mode[i].value;
3498 			found = true;
3499 			break;
3500 		}
3501 	}
3502 
3503 	if (!found)
3504 		return -EINVAL;
3505 
3506 	return count;
3507 }
3508 
3509 static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode,
3510 						struct file *filp)
3511 {
3512 	return single_open(filp, hisi_sas_debugfs_bist_code_mode_show,
3513 			   inode->i_private);
3514 }
3515 
3516 static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = {
3517 	.open = hisi_sas_debugfs_bist_code_mode_open,
3518 	.read = seq_read,
3519 	.write = hisi_sas_debugfs_bist_code_mode_write,
3520 	.llseek = seq_lseek,
3521 	.release = single_release,
3522 	.owner = THIS_MODULE,
3523 };
3524 
3525 static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp,
3526 					       const char __user *buf,
3527 					       size_t count, loff_t *ppos)
3528 {
3529 	struct seq_file *m = filp->private_data;
3530 	struct hisi_hba *hisi_hba = m->private;
3531 	unsigned int phy_no;
3532 	int val;
3533 
3534 	if (hisi_hba->debugfs_bist_enable)
3535 		return -EPERM;
3536 
3537 	val = kstrtouint_from_user(buf, count, 0, &phy_no);
3538 	if (val)
3539 		return val;
3540 
3541 	if (phy_no >= hisi_hba->n_phy)
3542 		return -EINVAL;
3543 
3544 	hisi_hba->debugfs_bist_phy_no = phy_no;
3545 
3546 	return count;
3547 }
3548 
3549 static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p)
3550 {
3551 	struct hisi_hba *hisi_hba = s->private;
3552 
3553 	seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
3554 
3555 	return 0;
3556 }
3557 
3558 static int hisi_sas_debugfs_bist_phy_open(struct inode *inode,
3559 					  struct file *filp)
3560 {
3561 	return single_open(filp, hisi_sas_debugfs_bist_phy_show,
3562 			   inode->i_private);
3563 }
3564 
3565 static const struct file_operations hisi_sas_debugfs_bist_phy_ops = {
3566 	.open = hisi_sas_debugfs_bist_phy_open,
3567 	.read = seq_read,
3568 	.write = hisi_sas_debugfs_bist_phy_write,
3569 	.llseek = seq_lseek,
3570 	.release = single_release,
3571 	.owner = THIS_MODULE,
3572 };
3573 
3574 static const struct {
3575 	int		value;
3576 	char		*name;
3577 } hisi_sas_debugfs_loop_modes[] = {
3578 	{ HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" },
3579 	{ HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
3580 	{ HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
3581 };
3582 
3583 static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p)
3584 {
3585 	struct hisi_hba *hisi_hba = s->private;
3586 	int i;
3587 
3588 	for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3589 		int match = (hisi_hba->debugfs_bist_mode ==
3590 			     hisi_sas_debugfs_loop_modes[i].value);
3591 
3592 		seq_printf(s, "%s%s%s ", match ? "[" : "",
3593 			   hisi_sas_debugfs_loop_modes[i].name,
3594 			   match ? "]" : "");
3595 	}
3596 	seq_puts(s, "\n");
3597 
3598 	return 0;
3599 }
3600 
3601 static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp,
3602 						const char __user *buf,
3603 						size_t count, loff_t *ppos)
3604 {
3605 	struct seq_file *m = filp->private_data;
3606 	struct hisi_hba *hisi_hba = m->private;
3607 	char kbuf[16] = {}, *pkbuf;
3608 	bool found = false;
3609 	int i;
3610 
3611 	if (hisi_hba->debugfs_bist_enable)
3612 		return -EPERM;
3613 
3614 	if (count >= sizeof(kbuf))
3615 		return -EINVAL;
3616 
3617 	if (copy_from_user(kbuf, buf, count))
3618 		return -EOVERFLOW;
3619 
3620 	pkbuf = strstrip(kbuf);
3621 
3622 	for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3623 		if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) {
3624 			hisi_hba->debugfs_bist_mode =
3625 				hisi_sas_debugfs_loop_modes[i].value;
3626 			found = true;
3627 			break;
3628 		}
3629 	}
3630 
3631 	if (!found)
3632 		return -EINVAL;
3633 
3634 	return count;
3635 }
3636 
3637 static int hisi_sas_debugfs_bist_mode_open(struct inode *inode,
3638 					   struct file *filp)
3639 {
3640 	return single_open(filp, hisi_sas_debugfs_bist_mode_show,
3641 			   inode->i_private);
3642 }
3643 
3644 static const struct file_operations hisi_sas_debugfs_bist_mode_ops = {
3645 	.open = hisi_sas_debugfs_bist_mode_open,
3646 	.read = seq_read,
3647 	.write = hisi_sas_debugfs_bist_mode_write,
3648 	.llseek = seq_lseek,
3649 	.release = single_release,
3650 	.owner = THIS_MODULE,
3651 };
3652 
3653 static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp,
3654 						  const char __user *buf,
3655 						  size_t count, loff_t *ppos)
3656 {
3657 	struct seq_file *m = filp->private_data;
3658 	struct hisi_hba *hisi_hba = m->private;
3659 	unsigned int enable;
3660 	int val;
3661 
3662 	val = kstrtouint_from_user(buf, count, 0, &enable);
3663 	if (val)
3664 		return val;
3665 
3666 	if (enable > 1)
3667 		return -EINVAL;
3668 
3669 	if (enable == hisi_hba->debugfs_bist_enable)
3670 		return count;
3671 
3672 	if (!hisi_hba->hw->set_bist)
3673 		return -EPERM;
3674 
3675 	val = hisi_hba->hw->set_bist(hisi_hba, enable);
3676 	if (val < 0)
3677 		return val;
3678 
3679 	hisi_hba->debugfs_bist_enable = enable;
3680 
3681 	return count;
3682 }
3683 
3684 static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p)
3685 {
3686 	struct hisi_hba *hisi_hba = s->private;
3687 
3688 	seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
3689 
3690 	return 0;
3691 }
3692 
3693 static int hisi_sas_debugfs_bist_enable_open(struct inode *inode,
3694 					     struct file *filp)
3695 {
3696 	return single_open(filp, hisi_sas_debugfs_bist_enable_show,
3697 			   inode->i_private);
3698 }
3699 
3700 static const struct file_operations hisi_sas_debugfs_bist_enable_ops = {
3701 	.open = hisi_sas_debugfs_bist_enable_open,
3702 	.read = seq_read,
3703 	.write = hisi_sas_debugfs_bist_enable_write,
3704 	.llseek = seq_lseek,
3705 	.release = single_release,
3706 	.owner = THIS_MODULE,
3707 };
3708 
3709 static ssize_t hisi_sas_debugfs_phy_down_cnt_write(struct file *filp,
3710 						   const char __user *buf,
3711 						   size_t count, loff_t *ppos)
3712 {
3713 	struct seq_file *s = filp->private_data;
3714 	struct hisi_sas_phy *phy = s->private;
3715 	unsigned int set_val;
3716 	int res;
3717 
3718 	res = kstrtouint_from_user(buf, count, 0, &set_val);
3719 	if (res)
3720 		return res;
3721 
3722 	if (set_val > 0)
3723 		return -EINVAL;
3724 
3725 	atomic_set(&phy->down_cnt, 0);
3726 
3727 	return count;
3728 }
3729 
3730 static int hisi_sas_debugfs_phy_down_cnt_show(struct seq_file *s, void *p)
3731 {
3732 	struct hisi_sas_phy *phy = s->private;
3733 
3734 	seq_printf(s, "%d\n", atomic_read(&phy->down_cnt));
3735 
3736 	return 0;
3737 }
3738 
3739 static int hisi_sas_debugfs_phy_down_cnt_open(struct inode *inode,
3740 					      struct file *filp)
3741 {
3742 	return single_open(filp, hisi_sas_debugfs_phy_down_cnt_show,
3743 			   inode->i_private);
3744 }
3745 
3746 static const struct file_operations hisi_sas_debugfs_phy_down_cnt_ops = {
3747 	.open = hisi_sas_debugfs_phy_down_cnt_open,
3748 	.read = seq_read,
3749 	.write = hisi_sas_debugfs_phy_down_cnt_write,
3750 	.llseek = seq_lseek,
3751 	.release = single_release,
3752 	.owner = THIS_MODULE,
3753 };
3754 
3755 void hisi_sas_debugfs_work_handler(struct work_struct *work)
3756 {
3757 	struct hisi_hba *hisi_hba =
3758 		container_of(work, struct hisi_hba, debugfs_work);
3759 	int debugfs_dump_index = hisi_hba->debugfs_dump_index;
3760 	struct device *dev = hisi_hba->dev;
3761 	u64 timestamp = local_clock();
3762 
3763 	if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) {
3764 		dev_warn(dev, "dump count exceeded!\n");
3765 		return;
3766 	}
3767 
3768 	do_div(timestamp, NSEC_PER_MSEC);
3769 	hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp;
3770 
3771 	hisi_sas_debugfs_snapshot_regs(hisi_hba);
3772 	hisi_hba->debugfs_dump_index++;
3773 }
3774 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
3775 
3776 static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba, int dump_index)
3777 {
3778 	struct device *dev = hisi_hba->dev;
3779 	int i;
3780 
3781 	devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache);
3782 	devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache);
3783 	devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost);
3784 	devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct);
3785 
3786 	for (i = 0; i < hisi_hba->queue_count; i++)
3787 		devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr);
3788 
3789 	for (i = 0; i < hisi_hba->queue_count; i++)
3790 		devm_kfree(dev,
3791 			   hisi_hba->debugfs_cq[dump_index][i].complete_hdr);
3792 
3793 	for (i = 0; i < DEBUGFS_REGS_NUM; i++)
3794 		devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data);
3795 
3796 	for (i = 0; i < hisi_hba->n_phy; i++)
3797 		devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data);
3798 }
3799 
3800 static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba, int dump_index)
3801 {
3802 	const struct hisi_sas_hw *hw = hisi_hba->hw;
3803 	struct device *dev = hisi_hba->dev;
3804 	int p, c, d, r, i;
3805 	size_t sz;
3806 
3807 	for (r = 0; r < DEBUGFS_REGS_NUM; r++) {
3808 		struct hisi_sas_debugfs_regs *regs =
3809 				&hisi_hba->debugfs_regs[dump_index][r];
3810 
3811 		sz = hw->debugfs_reg_array[r]->count * 4;
3812 		regs->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3813 		if (!regs->data)
3814 			goto fail;
3815 		regs->hisi_hba = hisi_hba;
3816 	}
3817 
3818 	sz = hw->debugfs_reg_port->count * 4;
3819 	for (p = 0; p < hisi_hba->n_phy; p++) {
3820 		struct hisi_sas_debugfs_port *port =
3821 				&hisi_hba->debugfs_port_reg[dump_index][p];
3822 
3823 		port->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3824 		if (!port->data)
3825 			goto fail;
3826 		port->phy = &hisi_hba->phy[p];
3827 	}
3828 
3829 	sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
3830 	for (c = 0; c < hisi_hba->queue_count; c++) {
3831 		struct hisi_sas_debugfs_cq *cq =
3832 				&hisi_hba->debugfs_cq[dump_index][c];
3833 
3834 		cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3835 		if (!cq->complete_hdr)
3836 			goto fail;
3837 		cq->cq = &hisi_hba->cq[c];
3838 	}
3839 
3840 	sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
3841 	for (d = 0; d < hisi_hba->queue_count; d++) {
3842 		struct hisi_sas_debugfs_dq *dq =
3843 				&hisi_hba->debugfs_dq[dump_index][d];
3844 
3845 		dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3846 		if (!dq->hdr)
3847 			goto fail;
3848 		dq->dq = &hisi_hba->dq[d];
3849 	}
3850 
3851 	sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
3852 
3853 	hisi_hba->debugfs_iost[dump_index].iost =
3854 				devm_kmalloc(dev, sz, GFP_KERNEL);
3855 	if (!hisi_hba->debugfs_iost[dump_index].iost)
3856 		goto fail;
3857 
3858 	sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3859 	     sizeof(struct hisi_sas_iost_itct_cache);
3860 
3861 	hisi_hba->debugfs_iost_cache[dump_index].cache =
3862 				devm_kmalloc(dev, sz, GFP_KERNEL);
3863 	if (!hisi_hba->debugfs_iost_cache[dump_index].cache)
3864 		goto fail;
3865 
3866 	sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3867 	     sizeof(struct hisi_sas_iost_itct_cache);
3868 
3869 	hisi_hba->debugfs_itct_cache[dump_index].cache =
3870 				devm_kmalloc(dev, sz, GFP_KERNEL);
3871 	if (!hisi_hba->debugfs_itct_cache[dump_index].cache)
3872 		goto fail;
3873 
3874 	/* New memory allocation must be locate before itct */
3875 	sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
3876 
3877 	hisi_hba->debugfs_itct[dump_index].itct =
3878 				devm_kmalloc(dev, sz, GFP_KERNEL);
3879 	if (!hisi_hba->debugfs_itct[dump_index].itct)
3880 		goto fail;
3881 
3882 	return 0;
3883 fail:
3884 	for (i = 0; i < hisi_sas_debugfs_dump_count; i++)
3885 		hisi_sas_debugfs_release(hisi_hba, i);
3886 	return -ENOMEM;
3887 }
3888 
3889 static void hisi_sas_debugfs_phy_down_cnt_init(struct hisi_hba *hisi_hba)
3890 {
3891 	struct dentry *dir = debugfs_create_dir("phy_down_cnt",
3892 						hisi_hba->debugfs_dir);
3893 	char name[16];
3894 	int phy_no;
3895 
3896 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3897 		snprintf(name, 16, "%d", phy_no);
3898 		debugfs_create_file(name, 0600, dir,
3899 				    &hisi_hba->phy[phy_no],
3900 				    &hisi_sas_debugfs_phy_down_cnt_ops);
3901 	}
3902 }
3903 
3904 static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba)
3905 {
3906 	hisi_hba->debugfs_bist_dentry =
3907 			debugfs_create_dir("bist", hisi_hba->debugfs_dir);
3908 	debugfs_create_file("link_rate", 0600,
3909 			    hisi_hba->debugfs_bist_dentry, hisi_hba,
3910 			    &hisi_sas_debugfs_bist_linkrate_ops);
3911 
3912 	debugfs_create_file("code_mode", 0600,
3913 			    hisi_hba->debugfs_bist_dentry, hisi_hba,
3914 			    &hisi_sas_debugfs_bist_code_mode_ops);
3915 
3916 	debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
3917 			    hisi_hba, &hisi_sas_debugfs_bist_phy_ops);
3918 
3919 	debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
3920 			   &hisi_hba->debugfs_bist_cnt);
3921 
3922 	debugfs_create_file("loopback_mode", 0600,
3923 			    hisi_hba->debugfs_bist_dentry,
3924 			    hisi_hba, &hisi_sas_debugfs_bist_mode_ops);
3925 
3926 	debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
3927 			    hisi_hba, &hisi_sas_debugfs_bist_enable_ops);
3928 
3929 	hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
3930 }
3931 
3932 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
3933 {
3934 	struct device *dev = hisi_hba->dev;
3935 	int i;
3936 
3937 	hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
3938 						   hisi_sas_debugfs_dir);
3939 	debugfs_create_file("trigger_dump", 0600,
3940 			    hisi_hba->debugfs_dir,
3941 			    hisi_hba,
3942 			    &hisi_sas_debugfs_trigger_dump_fops);
3943 
3944 	/* create bist structures */
3945 	hisi_sas_debugfs_bist_init(hisi_hba);
3946 
3947 	hisi_hba->debugfs_dump_dentry =
3948 			debugfs_create_dir("dump", hisi_hba->debugfs_dir);
3949 
3950 	hisi_sas_debugfs_phy_down_cnt_init(hisi_hba);
3951 
3952 	for (i = 0; i < hisi_sas_debugfs_dump_count; i++) {
3953 		if (hisi_sas_debugfs_alloc(hisi_hba, i)) {
3954 			debugfs_remove_recursive(hisi_hba->debugfs_dir);
3955 			dev_dbg(dev, "failed to init debugfs!\n");
3956 			break;
3957 		}
3958 	}
3959 }
3960 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
3961 
3962 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
3963 {
3964 	debugfs_remove_recursive(hisi_hba->debugfs_dir);
3965 }
3966 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
3967 
3968 int hisi_sas_remove(struct platform_device *pdev)
3969 {
3970 	struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3971 	struct hisi_hba *hisi_hba = sha->lldd_ha;
3972 	struct Scsi_Host *shost = sha->core.shost;
3973 
3974 	if (timer_pending(&hisi_hba->timer))
3975 		del_timer(&hisi_hba->timer);
3976 
3977 	sas_unregister_ha(sha);
3978 	sas_remove_host(sha->core.shost);
3979 
3980 	hisi_sas_free(hisi_hba);
3981 	scsi_host_put(shost);
3982 	return 0;
3983 }
3984 EXPORT_SYMBOL_GPL(hisi_sas_remove);
3985 
3986 bool hisi_sas_debugfs_enable;
3987 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
3988 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
3989 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
3990 
3991 u32 hisi_sas_debugfs_dump_count = 1;
3992 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
3993 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
3994 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
3995 
3996 static __init int hisi_sas_init(void)
3997 {
3998 	hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
3999 	if (!hisi_sas_stt)
4000 		return -ENOMEM;
4001 
4002 	if (hisi_sas_debugfs_enable) {
4003 		hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
4004 		if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
4005 			pr_info("hisi_sas: Limiting debugfs dump count\n");
4006 			hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
4007 		}
4008 	}
4009 
4010 	return 0;
4011 }
4012 
4013 static __exit void hisi_sas_exit(void)
4014 {
4015 	sas_release_transport(hisi_sas_stt);
4016 
4017 	debugfs_remove(hisi_sas_debugfs_dir);
4018 }
4019 
4020 module_init(hisi_sas_init);
4021 module_exit(hisi_sas_exit);
4022 
4023 MODULE_LICENSE("GPL");
4024 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
4025 MODULE_DESCRIPTION("HISILICON SAS controller driver");
4026 MODULE_ALIAS("platform:" DRV_NAME);
4027