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