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