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