xref: /openbmc/linux/drivers/scsi/hisi_sas/hisi_sas_main.c (revision e4781421e883340b796da5a724bda7226817990b)
1 /*
2  * Copyright (c) 2015 Linaro Ltd.
3  * Copyright (c) 2015 Hisilicon Limited.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  */
11 
12 #include "hisi_sas.h"
13 #define DRV_NAME "hisi_sas"
14 
15 #define DEV_IS_GONE(dev) \
16 	((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17 
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 				u8 *lun, struct hisi_sas_tmf_task *tmf);
20 static int
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 			     struct domain_device *device,
23 			     int abort_flag, int tag);
24 
25 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
26 {
27 	return device->port->ha->lldd_ha;
28 }
29 
30 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
31 {
32 	void *bitmap = hisi_hba->slot_index_tags;
33 
34 	clear_bit(slot_idx, bitmap);
35 }
36 
37 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
38 {
39 	hisi_sas_slot_index_clear(hisi_hba, slot_idx);
40 }
41 
42 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
43 {
44 	void *bitmap = hisi_hba->slot_index_tags;
45 
46 	set_bit(slot_idx, bitmap);
47 }
48 
49 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
50 {
51 	unsigned int index;
52 	void *bitmap = hisi_hba->slot_index_tags;
53 
54 	index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
55 	if (index >= hisi_hba->slot_index_count)
56 		return -SAS_QUEUE_FULL;
57 	hisi_sas_slot_index_set(hisi_hba, index);
58 	*slot_idx = index;
59 	return 0;
60 }
61 
62 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
63 {
64 	int i;
65 
66 	for (i = 0; i < hisi_hba->slot_index_count; ++i)
67 		hisi_sas_slot_index_clear(hisi_hba, i);
68 }
69 
70 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
71 			     struct hisi_sas_slot *slot)
72 {
73 	struct device *dev = &hisi_hba->pdev->dev;
74 
75 	if (!slot->task)
76 		return;
77 
78 	if (!sas_protocol_ata(task->task_proto))
79 		if (slot->n_elem)
80 			dma_unmap_sg(dev, task->scatter, slot->n_elem,
81 				     task->data_dir);
82 
83 	if (slot->command_table)
84 		dma_pool_free(hisi_hba->command_table_pool,
85 			      slot->command_table, slot->command_table_dma);
86 
87 	if (slot->status_buffer)
88 		dma_pool_free(hisi_hba->status_buffer_pool,
89 			      slot->status_buffer, slot->status_buffer_dma);
90 
91 	if (slot->sge_page)
92 		dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
93 			      slot->sge_page_dma);
94 
95 	list_del_init(&slot->entry);
96 	task->lldd_task = NULL;
97 	slot->task = NULL;
98 	slot->port = NULL;
99 	hisi_sas_slot_index_free(hisi_hba, slot->idx);
100 	/* slot memory is fully zeroed when it is reused */
101 }
102 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
103 
104 static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
105 				  struct hisi_sas_slot *slot)
106 {
107 	return hisi_hba->hw->prep_smp(hisi_hba, slot);
108 }
109 
110 static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
111 				  struct hisi_sas_slot *slot, int is_tmf,
112 				  struct hisi_sas_tmf_task *tmf)
113 {
114 	return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
115 }
116 
117 static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
118 				  struct hisi_sas_slot *slot)
119 {
120 	return hisi_hba->hw->prep_stp(hisi_hba, slot);
121 }
122 
123 static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
124 		struct hisi_sas_slot *slot,
125 		int device_id, int abort_flag, int tag_to_abort)
126 {
127 	return hisi_hba->hw->prep_abort(hisi_hba, slot,
128 			device_id, abort_flag, tag_to_abort);
129 }
130 
131 /*
132  * This function will issue an abort TMF regardless of whether the
133  * task is in the sdev or not. Then it will do the task complete
134  * cleanup and callbacks.
135  */
136 static void hisi_sas_slot_abort(struct work_struct *work)
137 {
138 	struct hisi_sas_slot *abort_slot =
139 		container_of(work, struct hisi_sas_slot, abort_slot);
140 	struct sas_task *task = abort_slot->task;
141 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
142 	struct scsi_cmnd *cmnd = task->uldd_task;
143 	struct hisi_sas_tmf_task tmf_task;
144 	struct domain_device *device = task->dev;
145 	struct hisi_sas_device *sas_dev = device->lldd_dev;
146 	struct scsi_lun lun;
147 	struct device *dev = &hisi_hba->pdev->dev;
148 	int tag = abort_slot->idx;
149 
150 	if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
151 		dev_err(dev, "cannot abort slot for non-ssp task\n");
152 		goto out;
153 	}
154 
155 	int_to_scsilun(cmnd->device->lun, &lun);
156 	tmf_task.tmf = TMF_ABORT_TASK;
157 	tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
158 
159 	hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
160 out:
161 	/* Do cleanup for this task */
162 	hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
163 	if (task->task_done)
164 		task->task_done(task);
165 	if (sas_dev)
166 		atomic64_dec(&sas_dev->running_req);
167 }
168 
169 static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
170 			      int is_tmf, struct hisi_sas_tmf_task *tmf,
171 			      int *pass)
172 {
173 	struct domain_device *device = task->dev;
174 	struct hisi_sas_device *sas_dev = device->lldd_dev;
175 	struct hisi_sas_port *port;
176 	struct hisi_sas_slot *slot;
177 	struct hisi_sas_cmd_hdr	*cmd_hdr_base;
178 	struct device *dev = &hisi_hba->pdev->dev;
179 	int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
180 
181 	if (!device->port) {
182 		struct task_status_struct *ts = &task->task_status;
183 
184 		ts->resp = SAS_TASK_UNDELIVERED;
185 		ts->stat = SAS_PHY_DOWN;
186 		/*
187 		 * libsas will use dev->port, should
188 		 * not call task_done for sata
189 		 */
190 		if (device->dev_type != SAS_SATA_DEV)
191 			task->task_done(task);
192 		return 0;
193 	}
194 
195 	if (DEV_IS_GONE(sas_dev)) {
196 		if (sas_dev)
197 			dev_info(dev, "task prep: device %llu not ready\n",
198 				 sas_dev->device_id);
199 		else
200 			dev_info(dev, "task prep: device %016llx not ready\n",
201 				 SAS_ADDR(device->sas_addr));
202 
203 		rc = SAS_PHY_DOWN;
204 		return rc;
205 	}
206 	port = device->port->lldd_port;
207 	if (port && !port->port_attached) {
208 		dev_info(dev, "task prep: %s port%d not attach device\n",
209 			 (sas_protocol_ata(task->task_proto)) ?
210 			 "SATA/STP" : "SAS",
211 			 device->port->id);
212 
213 		return SAS_PHY_DOWN;
214 	}
215 
216 	if (!sas_protocol_ata(task->task_proto)) {
217 		if (task->num_scatter) {
218 			n_elem = dma_map_sg(dev, task->scatter,
219 					    task->num_scatter, task->data_dir);
220 			if (!n_elem) {
221 				rc = -ENOMEM;
222 				goto prep_out;
223 			}
224 		}
225 	} else
226 		n_elem = task->num_scatter;
227 
228 	if (hisi_hba->hw->slot_index_alloc)
229 		rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
230 						    device);
231 	else
232 		rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
233 	if (rc)
234 		goto err_out;
235 	rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
236 					&dlvry_queue, &dlvry_queue_slot);
237 	if (rc)
238 		goto err_out_tag;
239 
240 	slot = &hisi_hba->slot_info[slot_idx];
241 	memset(slot, 0, sizeof(struct hisi_sas_slot));
242 
243 	slot->idx = slot_idx;
244 	slot->n_elem = n_elem;
245 	slot->dlvry_queue = dlvry_queue;
246 	slot->dlvry_queue_slot = dlvry_queue_slot;
247 	cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
248 	slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
249 	slot->task = task;
250 	slot->port = port;
251 	task->lldd_task = slot;
252 	INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
253 
254 	slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
255 					     GFP_ATOMIC,
256 					     &slot->status_buffer_dma);
257 	if (!slot->status_buffer) {
258 		rc = -ENOMEM;
259 		goto err_out_slot_buf;
260 	}
261 	memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ);
262 
263 	slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool,
264 					     GFP_ATOMIC,
265 					     &slot->command_table_dma);
266 	if (!slot->command_table) {
267 		rc = -ENOMEM;
268 		goto err_out_status_buf;
269 	}
270 	memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ);
271 	memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
272 
273 	switch (task->task_proto) {
274 	case SAS_PROTOCOL_SMP:
275 		rc = hisi_sas_task_prep_smp(hisi_hba, slot);
276 		break;
277 	case SAS_PROTOCOL_SSP:
278 		rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
279 		break;
280 	case SAS_PROTOCOL_SATA:
281 	case SAS_PROTOCOL_STP:
282 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
283 		rc = hisi_sas_task_prep_ata(hisi_hba, slot);
284 		break;
285 	default:
286 		dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
287 			task->task_proto);
288 		rc = -EINVAL;
289 		break;
290 	}
291 
292 	if (rc) {
293 		dev_err(dev, "task prep: rc = 0x%x\n", rc);
294 		if (slot->sge_page)
295 			goto err_out_sge;
296 		goto err_out_command_table;
297 	}
298 
299 	list_add_tail(&slot->entry, &port->list);
300 	spin_lock(&task->task_state_lock);
301 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
302 	spin_unlock(&task->task_state_lock);
303 
304 	hisi_hba->slot_prep = slot;
305 
306 	atomic64_inc(&sas_dev->running_req);
307 	++(*pass);
308 
309 	return 0;
310 
311 err_out_sge:
312 	dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
313 		slot->sge_page_dma);
314 err_out_command_table:
315 	dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
316 		slot->command_table_dma);
317 err_out_status_buf:
318 	dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
319 		slot->status_buffer_dma);
320 err_out_slot_buf:
321 	/* Nothing to be done */
322 err_out_tag:
323 	hisi_sas_slot_index_free(hisi_hba, slot_idx);
324 err_out:
325 	dev_err(dev, "task prep: failed[%d]!\n", rc);
326 	if (!sas_protocol_ata(task->task_proto))
327 		if (n_elem)
328 			dma_unmap_sg(dev, task->scatter, n_elem,
329 				     task->data_dir);
330 prep_out:
331 	return rc;
332 }
333 
334 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
335 			      int is_tmf, struct hisi_sas_tmf_task *tmf)
336 {
337 	u32 rc;
338 	u32 pass = 0;
339 	unsigned long flags;
340 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
341 	struct device *dev = &hisi_hba->pdev->dev;
342 
343 	/* protect task_prep and start_delivery sequence */
344 	spin_lock_irqsave(&hisi_hba->lock, flags);
345 	rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass);
346 	if (rc)
347 		dev_err(dev, "task exec: failed[%d]!\n", rc);
348 
349 	if (likely(pass))
350 		hisi_hba->hw->start_delivery(hisi_hba);
351 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
352 
353 	return rc;
354 }
355 
356 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
357 {
358 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
359 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
360 	struct sas_ha_struct *sas_ha;
361 
362 	if (!phy->phy_attached)
363 		return;
364 
365 	sas_ha = &hisi_hba->sha;
366 	sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
367 
368 	if (sas_phy->phy) {
369 		struct sas_phy *sphy = sas_phy->phy;
370 
371 		sphy->negotiated_linkrate = sas_phy->linkrate;
372 		sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
373 		sphy->maximum_linkrate_hw =
374 			hisi_hba->hw->phy_get_max_linkrate();
375 		if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
376 			sphy->minimum_linkrate = phy->minimum_linkrate;
377 
378 		if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
379 			sphy->maximum_linkrate = phy->maximum_linkrate;
380 	}
381 
382 	if (phy->phy_type & PORT_TYPE_SAS) {
383 		struct sas_identify_frame *id;
384 
385 		id = (struct sas_identify_frame *)phy->frame_rcvd;
386 		id->dev_type = phy->identify.device_type;
387 		id->initiator_bits = SAS_PROTOCOL_ALL;
388 		id->target_bits = phy->identify.target_port_protocols;
389 	} else if (phy->phy_type & PORT_TYPE_SATA) {
390 		/*Nothing*/
391 	}
392 
393 	sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
394 	sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
395 }
396 
397 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
398 {
399 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
400 	struct hisi_sas_device *sas_dev = NULL;
401 	int i;
402 
403 	spin_lock(&hisi_hba->lock);
404 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
405 		if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
406 			hisi_hba->devices[i].device_id = i;
407 			sas_dev = &hisi_hba->devices[i];
408 			sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
409 			sas_dev->dev_type = device->dev_type;
410 			sas_dev->hisi_hba = hisi_hba;
411 			sas_dev->sas_device = device;
412 			break;
413 		}
414 	}
415 	spin_unlock(&hisi_hba->lock);
416 
417 	return sas_dev;
418 }
419 
420 static int hisi_sas_dev_found(struct domain_device *device)
421 {
422 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
423 	struct domain_device *parent_dev = device->parent;
424 	struct hisi_sas_device *sas_dev;
425 	struct device *dev = &hisi_hba->pdev->dev;
426 
427 	if (hisi_hba->hw->alloc_dev)
428 		sas_dev = hisi_hba->hw->alloc_dev(device);
429 	else
430 		sas_dev = hisi_sas_alloc_dev(device);
431 	if (!sas_dev) {
432 		dev_err(dev, "fail alloc dev: max support %d devices\n",
433 			HISI_SAS_MAX_DEVICES);
434 		return -EINVAL;
435 	}
436 
437 	device->lldd_dev = sas_dev;
438 	hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
439 
440 	if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
441 		int phy_no;
442 		u8 phy_num = parent_dev->ex_dev.num_phys;
443 		struct ex_phy *phy;
444 
445 		for (phy_no = 0; phy_no < phy_num; phy_no++) {
446 			phy = &parent_dev->ex_dev.ex_phy[phy_no];
447 			if (SAS_ADDR(phy->attached_sas_addr) ==
448 				SAS_ADDR(device->sas_addr)) {
449 				sas_dev->attached_phy = phy_no;
450 				break;
451 			}
452 		}
453 
454 		if (phy_no == phy_num) {
455 			dev_info(dev, "dev found: no attached "
456 				 "dev:%016llx at ex:%016llx\n",
457 				 SAS_ADDR(device->sas_addr),
458 				 SAS_ADDR(parent_dev->sas_addr));
459 			return -EINVAL;
460 		}
461 	}
462 
463 	return 0;
464 }
465 
466 static int hisi_sas_slave_configure(struct scsi_device *sdev)
467 {
468 	struct domain_device *dev = sdev_to_domain_dev(sdev);
469 	int ret = sas_slave_configure(sdev);
470 
471 	if (ret)
472 		return ret;
473 	if (!dev_is_sata(dev))
474 		sas_change_queue_depth(sdev, 64);
475 
476 	return 0;
477 }
478 
479 static void hisi_sas_scan_start(struct Scsi_Host *shost)
480 {
481 	struct hisi_hba *hisi_hba = shost_priv(shost);
482 	int i;
483 
484 	for (i = 0; i < hisi_hba->n_phy; ++i)
485 		hisi_sas_bytes_dmaed(hisi_hba, i);
486 
487 	hisi_hba->scan_finished = 1;
488 }
489 
490 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
491 {
492 	struct hisi_hba *hisi_hba = shost_priv(shost);
493 	struct sas_ha_struct *sha = &hisi_hba->sha;
494 
495 	if (hisi_hba->scan_finished == 0)
496 		return 0;
497 
498 	sas_drain_work(sha);
499 	return 1;
500 }
501 
502 static void hisi_sas_phyup_work(struct work_struct *work)
503 {
504 	struct hisi_sas_phy *phy =
505 		container_of(work, struct hisi_sas_phy, phyup_ws);
506 	struct hisi_hba *hisi_hba = phy->hisi_hba;
507 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
508 	int phy_no = sas_phy->id;
509 
510 	hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
511 	hisi_sas_bytes_dmaed(hisi_hba, phy_no);
512 }
513 
514 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
515 {
516 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
517 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
518 
519 	phy->hisi_hba = hisi_hba;
520 	phy->port = NULL;
521 	init_timer(&phy->timer);
522 	sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
523 	sas_phy->class = SAS;
524 	sas_phy->iproto = SAS_PROTOCOL_ALL;
525 	sas_phy->tproto = 0;
526 	sas_phy->type = PHY_TYPE_PHYSICAL;
527 	sas_phy->role = PHY_ROLE_INITIATOR;
528 	sas_phy->oob_mode = OOB_NOT_CONNECTED;
529 	sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
530 	sas_phy->id = phy_no;
531 	sas_phy->sas_addr = &hisi_hba->sas_addr[0];
532 	sas_phy->frame_rcvd = &phy->frame_rcvd[0];
533 	sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
534 	sas_phy->lldd_phy = phy;
535 
536 	INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
537 }
538 
539 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
540 {
541 	struct sas_ha_struct *sas_ha = sas_phy->ha;
542 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
543 	struct hisi_sas_phy *phy = sas_phy->lldd_phy;
544 	struct asd_sas_port *sas_port = sas_phy->port;
545 	struct hisi_sas_port *port = &hisi_hba->port[phy->port_id];
546 	unsigned long flags;
547 
548 	if (!sas_port)
549 		return;
550 
551 	spin_lock_irqsave(&hisi_hba->lock, flags);
552 	port->port_attached = 1;
553 	port->id = phy->port_id;
554 	phy->port = port;
555 	sas_port->lldd_port = port;
556 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
557 }
558 
559 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no,
560 				     struct domain_device *device)
561 {
562 	struct hisi_sas_phy *phy;
563 	struct hisi_sas_port *port;
564 	struct hisi_sas_slot *slot, *slot2;
565 	struct device *dev = &hisi_hba->pdev->dev;
566 
567 	phy = &hisi_hba->phy[phy_no];
568 	port = phy->port;
569 	if (!port)
570 		return;
571 
572 	list_for_each_entry_safe(slot, slot2, &port->list, entry) {
573 		struct sas_task *task;
574 
575 		task = slot->task;
576 		if (device && task->dev != device)
577 			continue;
578 
579 		dev_info(dev, "Release slot [%d:%d], task [%p]:\n",
580 			 slot->dlvry_queue, slot->dlvry_queue_slot, task);
581 		hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
582 	}
583 }
584 
585 static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy)
586 {
587 	struct domain_device *device;
588 	struct hisi_sas_phy *phy = sas_phy->lldd_phy;
589 	struct asd_sas_port *sas_port = sas_phy->port;
590 
591 	list_for_each_entry(device, &sas_port->dev_list, dev_list_node)
592 		hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device);
593 }
594 
595 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
596 			struct domain_device *device)
597 {
598 	struct asd_sas_port *port = device->port;
599 	struct asd_sas_phy *sas_phy;
600 
601 	list_for_each_entry(sas_phy, &port->phy_list, port_phy_el)
602 		hisi_sas_do_release_task(hisi_hba, sas_phy->id, device);
603 }
604 
605 static void hisi_sas_dev_gone(struct domain_device *device)
606 {
607 	struct hisi_sas_device *sas_dev = device->lldd_dev;
608 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
609 	struct device *dev = &hisi_hba->pdev->dev;
610 	u64 dev_id = sas_dev->device_id;
611 
612 	dev_info(dev, "found dev[%lld:%x] is gone\n",
613 		 sas_dev->device_id, sas_dev->dev_type);
614 
615 	hisi_sas_internal_task_abort(hisi_hba, device,
616 				     HISI_SAS_INT_ABT_DEV, 0);
617 
618 	hisi_hba->hw->free_device(hisi_hba, sas_dev);
619 	device->lldd_dev = NULL;
620 	memset(sas_dev, 0, sizeof(*sas_dev));
621 	sas_dev->device_id = dev_id;
622 	sas_dev->dev_type = SAS_PHY_UNUSED;
623 	sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
624 }
625 
626 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
627 {
628 	return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
629 }
630 
631 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
632 				void *funcdata)
633 {
634 	struct sas_ha_struct *sas_ha = sas_phy->ha;
635 	struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
636 	int phy_no = sas_phy->id;
637 
638 	switch (func) {
639 	case PHY_FUNC_HARD_RESET:
640 		hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
641 		break;
642 
643 	case PHY_FUNC_LINK_RESET:
644 		hisi_hba->hw->phy_enable(hisi_hba, phy_no);
645 		hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
646 		break;
647 
648 	case PHY_FUNC_DISABLE:
649 		hisi_hba->hw->phy_disable(hisi_hba, phy_no);
650 		break;
651 
652 	case PHY_FUNC_SET_LINK_RATE:
653 		hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
654 		break;
655 
656 	case PHY_FUNC_RELEASE_SPINUP_HOLD:
657 	default:
658 		return -EOPNOTSUPP;
659 	}
660 	return 0;
661 }
662 
663 static void hisi_sas_task_done(struct sas_task *task)
664 {
665 	if (!del_timer(&task->slow_task->timer))
666 		return;
667 	complete(&task->slow_task->completion);
668 }
669 
670 static void hisi_sas_tmf_timedout(unsigned long data)
671 {
672 	struct sas_task *task = (struct sas_task *)data;
673 
674 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
675 	complete(&task->slow_task->completion);
676 }
677 
678 #define TASK_TIMEOUT 20
679 #define TASK_RETRY 3
680 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
681 					   void *parameter, u32 para_len,
682 					   struct hisi_sas_tmf_task *tmf)
683 {
684 	struct hisi_sas_device *sas_dev = device->lldd_dev;
685 	struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
686 	struct device *dev = &hisi_hba->pdev->dev;
687 	struct sas_task *task;
688 	int res, retry;
689 
690 	for (retry = 0; retry < TASK_RETRY; retry++) {
691 		task = sas_alloc_slow_task(GFP_KERNEL);
692 		if (!task)
693 			return -ENOMEM;
694 
695 		task->dev = device;
696 		task->task_proto = device->tproto;
697 
698 		memcpy(&task->ssp_task, parameter, para_len);
699 		task->task_done = hisi_sas_task_done;
700 
701 		task->slow_task->timer.data = (unsigned long) task;
702 		task->slow_task->timer.function = hisi_sas_tmf_timedout;
703 		task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
704 		add_timer(&task->slow_task->timer);
705 
706 		res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
707 
708 		if (res) {
709 			del_timer(&task->slow_task->timer);
710 			dev_err(dev, "abort tmf: executing internal task failed: %d\n",
711 				res);
712 			goto ex_err;
713 		}
714 
715 		wait_for_completion(&task->slow_task->completion);
716 		res = TMF_RESP_FUNC_FAILED;
717 		/* Even TMF timed out, return direct. */
718 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
719 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
720 				dev_err(dev, "abort tmf: TMF task[%d] timeout\n",
721 					tmf->tag_of_task_to_be_managed);
722 				if (task->lldd_task) {
723 					struct hisi_sas_slot *slot =
724 						task->lldd_task;
725 
726 					hisi_sas_slot_task_free(hisi_hba,
727 								task, slot);
728 				}
729 
730 				goto ex_err;
731 			}
732 		}
733 
734 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
735 		     task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
736 			res = TMF_RESP_FUNC_COMPLETE;
737 			break;
738 		}
739 
740 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
741 			task->task_status.stat == TMF_RESP_FUNC_SUCC) {
742 			res = TMF_RESP_FUNC_SUCC;
743 			break;
744 		}
745 
746 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
747 		      task->task_status.stat == SAS_DATA_UNDERRUN) {
748 			/* no error, but return the number of bytes of
749 			 * underrun
750 			 */
751 			dev_warn(dev, "abort tmf: task to dev %016llx "
752 				 "resp: 0x%x sts 0x%x underrun\n",
753 				 SAS_ADDR(device->sas_addr),
754 				 task->task_status.resp,
755 				 task->task_status.stat);
756 			res = task->task_status.residual;
757 			break;
758 		}
759 
760 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
761 			task->task_status.stat == SAS_DATA_OVERRUN) {
762 			dev_warn(dev, "abort tmf: blocked task error\n");
763 			res = -EMSGSIZE;
764 			break;
765 		}
766 
767 		dev_warn(dev, "abort tmf: task to dev "
768 			 "%016llx resp: 0x%x status 0x%x\n",
769 			 SAS_ADDR(device->sas_addr), task->task_status.resp,
770 			 task->task_status.stat);
771 		sas_free_task(task);
772 		task = NULL;
773 	}
774 ex_err:
775 	if (retry == TASK_RETRY)
776 		dev_warn(dev, "abort tmf: executing internal task failed!\n");
777 	sas_free_task(task);
778 	return res;
779 }
780 
781 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
782 				u8 *lun, struct hisi_sas_tmf_task *tmf)
783 {
784 	struct sas_ssp_task ssp_task;
785 
786 	if (!(device->tproto & SAS_PROTOCOL_SSP))
787 		return TMF_RESP_FUNC_ESUPP;
788 
789 	memcpy(ssp_task.LUN, lun, 8);
790 
791 	return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
792 				sizeof(ssp_task), tmf);
793 }
794 
795 static int hisi_sas_abort_task(struct sas_task *task)
796 {
797 	struct scsi_lun lun;
798 	struct hisi_sas_tmf_task tmf_task;
799 	struct domain_device *device = task->dev;
800 	struct hisi_sas_device *sas_dev = device->lldd_dev;
801 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
802 	struct device *dev = &hisi_hba->pdev->dev;
803 	int rc = TMF_RESP_FUNC_FAILED;
804 	unsigned long flags;
805 
806 	if (!sas_dev) {
807 		dev_warn(dev, "Device has been removed\n");
808 		return TMF_RESP_FUNC_FAILED;
809 	}
810 
811 	spin_lock_irqsave(&task->task_state_lock, flags);
812 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
813 		spin_unlock_irqrestore(&task->task_state_lock, flags);
814 		rc = TMF_RESP_FUNC_COMPLETE;
815 		goto out;
816 	}
817 
818 	spin_unlock_irqrestore(&task->task_state_lock, flags);
819 	sas_dev->dev_status = HISI_SAS_DEV_EH;
820 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
821 		struct scsi_cmnd *cmnd = task->uldd_task;
822 		struct hisi_sas_slot *slot = task->lldd_task;
823 		u32 tag = slot->idx;
824 
825 		int_to_scsilun(cmnd->device->lun, &lun);
826 		tmf_task.tmf = TMF_ABORT_TASK;
827 		tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
828 
829 		rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
830 						  &tmf_task);
831 
832 		/* if successful, clear the task and callback forwards.*/
833 		if (rc == TMF_RESP_FUNC_COMPLETE) {
834 			if (task->lldd_task) {
835 				struct hisi_sas_slot *slot;
836 
837 				slot = &hisi_hba->slot_info
838 					[tmf_task.tag_of_task_to_be_managed];
839 				spin_lock_irqsave(&hisi_hba->lock, flags);
840 				hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
841 				spin_unlock_irqrestore(&hisi_hba->lock, flags);
842 			}
843 		}
844 
845 		hisi_sas_internal_task_abort(hisi_hba, device,
846 					     HISI_SAS_INT_ABT_CMD, tag);
847 	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
848 		task->task_proto & SAS_PROTOCOL_STP) {
849 		if (task->dev->dev_type == SAS_SATA_DEV) {
850 			hisi_sas_internal_task_abort(hisi_hba, device,
851 						     HISI_SAS_INT_ABT_DEV, 0);
852 			rc = TMF_RESP_FUNC_COMPLETE;
853 		}
854 	} else if (task->task_proto & SAS_PROTOCOL_SMP) {
855 		/* SMP */
856 		struct hisi_sas_slot *slot = task->lldd_task;
857 		u32 tag = slot->idx;
858 
859 		hisi_sas_internal_task_abort(hisi_hba, device,
860 					     HISI_SAS_INT_ABT_CMD, tag);
861 	}
862 
863 out:
864 	if (rc != TMF_RESP_FUNC_COMPLETE)
865 		dev_notice(dev, "abort task: rc=%d\n", rc);
866 	return rc;
867 }
868 
869 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
870 {
871 	struct hisi_sas_tmf_task tmf_task;
872 	int rc = TMF_RESP_FUNC_FAILED;
873 
874 	tmf_task.tmf = TMF_ABORT_TASK_SET;
875 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
876 
877 	return rc;
878 }
879 
880 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
881 {
882 	int rc = TMF_RESP_FUNC_FAILED;
883 	struct hisi_sas_tmf_task tmf_task;
884 
885 	tmf_task.tmf = TMF_CLEAR_ACA;
886 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
887 
888 	return rc;
889 }
890 
891 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
892 {
893 	struct sas_phy *phy = sas_get_local_phy(device);
894 	int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
895 			(device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
896 	rc = sas_phy_reset(phy, reset_type);
897 	sas_put_local_phy(phy);
898 	msleep(2000);
899 	return rc;
900 }
901 
902 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
903 {
904 	struct hisi_sas_device *sas_dev = device->lldd_dev;
905 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
906 	unsigned long flags;
907 	int rc = TMF_RESP_FUNC_FAILED;
908 
909 	if (sas_dev->dev_status != HISI_SAS_DEV_EH)
910 		return TMF_RESP_FUNC_FAILED;
911 	sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
912 
913 	rc = hisi_sas_debug_I_T_nexus_reset(device);
914 
915 	spin_lock_irqsave(&hisi_hba->lock, flags);
916 	hisi_sas_release_task(hisi_hba, device);
917 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
918 
919 	return 0;
920 }
921 
922 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
923 {
924 	struct hisi_sas_tmf_task tmf_task;
925 	struct hisi_sas_device *sas_dev = device->lldd_dev;
926 	struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
927 	struct device *dev = &hisi_hba->pdev->dev;
928 	unsigned long flags;
929 	int rc = TMF_RESP_FUNC_FAILED;
930 
931 	tmf_task.tmf = TMF_LU_RESET;
932 	sas_dev->dev_status = HISI_SAS_DEV_EH;
933 	rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
934 	if (rc == TMF_RESP_FUNC_COMPLETE) {
935 		spin_lock_irqsave(&hisi_hba->lock, flags);
936 		hisi_sas_release_task(hisi_hba, device);
937 		spin_unlock_irqrestore(&hisi_hba->lock, flags);
938 	}
939 
940 	/* If failed, fall-through I_T_Nexus reset */
941 	dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
942 		sas_dev->device_id, rc);
943 	return rc;
944 }
945 
946 static int hisi_sas_query_task(struct sas_task *task)
947 {
948 	struct scsi_lun lun;
949 	struct hisi_sas_tmf_task tmf_task;
950 	int rc = TMF_RESP_FUNC_FAILED;
951 
952 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
953 		struct scsi_cmnd *cmnd = task->uldd_task;
954 		struct domain_device *device = task->dev;
955 		struct hisi_sas_slot *slot = task->lldd_task;
956 		u32 tag = slot->idx;
957 
958 		int_to_scsilun(cmnd->device->lun, &lun);
959 		tmf_task.tmf = TMF_QUERY_TASK;
960 		tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
961 
962 		rc = hisi_sas_debug_issue_ssp_tmf(device,
963 						  lun.scsi_lun,
964 						  &tmf_task);
965 		switch (rc) {
966 		/* The task is still in Lun, release it then */
967 		case TMF_RESP_FUNC_SUCC:
968 		/* The task is not in Lun or failed, reset the phy */
969 		case TMF_RESP_FUNC_FAILED:
970 		case TMF_RESP_FUNC_COMPLETE:
971 			break;
972 		default:
973 			rc = TMF_RESP_FUNC_FAILED;
974 			break;
975 		}
976 	}
977 	return rc;
978 }
979 
980 static int
981 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
982 				  struct sas_task *task, int abort_flag,
983 				  int task_tag)
984 {
985 	struct domain_device *device = task->dev;
986 	struct hisi_sas_device *sas_dev = device->lldd_dev;
987 	struct device *dev = &hisi_hba->pdev->dev;
988 	struct hisi_sas_port *port;
989 	struct hisi_sas_slot *slot;
990 	struct hisi_sas_cmd_hdr *cmd_hdr_base;
991 	int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
992 
993 	if (!device->port)
994 		return -1;
995 
996 	port = device->port->lldd_port;
997 
998 	/* simply get a slot and send abort command */
999 	rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1000 	if (rc)
1001 		goto err_out;
1002 	rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
1003 					&dlvry_queue, &dlvry_queue_slot);
1004 	if (rc)
1005 		goto err_out_tag;
1006 
1007 	slot = &hisi_hba->slot_info[slot_idx];
1008 	memset(slot, 0, sizeof(struct hisi_sas_slot));
1009 
1010 	slot->idx = slot_idx;
1011 	slot->n_elem = n_elem;
1012 	slot->dlvry_queue = dlvry_queue;
1013 	slot->dlvry_queue_slot = dlvry_queue_slot;
1014 	cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1015 	slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1016 	slot->task = task;
1017 	slot->port = port;
1018 	task->lldd_task = slot;
1019 
1020 	memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1021 
1022 	rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1023 				      abort_flag, task_tag);
1024 	if (rc)
1025 		goto err_out_tag;
1026 
1027 	/* Port structure is static for the HBA, so
1028 	*  even if the port is deformed it is ok
1029 	*  to reference.
1030 	*/
1031 	list_add_tail(&slot->entry, &port->list);
1032 	spin_lock(&task->task_state_lock);
1033 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1034 	spin_unlock(&task->task_state_lock);
1035 
1036 	hisi_hba->slot_prep = slot;
1037 
1038 	atomic64_inc(&sas_dev->running_req);
1039 
1040 	/* send abort command to our chip */
1041 	hisi_hba->hw->start_delivery(hisi_hba);
1042 
1043 	return 0;
1044 
1045 err_out_tag:
1046 	hisi_sas_slot_index_free(hisi_hba, slot_idx);
1047 err_out:
1048 	dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1049 
1050 	return rc;
1051 }
1052 
1053 /**
1054  * hisi_sas_internal_task_abort -- execute an internal
1055  * abort command for single IO command or a device
1056  * @hisi_hba: host controller struct
1057  * @device: domain device
1058  * @abort_flag: mode of operation, device or single IO
1059  * @tag: tag of IO to be aborted (only relevant to single
1060  *       IO mode)
1061  */
1062 static int
1063 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1064 			     struct domain_device *device,
1065 			     int abort_flag, int tag)
1066 {
1067 	struct sas_task *task;
1068 	struct hisi_sas_device *sas_dev = device->lldd_dev;
1069 	struct device *dev = &hisi_hba->pdev->dev;
1070 	int res;
1071 	unsigned long flags;
1072 
1073 	if (!hisi_hba->hw->prep_abort)
1074 		return -EOPNOTSUPP;
1075 
1076 	task = sas_alloc_slow_task(GFP_KERNEL);
1077 	if (!task)
1078 		return -ENOMEM;
1079 
1080 	task->dev = device;
1081 	task->task_proto = device->tproto;
1082 	task->task_done = hisi_sas_task_done;
1083 	task->slow_task->timer.data = (unsigned long)task;
1084 	task->slow_task->timer.function = hisi_sas_tmf_timedout;
1085 	task->slow_task->timer.expires = jiffies + 20*HZ;
1086 	add_timer(&task->slow_task->timer);
1087 
1088 	/* Lock as we are alloc'ing a slot, which cannot be interrupted */
1089 	spin_lock_irqsave(&hisi_hba->lock, flags);
1090 	res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1091 						task, abort_flag, tag);
1092 	spin_unlock_irqrestore(&hisi_hba->lock, flags);
1093 	if (res) {
1094 		del_timer(&task->slow_task->timer);
1095 		dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1096 			res);
1097 		goto exit;
1098 	}
1099 	wait_for_completion(&task->slow_task->completion);
1100 	res = TMF_RESP_FUNC_FAILED;
1101 
1102 	if (task->task_status.resp == SAS_TASK_COMPLETE &&
1103 		task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1104 		res = TMF_RESP_FUNC_COMPLETE;
1105 		goto exit;
1106 	}
1107 
1108 	/* TMF timed out, return direct. */
1109 	if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1110 		if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1111 			dev_err(dev, "internal task abort: timeout.\n");
1112 			if (task->lldd_task) {
1113 				struct hisi_sas_slot *slot = task->lldd_task;
1114 
1115 				hisi_sas_slot_task_free(hisi_hba, task, slot);
1116 			}
1117 		}
1118 	}
1119 
1120 exit:
1121 	dev_info(dev, "internal task abort: task to dev %016llx task=%p "
1122 		"resp: 0x%x sts 0x%x\n",
1123 		SAS_ADDR(device->sas_addr),
1124 		task,
1125 		task->task_status.resp, /* 0 is complete, -1 is undelivered */
1126 		task->task_status.stat);
1127 	sas_free_task(task);
1128 
1129 	return res;
1130 }
1131 
1132 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1133 {
1134 	hisi_sas_port_notify_formed(sas_phy);
1135 }
1136 
1137 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1138 {
1139 	hisi_sas_port_notify_deformed(sas_phy);
1140 }
1141 
1142 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1143 {
1144 	phy->phy_attached = 0;
1145 	phy->phy_type = 0;
1146 	phy->port = NULL;
1147 }
1148 
1149 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1150 {
1151 	struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1152 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1153 	struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1154 
1155 	if (rdy) {
1156 		/* Phy down but ready */
1157 		hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1158 		hisi_sas_port_notify_formed(sas_phy);
1159 	} else {
1160 		struct hisi_sas_port *port  = phy->port;
1161 
1162 		/* Phy down and not ready */
1163 		sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1164 		sas_phy_disconnected(sas_phy);
1165 
1166 		if (port) {
1167 			if (phy->phy_type & PORT_TYPE_SAS) {
1168 				int port_id = port->id;
1169 
1170 				if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1171 								       port_id))
1172 					port->port_attached = 0;
1173 			} else if (phy->phy_type & PORT_TYPE_SATA)
1174 				port->port_attached = 0;
1175 		}
1176 		hisi_sas_phy_disconnected(phy);
1177 	}
1178 }
1179 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1180 
1181 static struct scsi_transport_template *hisi_sas_stt;
1182 
1183 static struct scsi_host_template hisi_sas_sht = {
1184 	.module			= THIS_MODULE,
1185 	.name			= DRV_NAME,
1186 	.queuecommand		= sas_queuecommand,
1187 	.target_alloc		= sas_target_alloc,
1188 	.slave_configure	= hisi_sas_slave_configure,
1189 	.scan_finished		= hisi_sas_scan_finished,
1190 	.scan_start		= hisi_sas_scan_start,
1191 	.change_queue_depth	= sas_change_queue_depth,
1192 	.bios_param		= sas_bios_param,
1193 	.can_queue		= 1,
1194 	.this_id		= -1,
1195 	.sg_tablesize		= SG_ALL,
1196 	.max_sectors		= SCSI_DEFAULT_MAX_SECTORS,
1197 	.use_clustering		= ENABLE_CLUSTERING,
1198 	.eh_device_reset_handler = sas_eh_device_reset_handler,
1199 	.eh_bus_reset_handler	= sas_eh_bus_reset_handler,
1200 	.target_destroy		= sas_target_destroy,
1201 	.ioctl			= sas_ioctl,
1202 };
1203 
1204 static struct sas_domain_function_template hisi_sas_transport_ops = {
1205 	.lldd_dev_found		= hisi_sas_dev_found,
1206 	.lldd_dev_gone		= hisi_sas_dev_gone,
1207 	.lldd_execute_task	= hisi_sas_queue_command,
1208 	.lldd_control_phy	= hisi_sas_control_phy,
1209 	.lldd_abort_task	= hisi_sas_abort_task,
1210 	.lldd_abort_task_set	= hisi_sas_abort_task_set,
1211 	.lldd_clear_aca		= hisi_sas_clear_aca,
1212 	.lldd_I_T_nexus_reset	= hisi_sas_I_T_nexus_reset,
1213 	.lldd_lu_reset		= hisi_sas_lu_reset,
1214 	.lldd_query_task	= hisi_sas_query_task,
1215 	.lldd_port_formed	= hisi_sas_port_formed,
1216 	.lldd_port_deformed	= hisi_sas_port_deformed,
1217 };
1218 
1219 static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1220 {
1221 	struct platform_device *pdev = hisi_hba->pdev;
1222 	struct device *dev = &pdev->dev;
1223 	int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1224 
1225 	spin_lock_init(&hisi_hba->lock);
1226 	for (i = 0; i < hisi_hba->n_phy; i++) {
1227 		hisi_sas_phy_init(hisi_hba, i);
1228 		hisi_hba->port[i].port_attached = 0;
1229 		hisi_hba->port[i].id = -1;
1230 		INIT_LIST_HEAD(&hisi_hba->port[i].list);
1231 	}
1232 
1233 	for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1234 		hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1235 		hisi_hba->devices[i].device_id = i;
1236 		hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1237 	}
1238 
1239 	for (i = 0; i < hisi_hba->queue_count; i++) {
1240 		struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1241 		struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1242 
1243 		/* Completion queue structure */
1244 		cq->id = i;
1245 		cq->hisi_hba = hisi_hba;
1246 
1247 		/* Delivery queue structure */
1248 		dq->id = i;
1249 		dq->hisi_hba = hisi_hba;
1250 
1251 		/* Delivery queue */
1252 		s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1253 		hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1254 					&hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1255 		if (!hisi_hba->cmd_hdr[i])
1256 			goto err_out;
1257 		memset(hisi_hba->cmd_hdr[i], 0, s);
1258 
1259 		/* Completion queue */
1260 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1261 		hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1262 				&hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1263 		if (!hisi_hba->complete_hdr[i])
1264 			goto err_out;
1265 		memset(hisi_hba->complete_hdr[i], 0, s);
1266 	}
1267 
1268 	s = HISI_SAS_STATUS_BUF_SZ;
1269 	hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1270 						       dev, s, 16, 0);
1271 	if (!hisi_hba->status_buffer_pool)
1272 		goto err_out;
1273 
1274 	s = HISI_SAS_COMMAND_TABLE_SZ;
1275 	hisi_hba->command_table_pool = dma_pool_create("command_table",
1276 						       dev, s, 16, 0);
1277 	if (!hisi_hba->command_table_pool)
1278 		goto err_out;
1279 
1280 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1281 	hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1282 					    GFP_KERNEL);
1283 	if (!hisi_hba->itct)
1284 		goto err_out;
1285 
1286 	memset(hisi_hba->itct, 0, s);
1287 
1288 	hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1289 					   sizeof(struct hisi_sas_slot),
1290 					   GFP_KERNEL);
1291 	if (!hisi_hba->slot_info)
1292 		goto err_out;
1293 
1294 	s = max_command_entries * sizeof(struct hisi_sas_iost);
1295 	hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1296 					    GFP_KERNEL);
1297 	if (!hisi_hba->iost)
1298 		goto err_out;
1299 
1300 	memset(hisi_hba->iost, 0, s);
1301 
1302 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1303 	hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1304 				&hisi_hba->breakpoint_dma, GFP_KERNEL);
1305 	if (!hisi_hba->breakpoint)
1306 		goto err_out;
1307 
1308 	memset(hisi_hba->breakpoint, 0, s);
1309 
1310 	hisi_hba->slot_index_count = max_command_entries;
1311 	s = hisi_hba->slot_index_count / BITS_PER_BYTE;
1312 	hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1313 	if (!hisi_hba->slot_index_tags)
1314 		goto err_out;
1315 
1316 	hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1317 				sizeof(struct hisi_sas_sge_page), 16, 0);
1318 	if (!hisi_hba->sge_page_pool)
1319 		goto err_out;
1320 
1321 	s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1322 	hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1323 				&hisi_hba->initial_fis_dma, GFP_KERNEL);
1324 	if (!hisi_hba->initial_fis)
1325 		goto err_out;
1326 	memset(hisi_hba->initial_fis, 0, s);
1327 
1328 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1329 	hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1330 				&hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1331 	if (!hisi_hba->sata_breakpoint)
1332 		goto err_out;
1333 	memset(hisi_hba->sata_breakpoint, 0, s);
1334 
1335 	hisi_sas_slot_index_init(hisi_hba);
1336 
1337 	hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1338 	if (!hisi_hba->wq) {
1339 		dev_err(dev, "sas_alloc: failed to create workqueue\n");
1340 		goto err_out;
1341 	}
1342 
1343 	return 0;
1344 err_out:
1345 	return -ENOMEM;
1346 }
1347 
1348 static void hisi_sas_free(struct hisi_hba *hisi_hba)
1349 {
1350 	struct device *dev = &hisi_hba->pdev->dev;
1351 	int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1352 
1353 	for (i = 0; i < hisi_hba->queue_count; i++) {
1354 		s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1355 		if (hisi_hba->cmd_hdr[i])
1356 			dma_free_coherent(dev, s,
1357 					  hisi_hba->cmd_hdr[i],
1358 					  hisi_hba->cmd_hdr_dma[i]);
1359 
1360 		s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1361 		if (hisi_hba->complete_hdr[i])
1362 			dma_free_coherent(dev, s,
1363 					  hisi_hba->complete_hdr[i],
1364 					  hisi_hba->complete_hdr_dma[i]);
1365 	}
1366 
1367 	dma_pool_destroy(hisi_hba->status_buffer_pool);
1368 	dma_pool_destroy(hisi_hba->command_table_pool);
1369 	dma_pool_destroy(hisi_hba->sge_page_pool);
1370 
1371 	s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1372 	if (hisi_hba->itct)
1373 		dma_free_coherent(dev, s,
1374 				  hisi_hba->itct, hisi_hba->itct_dma);
1375 
1376 	s = max_command_entries * sizeof(struct hisi_sas_iost);
1377 	if (hisi_hba->iost)
1378 		dma_free_coherent(dev, s,
1379 				  hisi_hba->iost, hisi_hba->iost_dma);
1380 
1381 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1382 	if (hisi_hba->breakpoint)
1383 		dma_free_coherent(dev, s,
1384 				  hisi_hba->breakpoint,
1385 				  hisi_hba->breakpoint_dma);
1386 
1387 
1388 	s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1389 	if (hisi_hba->initial_fis)
1390 		dma_free_coherent(dev, s,
1391 				  hisi_hba->initial_fis,
1392 				  hisi_hba->initial_fis_dma);
1393 
1394 	s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1395 	if (hisi_hba->sata_breakpoint)
1396 		dma_free_coherent(dev, s,
1397 				  hisi_hba->sata_breakpoint,
1398 				  hisi_hba->sata_breakpoint_dma);
1399 
1400 	if (hisi_hba->wq)
1401 		destroy_workqueue(hisi_hba->wq);
1402 }
1403 
1404 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1405 					      const struct hisi_sas_hw *hw)
1406 {
1407 	struct resource *res;
1408 	struct Scsi_Host *shost;
1409 	struct hisi_hba *hisi_hba;
1410 	struct device *dev = &pdev->dev;
1411 	struct device_node *np = pdev->dev.of_node;
1412 	struct clk *refclk;
1413 
1414 	shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
1415 	if (!shost) {
1416 		dev_err(dev, "scsi host alloc failed\n");
1417 		return NULL;
1418 	}
1419 	hisi_hba = shost_priv(shost);
1420 
1421 	hisi_hba->hw = hw;
1422 	hisi_hba->pdev = pdev;
1423 	hisi_hba->shost = shost;
1424 	SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1425 
1426 	init_timer(&hisi_hba->timer);
1427 
1428 	if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1429 					  SAS_ADDR_SIZE))
1430 		goto err_out;
1431 
1432 	if (np) {
1433 		hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1434 					"hisilicon,sas-syscon");
1435 		if (IS_ERR(hisi_hba->ctrl))
1436 			goto err_out;
1437 
1438 		if (device_property_read_u32(dev, "ctrl-reset-reg",
1439 					     &hisi_hba->ctrl_reset_reg))
1440 			goto err_out;
1441 
1442 		if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1443 					     &hisi_hba->ctrl_reset_sts_reg))
1444 			goto err_out;
1445 
1446 		if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1447 					     &hisi_hba->ctrl_clock_ena_reg))
1448 			goto err_out;
1449 	}
1450 
1451 	refclk = devm_clk_get(&pdev->dev, NULL);
1452 	if (IS_ERR(refclk))
1453 		dev_info(dev, "no ref clk property\n");
1454 	else
1455 		hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1456 
1457 	if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
1458 		goto err_out;
1459 
1460 	if (device_property_read_u32(dev, "queue-count",
1461 				     &hisi_hba->queue_count))
1462 		goto err_out;
1463 
1464 	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1465 	    dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1466 		dev_err(dev, "No usable DMA addressing method\n");
1467 		goto err_out;
1468 	}
1469 
1470 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1471 	hisi_hba->regs = devm_ioremap_resource(dev, res);
1472 	if (IS_ERR(hisi_hba->regs))
1473 		goto err_out;
1474 
1475 	if (hisi_sas_alloc(hisi_hba, shost)) {
1476 		hisi_sas_free(hisi_hba);
1477 		goto err_out;
1478 	}
1479 
1480 	return shost;
1481 err_out:
1482 	kfree(shost);
1483 	dev_err(dev, "shost alloc failed\n");
1484 	return NULL;
1485 }
1486 
1487 static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1488 {
1489 	int i;
1490 
1491 	for (i = 0; i < hisi_hba->n_phy; i++)
1492 		memcpy(&hisi_hba->phy[i].dev_sas_addr,
1493 		       hisi_hba->sas_addr,
1494 		       SAS_ADDR_SIZE);
1495 }
1496 
1497 int hisi_sas_probe(struct platform_device *pdev,
1498 			 const struct hisi_sas_hw *hw)
1499 {
1500 	struct Scsi_Host *shost;
1501 	struct hisi_hba *hisi_hba;
1502 	struct device *dev = &pdev->dev;
1503 	struct asd_sas_phy **arr_phy;
1504 	struct asd_sas_port **arr_port;
1505 	struct sas_ha_struct *sha;
1506 	int rc, phy_nr, port_nr, i;
1507 
1508 	shost = hisi_sas_shost_alloc(pdev, hw);
1509 	if (!shost)
1510 		return -ENOMEM;
1511 
1512 	sha = SHOST_TO_SAS_HA(shost);
1513 	hisi_hba = shost_priv(shost);
1514 	platform_set_drvdata(pdev, sha);
1515 
1516 	phy_nr = port_nr = hisi_hba->n_phy;
1517 
1518 	arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1519 	arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
1520 	if (!arr_phy || !arr_port) {
1521 		rc = -ENOMEM;
1522 		goto err_out_ha;
1523 	}
1524 
1525 	sha->sas_phy = arr_phy;
1526 	sha->sas_port = arr_port;
1527 	sha->lldd_ha = hisi_hba;
1528 
1529 	shost->transportt = hisi_sas_stt;
1530 	shost->max_id = HISI_SAS_MAX_DEVICES;
1531 	shost->max_lun = ~0;
1532 	shost->max_channel = 1;
1533 	shost->max_cmd_len = 16;
1534 	shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
1535 	shost->can_queue = hisi_hba->hw->max_command_entries;
1536 	shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
1537 
1538 	sha->sas_ha_name = DRV_NAME;
1539 	sha->dev = &hisi_hba->pdev->dev;
1540 	sha->lldd_module = THIS_MODULE;
1541 	sha->sas_addr = &hisi_hba->sas_addr[0];
1542 	sha->num_phys = hisi_hba->n_phy;
1543 	sha->core.shost = hisi_hba->shost;
1544 
1545 	for (i = 0; i < hisi_hba->n_phy; i++) {
1546 		sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1547 		sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1548 	}
1549 
1550 	hisi_sas_init_add(hisi_hba);
1551 
1552 	rc = hisi_hba->hw->hw_init(hisi_hba);
1553 	if (rc)
1554 		goto err_out_ha;
1555 
1556 	rc = scsi_add_host(shost, &pdev->dev);
1557 	if (rc)
1558 		goto err_out_ha;
1559 
1560 	rc = sas_register_ha(sha);
1561 	if (rc)
1562 		goto err_out_register_ha;
1563 
1564 	scsi_scan_host(shost);
1565 
1566 	return 0;
1567 
1568 err_out_register_ha:
1569 	scsi_remove_host(shost);
1570 err_out_ha:
1571 	hisi_sas_free(hisi_hba);
1572 	kfree(shost);
1573 	return rc;
1574 }
1575 EXPORT_SYMBOL_GPL(hisi_sas_probe);
1576 
1577 int hisi_sas_remove(struct platform_device *pdev)
1578 {
1579 	struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1580 	struct hisi_hba *hisi_hba = sha->lldd_ha;
1581 	struct Scsi_Host *shost = sha->core.shost;
1582 
1583 	scsi_remove_host(sha->core.shost);
1584 	sas_unregister_ha(sha);
1585 	sas_remove_host(sha->core.shost);
1586 
1587 	hisi_sas_free(hisi_hba);
1588 	kfree(shost);
1589 	return 0;
1590 }
1591 EXPORT_SYMBOL_GPL(hisi_sas_remove);
1592 
1593 static __init int hisi_sas_init(void)
1594 {
1595 	pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1596 
1597 	hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1598 	if (!hisi_sas_stt)
1599 		return -ENOMEM;
1600 
1601 	return 0;
1602 }
1603 
1604 static __exit void hisi_sas_exit(void)
1605 {
1606 	sas_release_transport(hisi_sas_stt);
1607 }
1608 
1609 module_init(hisi_sas_init);
1610 module_exit(hisi_sas_exit);
1611 
1612 MODULE_VERSION(DRV_VERSION);
1613 MODULE_LICENSE("GPL");
1614 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1615 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1616 MODULE_ALIAS("platform:" DRV_NAME);
1617