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