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