xref: /openbmc/linux/drivers/scsi/hisi_sas/hisi_sas_main.c (revision 44ad3baf1cca483e418b6aadf2d3994f69e0f16a)
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_slave_alloc(struct scsi_device * sdev)808 int hisi_sas_slave_alloc(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_slave_alloc(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_slave_alloc);
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_slave_configure(struct scsi_device * sdev)871 int hisi_sas_slave_configure(struct scsi_device *sdev)
872 {
873 	struct domain_device *dev = sdev_to_domain_dev(sdev);
874 	int ret = sas_slave_configure(sdev);
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_slave_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 	struct asd_sas_port *sas_port = sas_phy->port;
915 	struct hisi_sas_port *port = phy->port;
916 	struct device *dev = hisi_hba->dev;
917 	struct domain_device *port_dev;
918 	int phy_no = sas_phy->id;
919 
920 	if (!test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags) &&
921 	    sas_port && port && (port->id != phy->port_id)) {
922 		dev_info(dev, "phy%d's hw port id changed from %d to %llu\n",
923 				phy_no, port->id, phy->port_id);
924 		port_dev = sas_port->port_dev;
925 		if (port_dev && !dev_is_expander(port_dev->dev_type)) {
926 			/*
927 			 * Set the device state to gone to block
928 			 * sending IO to the device.
929 			 */
930 			set_bit(SAS_DEV_GONE, &port_dev->state);
931 			hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
932 			return;
933 		}
934 	}
935 
936 	phy->wait_phyup_cnt = 0;
937 	if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
938 		hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
939 	hisi_sas_bytes_dmaed(hisi_hba, phy_no, GFP_KERNEL);
940 }
941 
hisi_sas_phyup_work(struct work_struct * work)942 static void hisi_sas_phyup_work(struct work_struct *work)
943 {
944 	hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP);
945 }
946 
hisi_sas_linkreset_work(struct work_struct * work)947 static void hisi_sas_linkreset_work(struct work_struct *work)
948 {
949 	struct hisi_sas_phy *phy =
950 		container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
951 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
952 
953 	hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
954 }
955 
hisi_sas_phyup_pm_work(struct work_struct * work)956 static void hisi_sas_phyup_pm_work(struct work_struct *work)
957 {
958 	struct hisi_sas_phy *phy =
959 		container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP_PM]);
960 	struct hisi_hba *hisi_hba = phy->hisi_hba;
961 	struct device *dev = hisi_hba->dev;
962 
963 	hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP_PM);
964 	pm_runtime_put_sync(dev);
965 }
966 
967 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
968 	[HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
969 	[HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
970 	[HISI_PHYE_PHY_UP_PM] = hisi_sas_phyup_pm_work,
971 };
972 
hisi_sas_notify_phy_event(struct hisi_sas_phy * phy,enum hisi_sas_phy_event event)973 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
974 				enum hisi_sas_phy_event event)
975 {
976 	struct hisi_hba *hisi_hba = phy->hisi_hba;
977 
978 	if (WARN_ON(event >= HISI_PHYES_NUM))
979 		return false;
980 
981 	return queue_work(hisi_hba->wq, &phy->works[event]);
982 }
983 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
984 
hisi_sas_wait_phyup_timedout(struct timer_list * t)985 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
986 {
987 	struct hisi_sas_phy *phy = from_timer(phy, t, timer);
988 	struct hisi_hba *hisi_hba = phy->hisi_hba;
989 	struct device *dev = hisi_hba->dev;
990 	int phy_no = phy->sas_phy.id;
991 
992 	dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
993 	hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
994 }
995 
996 #define HISI_SAS_WAIT_PHYUP_RETRIES	10
997 
hisi_sas_phy_oob_ready(struct hisi_hba * hisi_hba,int phy_no)998 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
999 {
1000 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1001 	struct device *dev = hisi_hba->dev;
1002 	unsigned long flags;
1003 
1004 	dev_dbg(dev, "phy%d OOB ready\n", phy_no);
1005 	spin_lock_irqsave(&phy->lock, flags);
1006 	if (phy->phy_attached) {
1007 		spin_unlock_irqrestore(&phy->lock, flags);
1008 		return;
1009 	}
1010 
1011 	if (!timer_pending(&phy->timer)) {
1012 		if (phy->wait_phyup_cnt < HISI_SAS_WAIT_PHYUP_RETRIES) {
1013 			phy->wait_phyup_cnt++;
1014 			phy->timer.expires = jiffies +
1015 					     HISI_SAS_WAIT_PHYUP_TIMEOUT;
1016 			add_timer(&phy->timer);
1017 			spin_unlock_irqrestore(&phy->lock, flags);
1018 			return;
1019 		}
1020 
1021 		dev_warn(dev, "phy%d failed to come up %d times, giving up\n",
1022 			 phy_no, phy->wait_phyup_cnt);
1023 		phy->wait_phyup_cnt = 0;
1024 	}
1025 	spin_unlock_irqrestore(&phy->lock, flags);
1026 }
1027 
1028 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
1029 
hisi_sas_phy_init(struct hisi_hba * hisi_hba,int phy_no)1030 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
1031 {
1032 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1033 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1034 	int i;
1035 
1036 	phy->hisi_hba = hisi_hba;
1037 	phy->port = NULL;
1038 	phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1039 	phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
1040 	sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
1041 	sas_phy->iproto = SAS_PROTOCOL_ALL;
1042 	sas_phy->tproto = 0;
1043 	sas_phy->role = PHY_ROLE_INITIATOR;
1044 	sas_phy->oob_mode = OOB_NOT_CONNECTED;
1045 	sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
1046 	sas_phy->id = phy_no;
1047 	sas_phy->sas_addr = &hisi_hba->sas_addr[0];
1048 	sas_phy->frame_rcvd = &phy->frame_rcvd[0];
1049 	sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
1050 	sas_phy->lldd_phy = phy;
1051 
1052 	for (i = 0; i < HISI_PHYES_NUM; i++)
1053 		INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
1054 
1055 	spin_lock_init(&phy->lock);
1056 
1057 	timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
1058 }
1059 
1060 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
hisi_sas_phy_enable(struct hisi_hba * hisi_hba,int phy_no,int enable)1061 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
1062 {
1063 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1064 	struct asd_sas_phy *aphy = &phy->sas_phy;
1065 	struct sas_phy *sphy = aphy->phy;
1066 	unsigned long flags;
1067 
1068 	spin_lock_irqsave(&phy->lock, flags);
1069 
1070 	if (enable) {
1071 		/* We may have been enabled already; if so, don't touch */
1072 		if (!phy->enable)
1073 			sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1074 		hisi_hba->hw->phy_start(hisi_hba, phy_no);
1075 	} else {
1076 		sphy->negotiated_linkrate = SAS_PHY_DISABLED;
1077 		hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1078 	}
1079 	phy->enable = enable;
1080 	spin_unlock_irqrestore(&phy->lock, flags);
1081 }
1082 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
1083 
hisi_sas_port_notify_formed(struct asd_sas_phy * sas_phy)1084 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
1085 {
1086 	struct hisi_sas_phy *phy = sas_phy->lldd_phy;
1087 	struct asd_sas_port *sas_port = sas_phy->port;
1088 	struct hisi_sas_port *port;
1089 
1090 	if (!sas_port)
1091 		return;
1092 
1093 	port = to_hisi_sas_port(sas_port);
1094 	port->port_attached = 1;
1095 	port->id = phy->port_id;
1096 	phy->port = port;
1097 	sas_port->lldd_port = port;
1098 }
1099 
hisi_sas_do_release_task(struct hisi_hba * hisi_hba,struct sas_task * task,struct hisi_sas_slot * slot,bool need_lock)1100 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
1101 				     struct hisi_sas_slot *slot, bool need_lock)
1102 {
1103 	if (task) {
1104 		unsigned long flags;
1105 		struct task_status_struct *ts;
1106 
1107 		ts = &task->task_status;
1108 
1109 		ts->resp = SAS_TASK_COMPLETE;
1110 		ts->stat = SAS_ABORTED_TASK;
1111 		spin_lock_irqsave(&task->task_state_lock, flags);
1112 		task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
1113 		if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1114 			task->task_state_flags |= SAS_TASK_STATE_DONE;
1115 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1116 	}
1117 
1118 	hisi_sas_slot_task_free(hisi_hba, task, slot, need_lock);
1119 }
1120 
hisi_sas_release_task(struct hisi_hba * hisi_hba,struct domain_device * device)1121 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1122 			struct domain_device *device)
1123 {
1124 	struct hisi_sas_slot *slot, *slot2;
1125 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1126 
1127 	spin_lock(&sas_dev->lock);
1128 	list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1129 		hisi_sas_do_release_task(hisi_hba, slot->task, slot, false);
1130 
1131 	spin_unlock(&sas_dev->lock);
1132 }
1133 
hisi_sas_release_tasks(struct hisi_hba * hisi_hba)1134 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1135 {
1136 	struct hisi_sas_device *sas_dev;
1137 	struct domain_device *device;
1138 	int i;
1139 
1140 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1141 		sas_dev = &hisi_hba->devices[i];
1142 		device = sas_dev->sas_device;
1143 
1144 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1145 		    !device)
1146 			continue;
1147 
1148 		hisi_sas_release_task(hisi_hba, device);
1149 	}
1150 }
1151 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1152 
hisi_sas_dereg_device(struct hisi_hba * hisi_hba,struct domain_device * device)1153 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1154 				struct domain_device *device)
1155 {
1156 	if (hisi_hba->hw->dereg_device)
1157 		hisi_hba->hw->dereg_device(hisi_hba, device);
1158 }
1159 
1160 static int
hisi_sas_internal_task_abort_dev(struct hisi_sas_device * sas_dev,bool rst_ha_timeout)1161 hisi_sas_internal_task_abort_dev(struct hisi_sas_device *sas_dev,
1162 				 bool rst_ha_timeout)
1163 {
1164 	struct hisi_sas_internal_abort_data data = { rst_ha_timeout };
1165 	struct domain_device *device = sas_dev->sas_device;
1166 	struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1167 	int i, rc;
1168 
1169 	for (i = 0; i < hisi_hba->cq_nvecs; i++) {
1170 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1171 		const struct cpumask *mask = cq->irq_mask;
1172 
1173 		if (mask && !cpumask_intersects(cpu_online_mask, mask))
1174 			continue;
1175 		rc = sas_execute_internal_abort_dev(device, i, &data);
1176 		if (rc)
1177 			return rc;
1178 	}
1179 
1180 	return 0;
1181 }
1182 
hisi_sas_dev_gone(struct domain_device * device)1183 static void hisi_sas_dev_gone(struct domain_device *device)
1184 {
1185 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1186 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1187 	struct device *dev = hisi_hba->dev;
1188 	int ret = 0;
1189 
1190 	dev_info(dev, "dev[%d:%x] is gone\n",
1191 		 sas_dev->device_id, sas_dev->dev_type);
1192 
1193 	down(&hisi_hba->sem);
1194 	if (!test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) {
1195 		hisi_sas_internal_task_abort_dev(sas_dev, true);
1196 
1197 		hisi_sas_dereg_device(hisi_hba, device);
1198 
1199 		ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1200 		device->lldd_dev = NULL;
1201 	}
1202 
1203 	if (hisi_hba->hw->free_device)
1204 		hisi_hba->hw->free_device(sas_dev);
1205 
1206 	/* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1207 	if (!ret)
1208 		sas_dev->dev_type = SAS_PHY_UNUSED;
1209 	sas_dev->sas_device = NULL;
1210 	up(&hisi_hba->sem);
1211 }
1212 
hisi_sas_phy_set_linkrate(struct hisi_hba * hisi_hba,int phy_no,struct sas_phy_linkrates * r)1213 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1214 			struct sas_phy_linkrates *r)
1215 {
1216 	struct sas_phy_linkrates _r;
1217 
1218 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1219 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1220 	enum sas_linkrate min, max;
1221 
1222 	if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1223 		return -EINVAL;
1224 
1225 	if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1226 		max = sas_phy->phy->maximum_linkrate;
1227 		min = r->minimum_linkrate;
1228 	} else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1229 		max = r->maximum_linkrate;
1230 		min = sas_phy->phy->minimum_linkrate;
1231 	} else
1232 		return -EINVAL;
1233 
1234 	_r.maximum_linkrate = max;
1235 	_r.minimum_linkrate = min;
1236 
1237 	sas_phy->phy->maximum_linkrate = max;
1238 	sas_phy->phy->minimum_linkrate = min;
1239 
1240 	hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1241 	msleep(100);
1242 	hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1243 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1244 
1245 	return 0;
1246 }
1247 
hisi_sas_control_phy(struct asd_sas_phy * sas_phy,enum phy_func func,void * funcdata)1248 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1249 				void *funcdata)
1250 {
1251 	struct hisi_sas_phy *phy = container_of(sas_phy,
1252 			struct hisi_sas_phy, sas_phy);
1253 	struct sas_ha_struct *sas_ha = sas_phy->ha;
1254 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1255 	struct device *dev = hisi_hba->dev;
1256 	DECLARE_COMPLETION_ONSTACK(completion);
1257 	int phy_no = sas_phy->id;
1258 	u8 sts = phy->phy_attached;
1259 	int ret = 0;
1260 
1261 	down(&hisi_hba->sem);
1262 	phy->reset_completion = &completion;
1263 
1264 	switch (func) {
1265 	case PHY_FUNC_HARD_RESET:
1266 		hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1267 		break;
1268 
1269 	case PHY_FUNC_LINK_RESET:
1270 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1271 		msleep(100);
1272 		hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1273 		break;
1274 
1275 	case PHY_FUNC_DISABLE:
1276 		hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1277 		goto out;
1278 
1279 	case PHY_FUNC_SET_LINK_RATE:
1280 		ret = hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1281 		break;
1282 
1283 	case PHY_FUNC_GET_EVENTS:
1284 		if (hisi_hba->hw->get_events) {
1285 			hisi_hba->hw->get_events(hisi_hba, phy_no);
1286 			goto out;
1287 		}
1288 		fallthrough;
1289 	case PHY_FUNC_RELEASE_SPINUP_HOLD:
1290 	default:
1291 		ret = -EOPNOTSUPP;
1292 		goto out;
1293 	}
1294 
1295 	if (sts && !wait_for_completion_timeout(&completion,
1296 		HISI_SAS_WAIT_PHYUP_TIMEOUT)) {
1297 		dev_warn(dev, "phy%d wait phyup timed out for func %d\n",
1298 			 phy_no, func);
1299 		if (phy->in_reset)
1300 			ret = -ETIMEDOUT;
1301 	}
1302 
1303 out:
1304 	phy->reset_completion = NULL;
1305 
1306 	up(&hisi_hba->sem);
1307 	return ret;
1308 }
1309 
hisi_sas_fill_ata_reset_cmd(struct ata_device * dev,bool reset,int pmp,u8 * fis)1310 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1311 		bool reset, int pmp, u8 *fis)
1312 {
1313 	struct ata_taskfile tf;
1314 
1315 	ata_tf_init(dev, &tf);
1316 	if (reset)
1317 		tf.ctl |= ATA_SRST;
1318 	else
1319 		tf.ctl &= ~ATA_SRST;
1320 	tf.command = ATA_CMD_DEV_RESET;
1321 	ata_tf_to_fis(&tf, pmp, 0, fis);
1322 }
1323 
hisi_sas_softreset_ata_disk(struct domain_device * device)1324 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1325 {
1326 	u8 fis[20] = {0};
1327 	struct ata_port *ap = device->sata_dev.ap;
1328 	struct ata_link *link;
1329 	int rc = TMF_RESP_FUNC_FAILED;
1330 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1331 	struct device *dev = hisi_hba->dev;
1332 
1333 	ata_for_each_link(link, ap, EDGE) {
1334 		int pmp = sata_srst_pmp(link);
1335 
1336 		hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1337 		rc = sas_execute_ata_cmd(device, fis, -1);
1338 		if (rc != TMF_RESP_FUNC_COMPLETE)
1339 			break;
1340 	}
1341 
1342 	if (rc == TMF_RESP_FUNC_COMPLETE) {
1343 		ata_for_each_link(link, ap, EDGE) {
1344 			int pmp = sata_srst_pmp(link);
1345 
1346 			hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1347 			rc = sas_execute_ata_cmd(device, fis, -1);
1348 			if (rc != TMF_RESP_FUNC_COMPLETE)
1349 				dev_err(dev, "ata disk %016llx de-reset failed\n",
1350 					SAS_ADDR(device->sas_addr));
1351 		}
1352 	} else {
1353 		dev_err(dev, "ata disk %016llx reset failed\n",
1354 			SAS_ADDR(device->sas_addr));
1355 	}
1356 
1357 	if (rc == TMF_RESP_FUNC_COMPLETE)
1358 		hisi_sas_release_task(hisi_hba, device);
1359 
1360 	return rc;
1361 }
1362 
hisi_sas_refresh_port_id(struct hisi_hba * hisi_hba)1363 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1364 {
1365 	u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1366 	int i;
1367 
1368 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1369 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1370 		struct domain_device *device = sas_dev->sas_device;
1371 		struct asd_sas_port *sas_port;
1372 		struct hisi_sas_port *port;
1373 		struct hisi_sas_phy *phy = NULL;
1374 		struct asd_sas_phy *sas_phy;
1375 
1376 		if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1377 				|| !device || !device->port)
1378 			continue;
1379 
1380 		sas_port = device->port;
1381 		port = to_hisi_sas_port(sas_port);
1382 
1383 		spin_lock(&sas_port->phy_list_lock);
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 		spin_unlock(&sas_port->phy_list_lock);
1390 
1391 		if (phy) {
1392 			port->id = phy->port_id;
1393 
1394 			/* Update linkrate of directly attached device. */
1395 			if (!device->parent)
1396 				device->linkrate = phy->sas_phy.linkrate;
1397 
1398 			hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1399 		} else if (!port->port_attached)
1400 			port->id = 0xff;
1401 	}
1402 }
1403 
hisi_sas_rescan_topology(struct hisi_hba * hisi_hba,u32 state)1404 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1405 {
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_notify_port_event(sas_phy,
1427 							PORTE_BROADCAST_RCVD,
1428 							GFP_KERNEL);
1429 			}
1430 		} else {
1431 			hisi_sas_phy_down(hisi_hba, phy_no, 0, GFP_KERNEL);
1432 		}
1433 	}
1434 }
1435 
hisi_sas_reset_init_all_devices(struct hisi_hba * hisi_hba)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 
hisi_sas_send_ata_reset_each_phy(struct hisi_hba * hisi_hba,struct asd_sas_port * sas_port,struct domain_device * device)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 ata_port *ap = device->sata_dev.ap;
1458 	struct device *dev = hisi_hba->dev;
1459 	int rc = TMF_RESP_FUNC_FAILED;
1460 	struct ata_link *link;
1461 	u8 fis[20] = {0};
1462 	int i;
1463 
1464 	for (i = 0; i < hisi_hba->n_phy; i++) {
1465 		if (!(sas_port->phy_mask & BIT(i)))
1466 			continue;
1467 
1468 		ata_for_each_link(link, ap, EDGE) {
1469 			int pmp = sata_srst_pmp(link);
1470 
1471 			hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1472 			rc = sas_execute_ata_cmd(device, fis, i);
1473 			if (rc != TMF_RESP_FUNC_COMPLETE) {
1474 				dev_err(dev, "phy%d ata reset failed rc=%d\n",
1475 					i, rc);
1476 				break;
1477 			}
1478 		}
1479 	}
1480 }
1481 
hisi_sas_terminate_stp_reject(struct hisi_hba * hisi_hba)1482 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1483 {
1484 	struct device *dev = hisi_hba->dev;
1485 	int port_no, rc, i;
1486 
1487 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1488 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1489 		struct domain_device *device = sas_dev->sas_device;
1490 
1491 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1492 			continue;
1493 
1494 		rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1495 		if (rc < 0)
1496 			dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1497 	}
1498 
1499 	for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1500 		struct hisi_sas_port *port = &hisi_hba->port[port_no];
1501 		struct asd_sas_port *sas_port = &port->sas_port;
1502 		struct domain_device *port_dev = sas_port->port_dev;
1503 		struct domain_device *device;
1504 
1505 		if (!port_dev || !dev_is_expander(port_dev->dev_type))
1506 			continue;
1507 
1508 		/* Try to find a SATA device */
1509 		list_for_each_entry(device, &sas_port->dev_list,
1510 				    dev_list_node) {
1511 			if (dev_is_sata(device)) {
1512 				hisi_sas_send_ata_reset_each_phy(hisi_hba,
1513 								 sas_port,
1514 								 device);
1515 				break;
1516 			}
1517 		}
1518 	}
1519 }
1520 
hisi_sas_controller_reset_prepare(struct hisi_hba * hisi_hba)1521 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1522 {
1523 	struct Scsi_Host *shost = hisi_hba->shost;
1524 
1525 	hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1526 
1527 	scsi_block_requests(shost);
1528 	hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1529 
1530 	del_timer_sync(&hisi_hba->timer);
1531 
1532 	set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1533 }
1534 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1535 
hisi_sas_async_init_wait_phyup(void * data,async_cookie_t cookie)1536 static void hisi_sas_async_init_wait_phyup(void *data, async_cookie_t cookie)
1537 {
1538 	struct hisi_sas_phy *phy = data;
1539 	struct hisi_hba *hisi_hba = phy->hisi_hba;
1540 	struct device *dev = hisi_hba->dev;
1541 	DECLARE_COMPLETION_ONSTACK(completion);
1542 	int phy_no = phy->sas_phy.id;
1543 
1544 	phy->reset_completion = &completion;
1545 	hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1546 	if (!wait_for_completion_timeout(&completion,
1547 					 HISI_SAS_WAIT_PHYUP_TIMEOUT))
1548 		dev_warn(dev, "phy%d wait phyup timed out\n", phy_no);
1549 
1550 	phy->reset_completion = NULL;
1551 }
1552 
hisi_sas_controller_reset_done(struct hisi_hba * hisi_hba)1553 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1554 {
1555 	struct Scsi_Host *shost = hisi_hba->shost;
1556 	ASYNC_DOMAIN_EXCLUSIVE(async);
1557 	int phy_no;
1558 
1559 	/* Init and wait for PHYs to come up and all libsas event finished. */
1560 	for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1561 		struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1562 		struct asd_sas_phy *sas_phy = &phy->sas_phy;
1563 
1564 		if (!sas_phy->phy->enabled)
1565 			continue;
1566 
1567 		if (!(hisi_hba->phy_state & BIT(phy_no))) {
1568 			hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1569 			continue;
1570 		}
1571 
1572 		async_schedule_domain(hisi_sas_async_init_wait_phyup,
1573 				      phy, &async);
1574 	}
1575 
1576 	async_synchronize_full_domain(&async);
1577 	hisi_sas_refresh_port_id(hisi_hba);
1578 	clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1579 
1580 	if (hisi_hba->reject_stp_links_msk)
1581 		hisi_sas_terminate_stp_reject(hisi_hba);
1582 	hisi_sas_reset_init_all_devices(hisi_hba);
1583 	scsi_unblock_requests(shost);
1584 	clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags);
1585 	up(&hisi_hba->sem);
1586 
1587 	hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1588 }
1589 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1590 
hisi_sas_controller_prereset(struct hisi_hba * hisi_hba)1591 static int hisi_sas_controller_prereset(struct hisi_hba *hisi_hba)
1592 {
1593 	if (!hisi_hba->hw->soft_reset)
1594 		return -ENOENT;
1595 
1596 	down(&hisi_hba->sem);
1597 	if (test_and_set_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) {
1598 		up(&hisi_hba->sem);
1599 		return -EPERM;
1600 	}
1601 
1602 	if (hisi_sas_debugfs_enable)
1603 		hisi_hba->hw->debugfs_snapshot_regs(hisi_hba);
1604 
1605 	return 0;
1606 }
1607 
hisi_sas_controller_reset(struct hisi_hba * hisi_hba)1608 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1609 {
1610 	struct device *dev = hisi_hba->dev;
1611 	struct Scsi_Host *shost = hisi_hba->shost;
1612 	int rc;
1613 
1614 	dev_info(dev, "controller resetting...\n");
1615 	hisi_sas_controller_reset_prepare(hisi_hba);
1616 
1617 	rc = hisi_hba->hw->soft_reset(hisi_hba);
1618 	if (rc) {
1619 		dev_warn(dev, "controller reset failed (%d)\n", rc);
1620 		clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1621 		up(&hisi_hba->sem);
1622 		scsi_unblock_requests(shost);
1623 		clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags);
1624 		return rc;
1625 	}
1626 	clear_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags);
1627 
1628 	hisi_sas_controller_reset_done(hisi_hba);
1629 	dev_info(dev, "controller reset complete\n");
1630 
1631 	return 0;
1632 }
1633 
hisi_sas_abort_task(struct sas_task * task)1634 static int hisi_sas_abort_task(struct sas_task *task)
1635 {
1636 	struct hisi_sas_internal_abort_data internal_abort_data = { false };
1637 	struct domain_device *device = task->dev;
1638 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1639 	struct hisi_sas_slot *slot = task->lldd_task;
1640 	struct hisi_hba *hisi_hba;
1641 	struct device *dev;
1642 	int rc = TMF_RESP_FUNC_FAILED;
1643 	unsigned long flags;
1644 
1645 	if (!sas_dev)
1646 		return TMF_RESP_FUNC_FAILED;
1647 
1648 	hisi_hba = dev_to_hisi_hba(task->dev);
1649 	dev = hisi_hba->dev;
1650 
1651 	spin_lock_irqsave(&task->task_state_lock, flags);
1652 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1653 		struct hisi_sas_cq *cq;
1654 
1655 		if (slot) {
1656 			/*
1657 			 * sync irq or poll queue to avoid free'ing task
1658 			 * before using task in IO completion
1659 			 */
1660 			cq = &hisi_hba->cq[slot->dlvry_queue];
1661 			hisi_sas_sync_cq(cq);
1662 		}
1663 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1664 		rc = TMF_RESP_FUNC_COMPLETE;
1665 		goto out;
1666 	}
1667 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1668 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1669 
1670 	if (!slot)
1671 		goto out;
1672 
1673 	if (task->task_proto & SAS_PROTOCOL_SSP) {
1674 		u16 tag = slot->idx;
1675 		int rc2;
1676 
1677 		rc = sas_abort_task(task, tag);
1678 		rc2 = sas_execute_internal_abort_single(device, tag,
1679 				slot->dlvry_queue, &internal_abort_data);
1680 		if (rc2 < 0) {
1681 			dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1682 			return TMF_RESP_FUNC_FAILED;
1683 		}
1684 
1685 		/*
1686 		 * If the TMF finds that the IO is not in the device and also
1687 		 * the internal abort does not succeed, then it is safe to
1688 		 * free the slot.
1689 		 * Note: if the internal abort succeeds then the slot
1690 		 * will have already been completed
1691 		 */
1692 		if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1693 			if (task->lldd_task)
1694 				hisi_sas_do_release_task(hisi_hba, task, slot, true);
1695 		}
1696 	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1697 		task->task_proto & SAS_PROTOCOL_STP) {
1698 		if (task->dev->dev_type == SAS_SATA_DEV) {
1699 			struct ata_queued_cmd *qc = task->uldd_task;
1700 
1701 			rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1702 			if (rc < 0) {
1703 				dev_err(dev, "abort task: internal abort failed\n");
1704 				goto out;
1705 			}
1706 			hisi_sas_dereg_device(hisi_hba, device);
1707 
1708 			/*
1709 			 * If an ATA internal command times out in ATA EH, it
1710 			 * need to execute soft reset, so check the scsicmd
1711 			 */
1712 			if ((sas_dev->dev_status == HISI_SAS_DEV_NCQ_ERR) &&
1713 			    qc && qc->scsicmd) {
1714 				hisi_sas_do_release_task(hisi_hba, task, slot, true);
1715 				rc = TMF_RESP_FUNC_COMPLETE;
1716 			} else {
1717 				rc = hisi_sas_softreset_ata_disk(device);
1718 			}
1719 		}
1720 	} else if (task->task_proto & SAS_PROTOCOL_SMP) {
1721 		/* SMP */
1722 		u32 tag = slot->idx;
1723 		struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1724 
1725 		rc = sas_execute_internal_abort_single(device,
1726 						       tag, slot->dlvry_queue,
1727 						       &internal_abort_data);
1728 		if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1729 					task->lldd_task) {
1730 			/*
1731 			 * sync irq or poll queue to avoid free'ing task
1732 			 * before using task in IO completion
1733 			 */
1734 			hisi_sas_sync_cq(cq);
1735 			slot->task = NULL;
1736 		}
1737 	}
1738 
1739 out:
1740 	if (rc != TMF_RESP_FUNC_COMPLETE)
1741 		dev_notice(dev, "abort task: rc=%d\n", rc);
1742 	return rc;
1743 }
1744 
hisi_sas_abort_task_set(struct domain_device * device,u8 * lun)1745 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1746 {
1747 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1748 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1749 	struct device *dev = hisi_hba->dev;
1750 	int rc;
1751 
1752 	rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1753 	if (rc < 0) {
1754 		dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1755 		return TMF_RESP_FUNC_FAILED;
1756 	}
1757 	hisi_sas_dereg_device(hisi_hba, device);
1758 
1759 	rc = sas_abort_task_set(device, lun);
1760 	if (rc == TMF_RESP_FUNC_COMPLETE)
1761 		hisi_sas_release_task(hisi_hba, device);
1762 
1763 	return rc;
1764 }
1765 
hisi_sas_debug_I_T_nexus_reset(struct domain_device * device)1766 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1767 {
1768 	struct sas_phy *local_phy = sas_get_local_phy(device);
1769 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1770 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1771 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1772 	int rc, reset_type;
1773 
1774 	if (!local_phy->enabled) {
1775 		sas_put_local_phy(local_phy);
1776 		return -ENODEV;
1777 	}
1778 
1779 	if (scsi_is_sas_phy_local(local_phy)) {
1780 		struct asd_sas_phy *sas_phy =
1781 			sas_ha->sas_phy[local_phy->number];
1782 		struct hisi_sas_phy *phy =
1783 			container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1784 		unsigned long flags;
1785 
1786 		spin_lock_irqsave(&phy->lock, flags);
1787 		phy->in_reset = 1;
1788 		spin_unlock_irqrestore(&phy->lock, flags);
1789 	}
1790 
1791 	reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1792 		      !dev_is_sata(device)) ? true : false;
1793 
1794 	rc = sas_phy_reset(local_phy, reset_type);
1795 	sas_put_local_phy(local_phy);
1796 
1797 	if (scsi_is_sas_phy_local(local_phy)) {
1798 		struct asd_sas_phy *sas_phy =
1799 			sas_ha->sas_phy[local_phy->number];
1800 		struct hisi_sas_phy *phy =
1801 			container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1802 		unsigned long flags;
1803 
1804 		spin_lock_irqsave(&phy->lock, flags);
1805 		phy->in_reset = 0;
1806 		spin_unlock_irqrestore(&phy->lock, flags);
1807 
1808 		/* report PHY down if timed out */
1809 		if (rc == -ETIMEDOUT)
1810 			hisi_sas_phy_down(hisi_hba, sas_phy->id, 0, GFP_KERNEL);
1811 		return rc;
1812 	}
1813 
1814 	/* Remote phy */
1815 	if (rc)
1816 		return rc;
1817 
1818 	if (dev_is_sata(device)) {
1819 		struct ata_link *link = &device->sata_dev.ap->link;
1820 
1821 		rc = ata_wait_after_reset(link, jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT,
1822 					  smp_ata_check_ready_type);
1823 	} else {
1824 		msleep(2000);
1825 	}
1826 
1827 	return rc;
1828 }
1829 
hisi_sas_I_T_nexus_reset(struct domain_device * device)1830 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1831 {
1832 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1833 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1834 	struct device *dev = hisi_hba->dev;
1835 	int rc;
1836 
1837 	if (sas_dev->dev_status == HISI_SAS_DEV_NCQ_ERR)
1838 		sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1839 
1840 	rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1841 	if (rc < 0) {
1842 		dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1843 		return TMF_RESP_FUNC_FAILED;
1844 	}
1845 	hisi_sas_dereg_device(hisi_hba, device);
1846 
1847 	if (dev_is_sata(device)) {
1848 		rc = hisi_sas_softreset_ata_disk(device);
1849 		if (rc == TMF_RESP_FUNC_FAILED)
1850 			dev_err(dev, "ata disk %016llx reset (%d)\n",
1851 				SAS_ADDR(device->sas_addr), rc);
1852 	}
1853 
1854 	rc = hisi_sas_debug_I_T_nexus_reset(device);
1855 	if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1856 		hisi_sas_release_task(hisi_hba, device);
1857 
1858 	return rc;
1859 }
1860 
hisi_sas_lu_reset(struct domain_device * device,u8 * lun)1861 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1862 {
1863 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1864 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1865 	struct device *dev = hisi_hba->dev;
1866 	int rc = TMF_RESP_FUNC_FAILED;
1867 
1868 	/* Clear internal IO and then lu reset */
1869 	rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1870 	if (rc < 0) {
1871 		dev_err(dev, "lu_reset: internal abort failed\n");
1872 		goto out;
1873 	}
1874 	hisi_sas_dereg_device(hisi_hba, device);
1875 
1876 	if (dev_is_sata(device)) {
1877 		struct sas_phy *phy;
1878 
1879 		phy = sas_get_local_phy(device);
1880 
1881 		rc = sas_phy_reset(phy, true);
1882 
1883 		if (rc == 0)
1884 			hisi_sas_release_task(hisi_hba, device);
1885 		sas_put_local_phy(phy);
1886 	} else {
1887 		rc = sas_lu_reset(device, lun);
1888 		if (rc == TMF_RESP_FUNC_COMPLETE)
1889 			hisi_sas_release_task(hisi_hba, device);
1890 	}
1891 out:
1892 	if (rc != TMF_RESP_FUNC_COMPLETE)
1893 		dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1894 			     sas_dev->device_id, rc);
1895 	return rc;
1896 }
1897 
hisi_sas_async_I_T_nexus_reset(void * data,async_cookie_t cookie)1898 static void hisi_sas_async_I_T_nexus_reset(void *data, async_cookie_t cookie)
1899 {
1900 	struct domain_device *device = data;
1901 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1902 	int rc;
1903 
1904 	rc = hisi_sas_debug_I_T_nexus_reset(device);
1905 	if (rc != TMF_RESP_FUNC_COMPLETE)
1906 		dev_info(hisi_hba->dev, "I_T_nexus reset fail for dev:%016llx rc=%d\n",
1907 			 SAS_ADDR(device->sas_addr), rc);
1908 }
1909 
hisi_sas_clear_nexus_ha(struct sas_ha_struct * sas_ha)1910 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1911 {
1912 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1913 	HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1914 	ASYNC_DOMAIN_EXCLUSIVE(async);
1915 	int i;
1916 
1917 	queue_work(hisi_hba->wq, &r.work);
1918 	wait_for_completion(r.completion);
1919 	if (!r.done)
1920 		return TMF_RESP_FUNC_FAILED;
1921 
1922 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1923 		struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1924 		struct domain_device *device = sas_dev->sas_device;
1925 
1926 		if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1927 		    dev_is_expander(device->dev_type))
1928 			continue;
1929 
1930 		async_schedule_domain(hisi_sas_async_I_T_nexus_reset,
1931 				      device, &async);
1932 	}
1933 
1934 	async_synchronize_full_domain(&async);
1935 	hisi_sas_release_tasks(hisi_hba);
1936 
1937 	return TMF_RESP_FUNC_COMPLETE;
1938 }
1939 
hisi_sas_query_task(struct sas_task * task)1940 static int hisi_sas_query_task(struct sas_task *task)
1941 {
1942 	int rc = TMF_RESP_FUNC_FAILED;
1943 
1944 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1945 		struct hisi_sas_slot *slot = task->lldd_task;
1946 		u32 tag = slot->idx;
1947 
1948 		rc = sas_query_task(task, tag);
1949 		switch (rc) {
1950 		/* The task is still in Lun, release it then */
1951 		case TMF_RESP_FUNC_SUCC:
1952 		/* The task is not in Lun or failed, reset the phy */
1953 		case TMF_RESP_FUNC_FAILED:
1954 		case TMF_RESP_FUNC_COMPLETE:
1955 			break;
1956 		default:
1957 			rc = TMF_RESP_FUNC_FAILED;
1958 			break;
1959 		}
1960 	}
1961 	return rc;
1962 }
1963 
hisi_sas_internal_abort_timeout(struct sas_task * task,void * data)1964 static bool hisi_sas_internal_abort_timeout(struct sas_task *task,
1965 					    void *data)
1966 {
1967 	struct domain_device *device = task->dev;
1968 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1969 	struct hisi_sas_internal_abort_data *timeout = data;
1970 
1971 	if (hisi_sas_debugfs_enable) {
1972 		/*
1973 		 * If timeout occurs in device gone scenario, to avoid
1974 		 * circular dependency like:
1975 		 * hisi_sas_dev_gone() -> down() -> ... ->
1976 		 * hisi_sas_internal_abort_timeout() -> down().
1977 		 */
1978 		if (!timeout->rst_ha_timeout)
1979 			down(&hisi_hba->sem);
1980 		hisi_hba->hw->debugfs_snapshot_regs(hisi_hba);
1981 		if (!timeout->rst_ha_timeout)
1982 			up(&hisi_hba->sem);
1983 	}
1984 
1985 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1986 		pr_err("Internal abort: timeout %016llx\n",
1987 		       SAS_ADDR(device->sas_addr));
1988 	} else {
1989 		struct hisi_sas_slot *slot = task->lldd_task;
1990 
1991 		set_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags);
1992 
1993 		if (slot) {
1994 			struct hisi_sas_cq *cq =
1995 				&hisi_hba->cq[slot->dlvry_queue];
1996 			/*
1997 			 * sync irq or poll queue to avoid free'ing task
1998 			 * before using task in IO completion
1999 			 */
2000 			hisi_sas_sync_cq(cq);
2001 			slot->task = NULL;
2002 		}
2003 
2004 		if (timeout->rst_ha_timeout) {
2005 			pr_err("Internal abort: timeout and not done %016llx. Queuing reset.\n",
2006 			       SAS_ADDR(device->sas_addr));
2007 			queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2008 		} else {
2009 			pr_err("Internal abort: timeout and not done %016llx.\n",
2010 			       SAS_ADDR(device->sas_addr));
2011 		}
2012 
2013 		return true;
2014 	}
2015 
2016 	return false;
2017 }
2018 
hisi_sas_port_formed(struct asd_sas_phy * sas_phy)2019 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2020 {
2021 	hisi_sas_port_notify_formed(sas_phy);
2022 }
2023 
hisi_sas_write_gpio(struct sas_ha_struct * sha,u8 reg_type,u8 reg_index,u8 reg_count,u8 * write_data)2024 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2025 			u8 reg_index, u8 reg_count, u8 *write_data)
2026 {
2027 	struct hisi_hba *hisi_hba = sha->lldd_ha;
2028 
2029 	if (!hisi_hba->hw->write_gpio)
2030 		return -EOPNOTSUPP;
2031 
2032 	return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2033 				reg_index, reg_count, write_data);
2034 }
2035 
hisi_sas_phy_disconnected(struct hisi_sas_phy * phy)2036 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2037 {
2038 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2039 	struct sas_phy *sphy = sas_phy->phy;
2040 	unsigned long flags;
2041 
2042 	phy->phy_attached = 0;
2043 	phy->phy_type = 0;
2044 	phy->port = NULL;
2045 
2046 	spin_lock_irqsave(&phy->lock, flags);
2047 	if (phy->enable)
2048 		sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2049 	else
2050 		sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2051 	spin_unlock_irqrestore(&phy->lock, flags);
2052 }
2053 
hisi_sas_phy_down(struct hisi_hba * hisi_hba,int phy_no,int rdy,gfp_t gfp_flags)2054 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy,
2055 		       gfp_t gfp_flags)
2056 {
2057 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2058 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2059 	struct device *dev = hisi_hba->dev;
2060 
2061 	if (rdy) {
2062 		/* Phy down but ready */
2063 		hisi_sas_bytes_dmaed(hisi_hba, phy_no, gfp_flags);
2064 		hisi_sas_port_notify_formed(sas_phy);
2065 	} else {
2066 		struct hisi_sas_port *port  = phy->port;
2067 
2068 		if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags) ||
2069 		    phy->in_reset) {
2070 			dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2071 			return;
2072 		}
2073 		/* Phy down and not ready */
2074 		sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL, gfp_flags);
2075 		sas_phy_disconnected(sas_phy);
2076 
2077 		if (port) {
2078 			if (phy->phy_type & PORT_TYPE_SAS) {
2079 				int port_id = port->id;
2080 
2081 				if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2082 								       port_id))
2083 					port->port_attached = 0;
2084 			} else if (phy->phy_type & PORT_TYPE_SATA)
2085 				port->port_attached = 0;
2086 		}
2087 		hisi_sas_phy_disconnected(phy);
2088 	}
2089 }
2090 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2091 
hisi_sas_phy_bcast(struct hisi_sas_phy * phy)2092 void hisi_sas_phy_bcast(struct hisi_sas_phy *phy)
2093 {
2094 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2095 	struct hisi_hba	*hisi_hba = phy->hisi_hba;
2096 
2097 	if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags))
2098 		return;
2099 
2100 	sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD, GFP_ATOMIC);
2101 }
2102 EXPORT_SYMBOL_GPL(hisi_sas_phy_bcast);
2103 
hisi_sas_host_reset(struct Scsi_Host * shost,int reset_type)2104 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2105 {
2106 	struct hisi_hba *hisi_hba = shost_priv(shost);
2107 
2108 	if (reset_type != SCSI_ADAPTER_RESET)
2109 		return -EOPNOTSUPP;
2110 
2111 	queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2112 
2113 	return 0;
2114 }
2115 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2116 
2117 struct scsi_transport_template *hisi_sas_stt;
2118 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2119 
2120 static struct sas_domain_function_template hisi_sas_transport_ops = {
2121 	.lldd_dev_found		= hisi_sas_dev_found,
2122 	.lldd_dev_gone		= hisi_sas_dev_gone,
2123 	.lldd_execute_task	= hisi_sas_queue_command,
2124 	.lldd_control_phy	= hisi_sas_control_phy,
2125 	.lldd_abort_task	= hisi_sas_abort_task,
2126 	.lldd_abort_task_set	= hisi_sas_abort_task_set,
2127 	.lldd_I_T_nexus_reset	= hisi_sas_I_T_nexus_reset,
2128 	.lldd_lu_reset		= hisi_sas_lu_reset,
2129 	.lldd_query_task	= hisi_sas_query_task,
2130 	.lldd_clear_nexus_ha	= hisi_sas_clear_nexus_ha,
2131 	.lldd_port_formed	= hisi_sas_port_formed,
2132 	.lldd_write_gpio	= hisi_sas_write_gpio,
2133 	.lldd_tmf_aborted	= hisi_sas_tmf_aborted,
2134 	.lldd_abort_timeout	= hisi_sas_internal_abort_timeout,
2135 };
2136 
hisi_sas_init_mem(struct hisi_hba * hisi_hba)2137 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2138 {
2139 	int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2140 	struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2141 
2142 	for (i = 0; i < hisi_hba->queue_count; i++) {
2143 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2144 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2145 		struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2146 
2147 		s = sizeof(struct hisi_sas_cmd_hdr);
2148 		for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2149 			memset(&cmd_hdr[j], 0, s);
2150 
2151 		dq->wr_point = 0;
2152 
2153 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2154 		memset(hisi_hba->complete_hdr[i], 0, s);
2155 		cq->rd_point = 0;
2156 	}
2157 
2158 	s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2159 	memset(hisi_hba->initial_fis, 0, s);
2160 
2161 	s = max_command_entries * sizeof(struct hisi_sas_iost);
2162 	memset(hisi_hba->iost, 0, s);
2163 
2164 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2165 	memset(hisi_hba->breakpoint, 0, s);
2166 
2167 	s = sizeof(struct hisi_sas_sata_breakpoint);
2168 	for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2169 		memset(&sata_breakpoint[j], 0, s);
2170 }
2171 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2172 
hisi_sas_alloc(struct hisi_hba * hisi_hba)2173 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2174 {
2175 	struct device *dev = hisi_hba->dev;
2176 	int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2177 	int max_command_entries_ru, sz_slot_buf_ru;
2178 	int blk_cnt, slots_per_blk;
2179 
2180 	sema_init(&hisi_hba->sem, 1);
2181 	spin_lock_init(&hisi_hba->lock);
2182 	for (i = 0; i < hisi_hba->n_phy; i++) {
2183 		hisi_sas_phy_init(hisi_hba, i);
2184 		hisi_hba->port[i].port_attached = 0;
2185 		hisi_hba->port[i].id = -1;
2186 	}
2187 
2188 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2189 		hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2190 		hisi_hba->devices[i].device_id = i;
2191 		hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2192 	}
2193 
2194 	for (i = 0; i < hisi_hba->queue_count; i++) {
2195 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2196 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2197 
2198 		/* Completion queue structure */
2199 		cq->id = i;
2200 		cq->hisi_hba = hisi_hba;
2201 		spin_lock_init(&cq->poll_lock);
2202 
2203 		/* Delivery queue structure */
2204 		spin_lock_init(&dq->lock);
2205 		INIT_LIST_HEAD(&dq->list);
2206 		dq->id = i;
2207 		dq->hisi_hba = hisi_hba;
2208 
2209 		/* Delivery queue */
2210 		s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2211 		hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2212 						&hisi_hba->cmd_hdr_dma[i],
2213 						GFP_KERNEL);
2214 		if (!hisi_hba->cmd_hdr[i])
2215 			goto err_out;
2216 
2217 		/* Completion queue */
2218 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2219 		hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2220 						&hisi_hba->complete_hdr_dma[i],
2221 						GFP_KERNEL);
2222 		if (!hisi_hba->complete_hdr[i])
2223 			goto err_out;
2224 	}
2225 
2226 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2227 	hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2228 					     GFP_KERNEL);
2229 	if (!hisi_hba->itct)
2230 		goto err_out;
2231 
2232 	hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2233 					   sizeof(struct hisi_sas_slot),
2234 					   GFP_KERNEL);
2235 	if (!hisi_hba->slot_info)
2236 		goto err_out;
2237 
2238 	/* roundup to avoid overly large block size */
2239 	max_command_entries_ru = roundup(max_command_entries, 64);
2240 	if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2241 		sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2242 	else
2243 		sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2244 	sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2245 	s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2246 	blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2247 	slots_per_blk = s / sz_slot_buf_ru;
2248 
2249 	for (i = 0; i < blk_cnt; i++) {
2250 		int slot_index = i * slots_per_blk;
2251 		dma_addr_t buf_dma;
2252 		void *buf;
2253 
2254 		buf = dmam_alloc_coherent(dev, s, &buf_dma,
2255 					  GFP_KERNEL);
2256 		if (!buf)
2257 			goto err_out;
2258 
2259 		for (j = 0; j < slots_per_blk; j++, slot_index++) {
2260 			struct hisi_sas_slot *slot;
2261 
2262 			slot = &hisi_hba->slot_info[slot_index];
2263 			slot->buf = buf;
2264 			slot->buf_dma = buf_dma;
2265 			slot->idx = slot_index;
2266 
2267 			buf += sz_slot_buf_ru;
2268 			buf_dma += sz_slot_buf_ru;
2269 		}
2270 	}
2271 
2272 	s = max_command_entries * sizeof(struct hisi_sas_iost);
2273 	hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2274 					     GFP_KERNEL);
2275 	if (!hisi_hba->iost)
2276 		goto err_out;
2277 
2278 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2279 	hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2280 						   &hisi_hba->breakpoint_dma,
2281 						   GFP_KERNEL);
2282 	if (!hisi_hba->breakpoint)
2283 		goto err_out;
2284 
2285 	s = hisi_hba->slot_index_count = max_command_entries;
2286 	hisi_hba->slot_index_tags = devm_bitmap_zalloc(dev, s, GFP_KERNEL);
2287 	if (!hisi_hba->slot_index_tags)
2288 		goto err_out;
2289 
2290 	s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2291 	hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2292 						    &hisi_hba->initial_fis_dma,
2293 						    GFP_KERNEL);
2294 	if (!hisi_hba->initial_fis)
2295 		goto err_out;
2296 
2297 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2298 	hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2299 					&hisi_hba->sata_breakpoint_dma,
2300 					GFP_KERNEL);
2301 	if (!hisi_hba->sata_breakpoint)
2302 		goto err_out;
2303 
2304 	hisi_hba->last_slot_index = 0;
2305 
2306 	hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2307 	if (!hisi_hba->wq) {
2308 		dev_err(dev, "sas_alloc: failed to create workqueue\n");
2309 		goto err_out;
2310 	}
2311 
2312 	return 0;
2313 err_out:
2314 	return -ENOMEM;
2315 }
2316 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2317 
hisi_sas_free(struct hisi_hba * hisi_hba)2318 void hisi_sas_free(struct hisi_hba *hisi_hba)
2319 {
2320 	int i;
2321 
2322 	for (i = 0; i < hisi_hba->n_phy; i++) {
2323 		struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2324 
2325 		del_timer_sync(&phy->timer);
2326 	}
2327 
2328 	if (hisi_hba->wq)
2329 		destroy_workqueue(hisi_hba->wq);
2330 }
2331 EXPORT_SYMBOL_GPL(hisi_sas_free);
2332 
hisi_sas_rst_work_handler(struct work_struct * work)2333 void hisi_sas_rst_work_handler(struct work_struct *work)
2334 {
2335 	struct hisi_hba *hisi_hba =
2336 		container_of(work, struct hisi_hba, rst_work);
2337 
2338 	if (hisi_sas_controller_prereset(hisi_hba))
2339 		return;
2340 
2341 	hisi_sas_controller_reset(hisi_hba);
2342 }
2343 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2344 
hisi_sas_sync_rst_work_handler(struct work_struct * work)2345 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2346 {
2347 	struct hisi_sas_rst *rst =
2348 		container_of(work, struct hisi_sas_rst, work);
2349 
2350 	if (hisi_sas_controller_prereset(rst->hisi_hba))
2351 		goto rst_complete;
2352 
2353 	if (!hisi_sas_controller_reset(rst->hisi_hba))
2354 		rst->done = true;
2355 rst_complete:
2356 	complete(rst->completion);
2357 }
2358 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2359 
hisi_sas_get_fw_info(struct hisi_hba * hisi_hba)2360 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2361 {
2362 	struct device *dev = hisi_hba->dev;
2363 	struct platform_device *pdev = hisi_hba->platform_dev;
2364 	struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2365 	struct clk *refclk;
2366 
2367 	if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2368 					  SAS_ADDR_SIZE)) {
2369 		dev_err(dev, "could not get property sas-addr\n");
2370 		return -ENOENT;
2371 	}
2372 
2373 	if (np) {
2374 		/*
2375 		 * These properties are only required for platform device-based
2376 		 * controller with DT firmware.
2377 		 */
2378 		hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2379 					"hisilicon,sas-syscon");
2380 		if (IS_ERR(hisi_hba->ctrl)) {
2381 			dev_err(dev, "could not get syscon\n");
2382 			return -ENOENT;
2383 		}
2384 
2385 		if (device_property_read_u32(dev, "ctrl-reset-reg",
2386 					     &hisi_hba->ctrl_reset_reg)) {
2387 			dev_err(dev, "could not get property ctrl-reset-reg\n");
2388 			return -ENOENT;
2389 		}
2390 
2391 		if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2392 					     &hisi_hba->ctrl_reset_sts_reg)) {
2393 			dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2394 			return -ENOENT;
2395 		}
2396 
2397 		if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2398 					     &hisi_hba->ctrl_clock_ena_reg)) {
2399 			dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2400 			return -ENOENT;
2401 		}
2402 	}
2403 
2404 	refclk = devm_clk_get(dev, NULL);
2405 	if (IS_ERR(refclk))
2406 		dev_dbg(dev, "no ref clk property\n");
2407 	else
2408 		hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2409 
2410 	if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2411 		dev_err(dev, "could not get property phy-count\n");
2412 		return -ENOENT;
2413 	}
2414 
2415 	if (device_property_read_u32(dev, "queue-count",
2416 				     &hisi_hba->queue_count)) {
2417 		dev_err(dev, "could not get property queue-count\n");
2418 		return -ENOENT;
2419 	}
2420 
2421 	return 0;
2422 }
2423 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2424 
hisi_sas_shost_alloc(struct platform_device * pdev,const struct hisi_sas_hw * hw)2425 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2426 					      const struct hisi_sas_hw *hw)
2427 {
2428 	struct resource *res;
2429 	struct Scsi_Host *shost;
2430 	struct hisi_hba *hisi_hba;
2431 	struct device *dev = &pdev->dev;
2432 	int error;
2433 
2434 	shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2435 	if (!shost) {
2436 		dev_err(dev, "scsi host alloc failed\n");
2437 		return NULL;
2438 	}
2439 	hisi_hba = shost_priv(shost);
2440 
2441 	INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2442 	hisi_hba->hw = hw;
2443 	hisi_hba->dev = dev;
2444 	hisi_hba->platform_dev = pdev;
2445 	hisi_hba->shost = shost;
2446 	SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2447 
2448 	timer_setup(&hisi_hba->timer, NULL, 0);
2449 
2450 	if (hisi_sas_get_fw_info(hisi_hba) < 0)
2451 		goto err_out;
2452 
2453 	error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2454 	if (error) {
2455 		dev_err(dev, "No usable DMA addressing method\n");
2456 		goto err_out;
2457 	}
2458 
2459 	hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2460 	if (IS_ERR(hisi_hba->regs))
2461 		goto err_out;
2462 
2463 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2464 	if (res) {
2465 		hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2466 		if (IS_ERR(hisi_hba->sgpio_regs))
2467 			goto err_out;
2468 	}
2469 
2470 	if (hisi_sas_alloc(hisi_hba)) {
2471 		hisi_sas_free(hisi_hba);
2472 		goto err_out;
2473 	}
2474 
2475 	return shost;
2476 err_out:
2477 	scsi_host_put(shost);
2478 	dev_err(dev, "shost alloc failed\n");
2479 	return NULL;
2480 }
2481 
hisi_sas_interrupt_preinit(struct hisi_hba * hisi_hba)2482 static int hisi_sas_interrupt_preinit(struct hisi_hba *hisi_hba)
2483 {
2484 	if (hisi_hba->hw->interrupt_preinit)
2485 		return hisi_hba->hw->interrupt_preinit(hisi_hba);
2486 	return 0;
2487 }
2488 
hisi_sas_probe(struct platform_device * pdev,const struct hisi_sas_hw * hw)2489 int hisi_sas_probe(struct platform_device *pdev,
2490 		   const struct hisi_sas_hw *hw)
2491 {
2492 	struct Scsi_Host *shost;
2493 	struct hisi_hba *hisi_hba;
2494 	struct device *dev = &pdev->dev;
2495 	struct asd_sas_phy **arr_phy;
2496 	struct asd_sas_port **arr_port;
2497 	struct sas_ha_struct *sha;
2498 	int rc, phy_nr, port_nr, i;
2499 
2500 	shost = hisi_sas_shost_alloc(pdev, hw);
2501 	if (!shost)
2502 		return -ENOMEM;
2503 
2504 	sha = SHOST_TO_SAS_HA(shost);
2505 	hisi_hba = shost_priv(shost);
2506 	platform_set_drvdata(pdev, sha);
2507 
2508 	phy_nr = port_nr = hisi_hba->n_phy;
2509 
2510 	arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2511 	arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2512 	if (!arr_phy || !arr_port) {
2513 		rc = -ENOMEM;
2514 		goto err_out_ha;
2515 	}
2516 
2517 	sha->sas_phy = arr_phy;
2518 	sha->sas_port = arr_port;
2519 	sha->lldd_ha = hisi_hba;
2520 
2521 	shost->transportt = hisi_sas_stt;
2522 	shost->max_id = HISI_SAS_MAX_DEVICES;
2523 	shost->max_lun = ~0;
2524 	shost->max_channel = 1;
2525 	shost->max_cmd_len = 16;
2526 	if (hisi_hba->hw->slot_index_alloc) {
2527 		shost->can_queue = HISI_SAS_MAX_COMMANDS;
2528 		shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2529 	} else {
2530 		shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2531 		shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2532 	}
2533 
2534 	sha->sas_ha_name = DRV_NAME;
2535 	sha->dev = hisi_hba->dev;
2536 	sha->sas_addr = &hisi_hba->sas_addr[0];
2537 	sha->num_phys = hisi_hba->n_phy;
2538 	sha->shost = hisi_hba->shost;
2539 
2540 	for (i = 0; i < hisi_hba->n_phy; i++) {
2541 		sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2542 		sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2543 	}
2544 
2545 	rc = hisi_sas_interrupt_preinit(hisi_hba);
2546 	if (rc)
2547 		goto err_out_ha;
2548 
2549 	rc = scsi_add_host(shost, &pdev->dev);
2550 	if (rc)
2551 		goto err_out_ha;
2552 
2553 	rc = sas_register_ha(sha);
2554 	if (rc)
2555 		goto err_out_register_ha;
2556 
2557 	rc = hisi_hba->hw->hw_init(hisi_hba);
2558 	if (rc)
2559 		goto err_out_hw_init;
2560 
2561 	scsi_scan_host(shost);
2562 
2563 	return 0;
2564 
2565 err_out_hw_init:
2566 	sas_unregister_ha(sha);
2567 err_out_register_ha:
2568 	scsi_remove_host(shost);
2569 err_out_ha:
2570 	hisi_sas_free(hisi_hba);
2571 	scsi_host_put(shost);
2572 	return rc;
2573 }
2574 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2575 
hisi_sas_remove(struct platform_device * pdev)2576 void hisi_sas_remove(struct platform_device *pdev)
2577 {
2578 	struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2579 	struct hisi_hba *hisi_hba = sha->lldd_ha;
2580 	struct Scsi_Host *shost = sha->shost;
2581 
2582 	del_timer_sync(&hisi_hba->timer);
2583 
2584 	sas_unregister_ha(sha);
2585 	sas_remove_host(shost);
2586 
2587 	hisi_sas_free(hisi_hba);
2588 	scsi_host_put(shost);
2589 }
2590 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2591 
2592 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE)
2593 #define DEBUGFS_ENABLE_DEFAULT  "enabled"
2594 bool hisi_sas_debugfs_enable = true;
2595 u32 hisi_sas_debugfs_dump_count = 50;
2596 #else
2597 #define DEBUGFS_ENABLE_DEFAULT "disabled"
2598 bool hisi_sas_debugfs_enable;
2599 u32 hisi_sas_debugfs_dump_count = 1;
2600 #endif
2601 
2602 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2603 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2604 MODULE_PARM_DESC(hisi_sas_debugfs_enable,
2605 		 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT")");
2606 
2607 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
2608 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
2609 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
2610 
2611 struct dentry *hisi_sas_debugfs_dir;
2612 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir);
2613 
hisi_sas_init(void)2614 static __init int hisi_sas_init(void)
2615 {
2616 	hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2617 	if (!hisi_sas_stt)
2618 		return -ENOMEM;
2619 
2620 	if (hisi_sas_debugfs_enable) {
2621 		hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2622 		if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
2623 			pr_info("hisi_sas: Limiting debugfs dump count\n");
2624 			hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
2625 		}
2626 	}
2627 
2628 	return 0;
2629 }
2630 
hisi_sas_exit(void)2631 static __exit void hisi_sas_exit(void)
2632 {
2633 	sas_release_transport(hisi_sas_stt);
2634 
2635 	debugfs_remove(hisi_sas_debugfs_dir);
2636 }
2637 
2638 module_init(hisi_sas_init);
2639 module_exit(hisi_sas_exit);
2640 
2641 MODULE_LICENSE("GPL");
2642 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2643 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2644 MODULE_ALIAS("platform:" DRV_NAME);
2645