xref: /linux/drivers/scsi/leapraid/leapraid_os.c (revision 4c461ee2b2a5a7c327fe092b41de1fcc002adc01)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2026 LeapIO Tech Inc.
4  *
5  * LeapRAID storage and RAID controller driver.
6  */
7 #include <linux/module.h>
8 
9 #include "leapraid_func.h"
10 #include "leapraid.h"
11 
12 LIST_HEAD(leapraid_adapter_list);
13 DEFINE_SPINLOCK(leapraid_adapter_lock);
14 
15 MODULE_AUTHOR(LEAPRAID_AUTHOR);
16 MODULE_DESCRIPTION(LEAPRAID_DESCRIPTION);
17 MODULE_LICENSE("GPL");
18 MODULE_VERSION(LEAPRAID_DRIVER_VERSION);
19 
20 static atomic_t leapraid_ids = ATOMIC_INIT(0);
21 
22 static int open_pcie_trace = 1;
23 module_param(open_pcie_trace, int, 0644);
24 MODULE_PARM_DESC(open_pcie_trace, "Default=1(open)/0(close)");
25 
26 static int enable_mp = 1;
27 module_param(enable_mp, int, 0444);
28 MODULE_PARM_DESC(enable_mp,
29 		 "Enable multipath on target device. default=1(enable)");
30 
31 static inline void leapraid_get_sense_data(char *sense,
32 					   struct sense_info *data)
33 {
34 	bool desc_format = (sense[0] & SCSI_SENSE_RESPONSE_CODE_MASK) >=
35 			    DESC_FORMAT_THRESHOLD;
36 
37 	if (desc_format) {
38 		data->sense_key = sense[1] & SENSE_KEY_MASK;
39 		data->asc = sense[2];
40 		data->ascq = sense[3];
41 	} else {
42 		data->sense_key = sense[2] & SENSE_KEY_MASK;
43 		data->asc = sense[12];
44 		data->ascq = sense[13];
45 	}
46 }
47 
48 static struct leapraid_adapter *pdev_to_adapter(struct pci_dev *pdev)
49 {
50 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
51 
52 	if (!shost)
53 		return NULL;
54 
55 	return shost_priv(shost);
56 }
57 
58 void leapraid_set_tm_flg(struct leapraid_adapter *adapter, u16 hdl)
59 {
60 	struct leapraid_sdev_priv *sdev_priv;
61 	struct scsi_device *sdev;
62 	bool skip = false;
63 
64 	/* Iterate over all devices. */
65 	shost_for_each_device(sdev, adapter->shost) {
66 		if (skip)
67 			continue;
68 
69 		sdev_priv = sdev->hostdata;
70 		if (!sdev_priv)
71 			continue;
72 
73 		if (sdev_priv->starget_priv->hdl == hdl) {
74 			sdev_priv->starget_priv->tm_busy = 1;
75 			skip = true;
76 		}
77 	}
78 }
79 
80 void leapraid_clear_tm_flg(struct leapraid_adapter *adapter, u16 hdl)
81 {
82 	struct leapraid_sdev_priv *sdev_priv;
83 	struct scsi_device *sdev;
84 	bool skip = false;
85 
86 	/* Iterate over all devices. */
87 	shost_for_each_device(sdev, adapter->shost) {
88 		if (skip)
89 			continue;
90 
91 		sdev_priv = sdev->hostdata;
92 		if (!sdev_priv)
93 			continue;
94 
95 		if (sdev_priv->starget_priv->hdl == hdl) {
96 			sdev_priv->starget_priv->tm_busy = 0;
97 			skip = true;
98 		}
99 	}
100 }
101 
102 static int leapraid_tm_cmd_map_status(struct leapraid_adapter *adapter,
103 				      uint channel,
104 				      uint id,
105 				      uint lun,
106 				      u8 type,
107 				      u16 taskid_task)
108 {
109 	int rc = FAILED;
110 
111 	if (taskid_task <= adapter->shost->can_queue) {
112 		switch (type) {
113 		case LEAPRAID_TM_TASKTYPE_ABRT_TASK_SET:
114 		case LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET:
115 			if (!leapraid_scmd_find_by_lun(adapter, id, lun,
116 						       channel))
117 				rc = SUCCESS;
118 			break;
119 		case LEAPRAID_TM_TASKTYPE_TARGET_RESET:
120 			if (!leapraid_scmd_find_by_tgt(adapter, id, channel))
121 				rc = SUCCESS;
122 			break;
123 		default:
124 			rc = SUCCESS;
125 		}
126 	}
127 
128 	if (taskid_task == adapter->driver_cmds.ctl_cmd.hp_taskid &&
129 	    (adapter->driver_cmds.ctl_cmd.status &
130 	     LEAPRAID_CMD_DONE ||
131 	     adapter->driver_cmds.ctl_cmd.status &
132 	     LEAPRAID_CMD_NOT_USED))
133 		rc = SUCCESS;
134 
135 	return rc;
136 }
137 
138 static int leapraid_tm_post_processing(struct leapraid_adapter *adapter,
139 				       u16 hdl, uint channel, uint id,
140 				       uint lun, u8 type, u16 taskid_task)
141 {
142 	int rc;
143 
144 	rc = leapraid_tm_cmd_map_status(adapter, channel, id, lun,
145 					type, taskid_task);
146 	if (rc == SUCCESS)
147 		return rc;
148 
149 	leapraid_mask_int(adapter);
150 	leapraid_sync_irqs(adapter, true);
151 	leapraid_unmask_int(adapter);
152 
153 	return leapraid_tm_cmd_map_status(adapter, channel, id, lun, type,
154 					  taskid_task);
155 }
156 
157 static void leapraid_build_tm_req(struct leapraid_scsi_tm_req *scsi_tm_req,
158 				  u16 hdl, uint lun, u8 type, u8 tr_method,
159 				  u16 target_taskid)
160 {
161 	memset(scsi_tm_req, 0, sizeof(*scsi_tm_req));
162 	scsi_tm_req->func = LEAPRAID_FUNC_SCSI_TMF;
163 	scsi_tm_req->dev_hdl = cpu_to_le16(hdl);
164 	scsi_tm_req->task_type = type;
165 	scsi_tm_req->msg_flg = tr_method;
166 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK ||
167 	    type == LEAPRAID_TM_TASKTYPE_QUERY_TASK)
168 		scsi_tm_req->task_mid = cpu_to_le16(target_taskid);
169 	int_to_scsilun(lun, (struct scsi_lun *)scsi_tm_req->lun);
170 }
171 
172 int leapraid_issue_tm(struct leapraid_adapter *adapter, u16 hdl, uint channel,
173 		      uint id, uint lun, u8 type,
174 		      u16 target_taskid, u8 tr_method)
175 {
176 	struct leapraid_scsi_tm_req *scsi_tm_req;
177 	struct leapraid_scsiio_req *scsiio_req;
178 	struct leapraid_io_req_tracker *io_req_tracker = NULL;
179 	u16 msix_task;
180 	u16 taskid;
181 	bool issue_reset = false;
182 	u32 db;
183 	int rc;
184 
185 	lockdep_assert_held(&adapter->driver_cmds.tm_cmd.mutex);
186 
187 	if (adapter->access_ctrl.shost_recovering ||
188 	    adapter->access_ctrl.host_removing ||
189 	    adapter->access_ctrl.pcie_recovering) {
190 		dev_info(&adapter->pdev->dev,
191 			 "%s %s: Host is recovering, skip tm command!\n",
192 			 __func__, adapter->adapter_attr.name);
193 		return FAILED;
194 	}
195 
196 	db = leapraid_readl(&adapter->iomem_base->db);
197 	if (db & LEAPRAID_DB_USED) {
198 		dev_info(&adapter->pdev->dev,
199 			 "%s Unexpected db status, issuing hard reset!\n",
200 			 adapter->adapter_attr.name);
201 		dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
202 			 __func__, __LINE__);
203 		rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
204 		return !rc ? SUCCESS : FAILED;
205 	}
206 
207 	if ((db & LEAPRAID_DB_MASK) == LEAPRAID_DB_FAULT) {
208 		dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
209 			 __func__, __LINE__);
210 		rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
211 		return !rc ? SUCCESS : FAILED;
212 	}
213 
214 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK)
215 		io_req_tracker =
216 			leapraid_get_io_tracker_from_taskid(adapter,
217 							    target_taskid);
218 
219 	adapter->driver_cmds.tm_cmd.status = LEAPRAID_CMD_PENDING;
220 	scsi_tm_req =
221 		leapraid_get_task_desc(adapter,
222 				       adapter->driver_cmds.tm_cmd.hp_taskid);
223 	leapraid_build_tm_req(scsi_tm_req, hdl, lun, type, tr_method,
224 			      target_taskid);
225 	memset(&adapter->driver_cmds.tm_cmd.reply, 0,
226 	       sizeof(struct leapraid_scsi_tm_rep));
227 	leapraid_set_tm_flg(adapter, hdl);
228 	init_completion(&adapter->driver_cmds.tm_cmd.done);
229 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK &&
230 	    io_req_tracker &&
231 	    io_req_tracker->msix_io < adapter->adapter_attr.rq_cnt)
232 		msix_task = io_req_tracker->msix_io;
233 	else
234 		msix_task = 0;
235 	taskid = adapter->driver_cmds.tm_cmd.hp_taskid;
236 	leapraid_fire_hpr_task(adapter, taskid, msix_task);
237 	wait_for_completion_timeout(&adapter->driver_cmds.tm_cmd.done,
238 				    LEAPRAID_TM_CMD_TIMEOUT * HZ);
239 	if (!(adapter->driver_cmds.tm_cmd.status & LEAPRAID_CMD_DONE)) {
240 		dev_err(&adapter->pdev->dev,
241 			"%s: TM cmd timeout, status=0x%x\n",
242 			__func__, adapter->driver_cmds.tm_cmd.status);
243 		leapraid_log_req_context(adapter, taskid, scsi_tm_req);
244 		issue_reset =
245 			leapraid_check_reset(
246 				adapter->driver_cmds.tm_cmd.status);
247 		if (issue_reset) {
248 			dev_info(&adapter->pdev->dev,
249 				 "%s:%d: call hard_reset\n",
250 				 __func__, __LINE__);
251 			rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
252 			rc = !rc ? SUCCESS : FAILED;
253 			goto out_cleanup;
254 		}
255 	}
256 
257 	leapraid_sync_irqs(adapter, false);
258 
259 	switch (type) {
260 	case LEAPRAID_TM_TASKTYPE_TARGET_RESET:
261 	case LEAPRAID_TM_TASKTYPE_ABRT_TASK_SET:
262 	case LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET:
263 		rc = leapraid_tm_post_processing(adapter, hdl, channel, id,
264 						 lun, type, target_taskid);
265 		break;
266 	case LEAPRAID_TM_TASKTYPE_ABORT_TASK:
267 		rc = SUCCESS;
268 		scsiio_req = leapraid_get_task_desc(adapter, target_taskid);
269 		if (le16_to_cpu(scsiio_req->dev_hdl) != hdl)
270 			break;
271 		dev_err(&adapter->pdev->dev, "%s: Abort failed, hdl=0x%04x\n",
272 			adapter->adapter_attr.name, hdl);
273 		rc = FAILED;
274 		break;
275 	case LEAPRAID_TM_TASKTYPE_QUERY_TASK:
276 		rc = SUCCESS;
277 		break;
278 	default:
279 		rc = FAILED;
280 		break;
281 	}
282 
283 out_cleanup:
284 	leapraid_clear_tm_flg(adapter, hdl);
285 	adapter->driver_cmds.tm_cmd.status = LEAPRAID_CMD_NOT_USED;
286 	return rc;
287 }
288 
289 int leapraid_issue_locked_tm(struct leapraid_adapter *adapter, u16 hdl,
290 			     uint channel, uint id, uint lun, u8 type,
291 			     u16 target_taskid, u8 tr_method)
292 {
293 	int rc;
294 
295 	mutex_lock(&adapter->driver_cmds.tm_cmd.mutex);
296 	rc = leapraid_issue_tm(adapter, hdl, channel, id, lun, type,
297 			       target_taskid, tr_method);
298 	mutex_unlock(&adapter->driver_cmds.tm_cmd.mutex);
299 
300 	return rc;
301 }
302 
303 void leapraid_smart_fault_detect(struct leapraid_adapter *adapter, u16 hdl)
304 {
305 	struct leapraid_starget_priv *starget_priv;
306 	struct leapraid_sas_dev *sas_dev;
307 	struct scsi_target *starget;
308 	unsigned long flags;
309 
310 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
311 	sas_dev = leapraid_hold_lock_get_sas_dev_by_hdl(adapter, hdl);
312 	if (!sas_dev) {
313 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
314 		return;
315 	}
316 
317 	starget = sas_dev->starget;
318 	starget_priv = starget ? starget->hostdata : NULL;
319 	if (!starget_priv) {
320 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
321 		goto release_sdev;
322 	}
323 
324 	if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER ||
325 	    starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
326 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
327 		goto release_sdev;
328 	}
329 
330 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
331 	leapraid_async_turn_on_led(adapter, hdl);
332 release_sdev:
333 	if (sas_dev)
334 		leapraid_sdev_put(sas_dev);
335 }
336 
337 static void leapraid_process_sense_data(struct leapraid_adapter *adapter,
338 					struct leapraid_scsiio_rep *scsiio_rep,
339 					struct scsi_cmnd *scmd, u16 taskid)
340 {
341 	struct sense_info data;
342 	const void *sense_data;
343 	u32 sz;
344 
345 	if (!(scsiio_rep->scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID))
346 		return;
347 
348 	sense_data = leapraid_get_sense_buffer(adapter, taskid);
349 	sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
350 		   le32_to_cpu(scsiio_rep->sense_count));
351 
352 	memcpy(scmd->sense_buffer, sense_data, sz);
353 	leapraid_get_sense_data(scmd->sense_buffer, &data);
354 	if (data.asc == ASC_FAILURE_PREDICTION_THRESHOLD_EXCEEDED)
355 		leapraid_smart_fault_detect(adapter,
356 					    le16_to_cpu(scsiio_rep->dev_hdl));
357 }
358 
359 static void leapraid_handle_data_underrun(
360 		struct leapraid_scsiio_rep *scsiio_rep,
361 		struct scsi_cmnd *scmd, u32 xfer_cnt)
362 {
363 	u8 scsi_status = scsiio_rep->scsi_status;
364 	u8 scsi_state = scsiio_rep->scsi_state;
365 
366 	scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status;
367 
368 	if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID)
369 		return;
370 
371 	if (xfer_cnt < scmd->underflow) {
372 		if (scsi_status == SAM_STAT_BUSY)
373 			scmd->result = SAM_STAT_BUSY;
374 		else
375 			scmd->result = DID_SOFT_ERROR <<
376 				LEAPRAID_SCSI_HOST_SHIFT;
377 	} else if (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED |
378 				 LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) {
379 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
380 	} else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED) {
381 		scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
382 	} else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
383 		scsiio_rep->scsi_state = LEAPRAID_SCSI_STATE_AUTOSENSE_VALID;
384 		scsiio_rep->scsi_status = SAM_STAT_CHECK_CONDITION;
385 		scsi_build_sense(scmd, 0, ILLEGAL_REQUEST,
386 				 LEAPRAID_SCSI_ASC_INVALID_CMD_CODE,
387 				 LEAPRAID_SCSI_ASCQ_DEFAULT);
388 	}
389 }
390 
391 static void leapraid_handle_success_status(
392 		struct leapraid_scsiio_rep *scsiio_rep,
393 		struct scsi_cmnd *scmd,
394 		u32 response_code)
395 {
396 	u8 scsi_status = scsiio_rep->scsi_status;
397 	u8 scsi_state = scsiio_rep->scsi_state;
398 
399 	scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status;
400 
401 	if (response_code == LEAPRAID_TM_RSP_INVALID_FRAME ||
402 	    (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED |
403 			   LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)))
404 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
405 	else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED)
406 		scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
407 }
408 
409 static void leapraid_scsiio_done_dispatch(
410 		struct leapraid_adapter *adapter,
411 		struct leapraid_scsiio_rep *scsiio_rep,
412 		struct leapraid_sdev_priv *sdev_priv,
413 		struct scsi_cmnd *scmd,
414 		u16 taskid, u32 response_code)
415 {
416 	u8 scsi_status = scsiio_rep->scsi_status;
417 	u8 scsi_state = scsiio_rep->scsi_state;
418 	u16 adapter_status;
419 	u32 xfer_cnt;
420 	u32 sz;
421 
422 	adapter_status = le16_to_cpu(scsiio_rep->adapter_status) &
423 				     LEAPRAID_ADAPTER_STATUS_MASK;
424 
425 	xfer_cnt = le32_to_cpu(scsiio_rep->transfer_count);
426 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
427 
428 	if (adapter_status == LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN &&
429 	    xfer_cnt == 0 &&
430 	    (scsi_status == LEAPRAID_SCSI_STATUS_BUSY ||
431 	    scsi_status == LEAPRAID_SCSI_STATUS_RESERVATION_CONFLICT ||
432 	    scsi_status == LEAPRAID_SCSI_STATUS_TASK_SET_FULL))
433 		adapter_status = LEAPRAID_ADAPTER_STATUS_SUCCESS;
434 
435 	switch (adapter_status) {
436 	case LEAPRAID_ADAPTER_STATUS_SCSI_DEVICE_NOT_THERE:
437 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
438 		break;
439 
440 	case LEAPRAID_ADAPTER_STATUS_BUSY:
441 	case LEAPRAID_ADAPTER_STATUS_INSUFFICIENT_RESOURCES:
442 		scmd->result = SAM_STAT_BUSY;
443 		break;
444 
445 	case LEAPRAID_ADAPTER_STATUS_SCSI_RESIDUAL_MISMATCH:
446 		if (xfer_cnt == 0 || scmd->underflow > xfer_cnt)
447 			scmd->result = DID_SOFT_ERROR <<
448 				LEAPRAID_SCSI_HOST_SHIFT;
449 		else
450 			scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) |
451 				scsi_status;
452 		break;
453 
454 	case LEAPRAID_ADAPTER_STATUS_SCSI_ADAPTER_TERMINATED:
455 		if (sdev_priv->block) {
456 			scmd->result = DID_TRANSPORT_DISRUPTED <<
457 				LEAPRAID_SCSI_HOST_SHIFT;
458 			return;
459 		}
460 
461 		if (scmd->device->channel == RAID_CHANNEL &&
462 		    scsi_state == (LEAPRAID_SCSI_STATE_TERMINATED |
463 				    LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) {
464 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
465 			break;
466 		}
467 
468 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
469 		break;
470 
471 	case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_TERMINATED:
472 	case LEAPRAID_ADAPTER_STATUS_SCSI_EXT_TERMINATED:
473 		scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
474 		break;
475 
476 	case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN:
477 		leapraid_handle_data_underrun(scsiio_rep, scmd, xfer_cnt);
478 		break;
479 
480 	case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_OVERRUN:
481 		scsi_set_resid(scmd, 0);
482 		leapraid_handle_success_status(scsiio_rep, scmd,
483 					       response_code);
484 		break;
485 	case LEAPRAID_ADAPTER_STATUS_SCSI_RECOVERED_ERROR:
486 	case LEAPRAID_ADAPTER_STATUS_SUCCESS:
487 		leapraid_handle_success_status(scsiio_rep, scmd,
488 					       response_code);
489 		break;
490 
491 	case LEAPRAID_ADAPTER_STATUS_SCSI_PROTOCOL_ERROR:
492 	case LEAPRAID_ADAPTER_STATUS_INTERNAL_ERROR:
493 	case LEAPRAID_ADAPTER_STATUS_SCSI_IO_DATA_ERROR:
494 	case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_MGMT_FAILED:
495 	default:
496 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
497 		break;
498 	}
499 
500 	if (!scmd->result)
501 		return;
502 
503 	scsi_print_command(scmd);
504 	dev_warn(&adapter->pdev->dev,
505 		 "SCSI I/O: hdl=0x%x, status: 0x%x, 0x%x, 0x%x\n",
506 		 le16_to_cpu(scsiio_rep->dev_hdl), adapter_status,
507 		 scsi_status, scsi_state);
508 
509 	if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID) {
510 		struct scsi_sense_hdr sshdr;
511 
512 		sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
513 			   le32_to_cpu(scsiio_rep->sense_count));
514 		if (scsi_normalize_sense(scmd->sense_buffer, sz,
515 					 &sshdr))
516 			dev_warn(&adapter->pdev->dev,
517 				 "Sense: key=0x%x asc=0x%x ascq=0x%x\n",
518 				 sshdr.sense_key, sshdr.asc,
519 				 sshdr.ascq);
520 		else
521 			dev_warn(&adapter->pdev->dev,
522 				 "Sense: Invalid sense data\n");
523 	}
524 }
525 
526 bool leapraid_scsiio_done(struct leapraid_adapter *adapter, u16 taskid,
527 			  u8 msix_index, u32 rep)
528 {
529 	struct leapraid_scsiio_rep *scsiio_rep;
530 	struct leapraid_sdev_priv *sdev_priv;
531 	struct scsi_cmnd *scmd;
532 	u32 response_code = 0;
533 
534 	scmd = leapraid_get_scmd_from_taskid(adapter, taskid);
535 	if (!scmd)
536 		return true;
537 
538 	scsiio_rep = leapraid_get_reply_vaddr(adapter, rep);
539 	if (!scsiio_rep) {
540 		scmd->result = DID_OK << LEAPRAID_SCSI_HOST_SHIFT;
541 		goto out_scsiio_done;
542 	}
543 
544 	sdev_priv = scmd->device->hostdata;
545 	if (!sdev_priv ||
546 	    !sdev_priv->starget_priv ||
547 	    sdev_priv->starget_priv->deleted) {
548 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
549 		goto out_scsiio_done;
550 	}
551 
552 	if (scsiio_rep->scsi_state & LEAPRAID_SCSI_STATE_RESPONSE_INFO_VALID)
553 		response_code = le32_to_cpu(scsiio_rep->resp_info) & 0xFF;
554 
555 	leapraid_process_sense_data(adapter, scsiio_rep, scmd, taskid);
556 	leapraid_scsiio_done_dispatch(adapter, scsiio_rep, sdev_priv, scmd,
557 				      taskid, response_code);
558 
559 out_scsiio_done:
560 	scsi_dma_unmap(scmd);
561 	leapraid_free_taskid(adapter, taskid);
562 	scsi_done(scmd);
563 	return false;
564 }
565 
566 static void leapraid_probe_raid(struct leapraid_adapter *adapter)
567 {
568 	struct leapraid_raid_volume *raid_volume, *next_raid_volume;
569 	unsigned long flags;
570 	LIST_HEAD(head);
571 	int rc;
572 
573 	spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
574 	list_splice_init(&adapter->dev_topo.raid_volume_list, &head);
575 	spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock, flags);
576 
577 	list_for_each_entry_safe(raid_volume, next_raid_volume, &head, list) {
578 		spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
579 		list_move_tail(&raid_volume->list,
580 			       &adapter->dev_topo.raid_volume_list);
581 		if (raid_volume->starget) {
582 			spin_unlock_irqrestore(
583 				&adapter->dev_topo.raid_volume_lock, flags);
584 			continue;
585 		}
586 
587 		leapraid_raid_volume_get(raid_volume);
588 		spin_unlock_irqrestore(
589 			&adapter->dev_topo.raid_volume_lock, flags);
590 
591 		rc = scsi_add_device(adapter->shost, RAID_CHANNEL,
592 				     raid_volume->id, 0);
593 		if (rc)
594 			leapraid_raid_volume_remove(adapter, raid_volume);
595 
596 		leapraid_raid_volume_put(raid_volume);
597 	}
598 }
599 
600 static void leapraid_sas_dev_make_active(struct leapraid_adapter *adapter,
601 					 struct leapraid_sas_dev *sas_dev)
602 {
603 	unsigned long flags;
604 
605 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
606 	if (!list_empty(&sas_dev->list)) {
607 		list_del_init(&sas_dev->list);
608 		leapraid_sdev_put(sas_dev);
609 	}
610 
611 	leapraid_sdev_get(sas_dev);
612 	list_add_tail(&sas_dev->list, &adapter->dev_topo.sas_dev_list);
613 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
614 }
615 
616 static void leapraid_probe_sas(struct leapraid_adapter *adapter)
617 {
618 	struct leapraid_sas_dev *sas_dev;
619 	bool added;
620 
621 	for (;;) {
622 		sas_dev = leapraid_get_next_sas_dev_from_init_list(adapter);
623 		if (!sas_dev)
624 			break;
625 		added = leapraid_transport_port_add(adapter,
626 						    sas_dev->hdl,
627 						    sas_dev->parent_sas_addr,
628 						    sas_dev->card_port);
629 		if (!added)
630 			goto remove_dev;
631 
632 		if (!sas_dev->starget &&
633 		    !adapter->scan_dev_desc.driver_loading) {
634 			leapraid_transport_port_remove(adapter,
635 						       sas_dev->sas_addr,
636 						       sas_dev->parent_sas_addr,
637 						       sas_dev->card_port);
638 			goto remove_dev;
639 		}
640 
641 		leapraid_sas_dev_make_active(adapter, sas_dev);
642 		leapraid_sdev_put(sas_dev);
643 		continue;
644 
645 remove_dev:
646 		leapraid_sas_dev_remove(adapter, sas_dev);
647 		leapraid_sdev_put(sas_dev);
648 	}
649 }
650 
651 static bool leapraid_get_boot_dev(struct leapraid_adapter *adapter,
652 				  struct leapraid_boot_dev *boot_dev,
653 				  void **pdev, u32 *pchnl)
654 {
655 	unsigned long flags;
656 	void *dev;
657 	u32 chnl;
658 
659 	spin_lock_irqsave(&adapter->boot_devs.lock, flags);
660 	if (!boot_dev->dev) {
661 		spin_unlock_irqrestore(&adapter->boot_devs.lock, flags);
662 		return false;
663 	}
664 
665 	dev = boot_dev->dev;
666 	chnl = boot_dev->chnl;
667 	leapraid_boot_dev_get(dev, chnl);
668 	spin_unlock_irqrestore(&adapter->boot_devs.lock, flags);
669 
670 	*pdev = dev;
671 	*pchnl = chnl;
672 	return true;
673 }
674 
675 static void leapraid_probe_boot_dev(struct leapraid_adapter *adapter)
676 {
677 	struct leapraid_raid_volume *raid_volume;
678 	struct leapraid_sas_dev *sas_dev;
679 	struct leapraid_sas_port *sport;
680 	unsigned long flags;
681 	void *dev = NULL;
682 	u32 chnl;
683 
684 	if (leapraid_get_boot_dev(adapter,
685 				  &adapter->boot_devs.requested_boot_dev,
686 				  &dev, &chnl))
687 		goto boot_dev_found;
688 
689 	if (leapraid_get_boot_dev(adapter,
690 				  &adapter->boot_devs.requested_alt_boot_dev,
691 				  &dev, &chnl))
692 		goto boot_dev_found;
693 
694 	if (leapraid_get_boot_dev(adapter,
695 				  &adapter->boot_devs.current_boot_dev,
696 				  &dev, &chnl))
697 		goto boot_dev_found;
698 
699 	return;
700 
701 boot_dev_found:
702 	switch (chnl) {
703 	case RAID_CHANNEL:
704 		raid_volume = dev;
705 
706 		if (raid_volume->starget)
707 			break;
708 
709 		/* TODO eedp */
710 
711 		if (scsi_add_device(adapter->shost, RAID_CHANNEL,
712 				    raid_volume->id, 0))
713 			leapraid_raid_volume_remove(adapter, raid_volume);
714 		break;
715 	default:
716 		sas_dev = dev;
717 
718 		if (sas_dev->starget)
719 			break;
720 
721 		spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
722 		list_move_tail(&sas_dev->list,
723 			       &adapter->dev_topo.sas_dev_list);
724 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
725 
726 		if (!sas_dev->card_port)
727 			break;
728 
729 		sport = leapraid_transport_port_add(adapter, sas_dev->hdl,
730 						    sas_dev->parent_sas_addr,
731 						    sas_dev->card_port);
732 		if (!sport)
733 			leapraid_sas_dev_remove(adapter, sas_dev);
734 		break;
735 	}
736 
737 	leapraid_boot_dev_put(dev, chnl);
738 }
739 
740 static void leapraid_probe_devices(struct leapraid_adapter *adapter)
741 {
742 	leapraid_probe_boot_dev(adapter);
743 
744 	if (adapter->adapter_attr.raid_support) {
745 		leapraid_probe_raid(adapter);
746 		leapraid_probe_sas(adapter);
747 	} else {
748 		leapraid_probe_sas(adapter);
749 	}
750 }
751 
752 void leapraid_scan_dev_done(struct leapraid_adapter *adapter)
753 {
754 	if (adapter->scan_dev_desc.wait_scan_dev_done) {
755 		adapter->scan_dev_desc.wait_scan_dev_done = 0;
756 		leapraid_probe_devices(adapter);
757 	}
758 
759 	adapter->scan_dev_desc.scan_start = 0;
760 
761 	leapraid_check_scheduled_fault_start(adapter);
762 	leapraid_fw_log_start(adapter);
763 	adapter->scan_dev_desc.driver_loading = 0;
764 	wake_up(&adapter->scan_dev_desc.wait_driver_loading);
765 }
766 
767 static const struct pci_device_id leapraid_pci_table[] = {
768 	{ PCI_DEVICE_SUB(LEAPRAID_VENDOR_ID, LEAPRAID_DEVID_HBA,
769 			 LEAPRAID_SUBVENDOR_ID,
770 			 LEAPRAID_SUBDEVID_HBA) },
771 	{ 0, }
772 };
773 
774 static inline bool leapraid_is_scmd_permitted(struct leapraid_adapter *adapter,
775 					      struct scsi_cmnd *scmd)
776 {
777 	u8 opcode;
778 
779 	if (adapter->access_ctrl.pcie_recovering ||
780 	    atomic_read(&adapter->overheat_desc.thermal_alert))
781 		return false;
782 
783 	if (adapter->access_ctrl.host_removing) {
784 		if (leapraid_pci_removed(adapter))
785 			return false;
786 
787 		opcode = scmd->cmnd[0];
788 		return opcode == SYNCHRONIZE_CACHE || opcode == START_STOP;
789 	}
790 	return true;
791 }
792 
793 static bool leapraid_should_queuecommand(struct leapraid_adapter *adapter,
794 					 struct leapraid_sdev_priv *sdev_priv,
795 					 struct scsi_cmnd *scmd,
796 					 enum scsi_qc_status *rc)
797 {
798 	struct leapraid_starget_priv *starget_priv;
799 
800 	if (!sdev_priv || !sdev_priv->starget_priv)
801 		goto no_connect;
802 
803 	if (!leapraid_is_scmd_permitted(adapter, scmd))
804 		goto no_connect;
805 
806 	starget_priv = sdev_priv->starget_priv;
807 	if (starget_priv->hdl == LEAPRAID_INVALID_DEV_HANDLE)
808 		goto no_connect;
809 
810 	if (sdev_priv->block &&
811 	    scsi_get_host_state(scmd->device->host) == SHOST_RECOVERY &&
812 	    scmd->cmnd[0] == TEST_UNIT_READY) {
813 		scsi_build_sense(scmd, 0, UNIT_ATTENTION,
814 				 LEAPRAID_SCSI_ASC_POWER_ON_RESET,
815 				 LEAPRAID_SCSI_ASCQ_POWER_ON_RESET);
816 		goto scsiio_done;
817 	}
818 
819 	if (adapter->access_ctrl.shost_recovering ||
820 	    adapter->reset_desc.adapter_link_resetting) {
821 		*rc = SCSI_MLQUEUE_HOST_BUSY;
822 		return false;
823 	}
824 
825 	if (starget_priv->deleted || sdev_priv->deleted)
826 		goto no_connect;
827 
828 	if (starget_priv->tm_busy || sdev_priv->block) {
829 		*rc = SCSI_MLQUEUE_DEVICE_BUSY;
830 		return false;
831 	}
832 
833 	return true;
834 
835 no_connect:
836 	scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
837 scsiio_done:
838 	scsi_done(scmd);
839 
840 	return false;
841 }
842 
843 static u32 build_scsiio_req_control(struct scsi_cmnd *scmd,
844 				    struct leapraid_sdev_priv *sdev_priv)
845 {
846 	u32 control;
847 
848 	switch (scmd->sc_data_direction) {
849 	case DMA_FROM_DEVICE:
850 		control = LEAPRAID_SCSIIO_CTRL_READ;
851 		break;
852 	case DMA_TO_DEVICE:
853 		control = LEAPRAID_SCSIIO_CTRL_WRITE;
854 		break;
855 	default:
856 		control = LEAPRAID_SCSIIO_CTRL_NODATATRANSFER;
857 		break;
858 	}
859 
860 	control |= LEAPRAID_SCSIIO_CTRL_SIMPLEQ;
861 
862 	if (sdev_priv->ncq_cmd_prio_enable &&
863 	    (IOPRIO_PRIO_CLASS(req_get_ioprio(scsi_cmd_to_rq(scmd))) ==
864 	     IOPRIO_CLASS_RT))
865 		control |= LEAPRAID_SCSIIO_CTRL_CMDPRI;
866 	if (scmd->cmd_len == 32)
867 		control |= LEAPRAID_SCSIIO_CTRL_CDB_32BYTE <<
868 				LEAPRAID_SCSIIO_CTRL_CDB_LEN_SHIFT;
869 
870 	return control;
871 }
872 
873 enum scsi_qc_status leapraid_queuecommand(struct Scsi_Host *shost,
874 					  struct scsi_cmnd *scmd)
875 {
876 	struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
877 	struct leapraid_sdev_priv *sdev_priv = scmd->device->hostdata;
878 	struct leapraid_starget_priv *starget_priv;
879 	struct leapraid_scsiio_req *scsiio_req;
880 	u32 control;
881 	u16 taskid;
882 	u16 hdl;
883 	enum scsi_qc_status rc = 0;
884 
885 	if (!leapraid_should_queuecommand(adapter, sdev_priv, scmd, &rc))
886 		return rc;
887 
888 	starget_priv = sdev_priv->starget_priv;
889 	hdl = starget_priv->hdl;
890 	control = build_scsiio_req_control(scmd, sdev_priv);
891 
892 	taskid = leapraid_alloc_scsiio_taskid(adapter, scmd);
893 	scsiio_req = leapraid_get_task_desc(adapter, taskid);
894 
895 	scsiio_req->func = LEAPRAID_FUNC_SCSIIO;
896 	if (sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER)
897 		scsiio_req->func = LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH;
898 	else
899 		scsiio_req->func = LEAPRAID_FUNC_SCSIIO;
900 
901 	scsiio_req->dev_hdl = cpu_to_le16(hdl);
902 	scsiio_req->data_len = cpu_to_le32(scsi_bufflen(scmd));
903 	scsiio_req->ctrl = cpu_to_le32(control);
904 	scsiio_req->io_flg = cpu_to_le16(scmd->cmd_len);
905 	scsiio_req->msg_flg = 0;
906 	scsiio_req->sense_buffer_len = SCSI_SENSE_BUFFERSIZE;
907 	scsiio_req->sense_buffer_low_add =
908 		leapraid_get_sense_buffer_dma(adapter, taskid);
909 	scsiio_req->sgl_offset0 =
910 		offsetof(struct leapraid_scsiio_req, sgl) /
911 		LEAPRAID_DWORDS_BYTE_SIZE;
912 	int_to_scsilun(sdev_priv->lun, (struct scsi_lun *)scsiio_req->lun);
913 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
914 	if (scsiio_req->data_len) {
915 		if (leapraid_build_scmd_ieee_sg(adapter, scmd, taskid)) {
916 			leapraid_free_taskid(adapter, taskid);
917 			return SCSI_MLQUEUE_HOST_BUSY;
918 		}
919 	} else {
920 		leapraid_build_ieee_nodata_sg(adapter, &scsiio_req->sgl);
921 	}
922 
923 	if (likely(scsiio_req->func == LEAPRAID_FUNC_SCSIIO))
924 		leapraid_fire_scsi_io(adapter, taskid,
925 				      le16_to_cpu(scsiio_req->dev_hdl));
926 	else
927 		leapraid_fire_task(adapter, taskid);
928 
929 	dev_dbg(&adapter->pdev->dev,
930 		"LEAPRAID_SCSIIO: Send Descriptor taskid %d, req type 0x%x\n",
931 		taskid, scsiio_req->func);
932 	return rc;
933 }
934 
935 static int leapraid_init_cmd_priv(struct Scsi_Host *shost,
936 				  struct scsi_cmnd *scmd)
937 {
938 	struct leapraid_adapter *adapter = shost_priv(shost);
939 	struct leapraid_io_req_tracker *io_tracker;
940 
941 	io_tracker = scsi_cmd_priv(scmd);
942 	leapraid_internal_init_cmd_priv(adapter, io_tracker);
943 
944 	return 0;
945 }
946 
947 static int leapraid_exit_cmd_priv(struct Scsi_Host *shost,
948 				  struct scsi_cmnd *scmd)
949 {
950 	struct leapraid_adapter *adapter = shost_priv(shost);
951 	struct leapraid_io_req_tracker *io_tracker;
952 
953 	io_tracker = scsi_cmd_priv(scmd);
954 	leapraid_internal_exit_cmd_priv(adapter, io_tracker);
955 
956 	return 0;
957 }
958 
959 static int leapraid_error_handler(struct scsi_cmnd *scmd,
960 				  const char *str, u8 type)
961 {
962 	struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
963 	struct scsi_target *starget = scmd->device->sdev_target;
964 	struct leapraid_starget_priv *starget_priv = starget->hostdata;
965 	struct leapraid_io_req_tracker *io_req_tracker = NULL;
966 	struct leapraid_sdev_priv *sdev_priv;
967 	struct leapraid_sas_dev *sas_dev = NULL;
968 	u16 hdl;
969 	int rc;
970 
971 	dev_info(&adapter->pdev->dev,
972 		 "EH enter: type=%s, scmd=0x%p, req tag=%d\n", str, scmd,
973 		 scsi_cmd_to_rq(scmd)->tag);
974 	scsi_print_command(scmd);
975 
976 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
977 		io_req_tracker = scsi_cmd_priv(scmd);
978 		dev_info(&adapter->pdev->dev,
979 			 "EH ABORT: scmd=0x%p, pend=%ums, tout=%ums, tag=%d\n",
980 			 scmd,
981 			 jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc),
982 			 (scsi_cmd_to_rq(scmd)->timeout / HZ) * 1000,
983 			 scsi_cmd_to_rq(scmd)->tag);
984 	}
985 
986 	if (leapraid_pci_removed(adapter) ||
987 	    adapter->access_ctrl.host_removing) {
988 		dev_err(&adapter->pdev->dev,
989 			"EH %s failed: %s scmd=0x%p\n", str,
990 			(adapter->access_ctrl.host_removing ?
991 			"shost removing!" : "pci_dev removed!"), scmd);
992 		if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK &&
993 		    io_req_tracker && io_req_tracker->taskid)
994 			leapraid_free_taskid(adapter, io_req_tracker->taskid);
995 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
996 #ifdef FAST_IO_FAIL
997 		rc = FAST_IO_FAIL;
998 #else
999 		rc = FAILED;
1000 #endif
1001 		goto out_eh_done;
1002 	}
1003 
1004 	sdev_priv = scmd->device->hostdata;
1005 	if (!sdev_priv || !sdev_priv->starget_priv) {
1006 		dev_warn(&adapter->pdev->dev,
1007 			 "EH %s: SAS dev or starget gone, scmd=0x%p\n",
1008 			 str, scmd);
1009 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
1010 		scsi_done(scmd);
1011 		rc = SUCCESS;
1012 		goto out_eh_done;
1013 	}
1014 
1015 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
1016 		if (!io_req_tracker) {
1017 			dev_warn(&adapter->pdev->dev,
1018 				 "EH ABORT: No I/O tracker, scmd 0x%p\n", scmd);
1019 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1020 			rc = SUCCESS;
1021 			goto out_eh_done;
1022 		}
1023 
1024 		if (sdev_priv->starget_priv->flg &
1025 			LEAPRAID_TGT_FLG_RAID_MEMBER ||
1026 		    sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
1027 			dev_err(&adapter->pdev->dev,
1028 				"EH ABORT: Skip RAID/VOLUME, scmd=0x%p\n",
1029 				scmd);
1030 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1031 			rc = FAILED;
1032 			goto out_eh_done;
1033 		}
1034 
1035 		hdl = sdev_priv->starget_priv->hdl;
1036 	} else {
1037 		hdl = 0;
1038 		if (sdev_priv->starget_priv->flg &
1039 		    LEAPRAID_TGT_FLG_RAID_MEMBER) {
1040 			sas_dev = leapraid_get_sas_dev_from_tgt(adapter,
1041 								starget_priv);
1042 			if (sas_dev)
1043 				hdl = sas_dev->volume_hdl;
1044 		} else {
1045 			hdl = sdev_priv->starget_priv->hdl;
1046 		}
1047 
1048 		if (!hdl) {
1049 			dev_err(&adapter->pdev->dev,
1050 				"EH %s failed: Target handle 0, scmd=0x%p\n",
1051 				str, scmd);
1052 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1053 			rc = FAILED;
1054 			goto out_eh_done;
1055 		}
1056 	}
1057 
1058 	dev_info(&adapter->pdev->dev,
1059 		 "EH issue TM: type=%s, scmd=0x%p, hdl=0x%x\n",
1060 		 str, scmd, hdl);
1061 
1062 	rc = leapraid_issue_locked_tm(
1063 			adapter,
1064 			hdl,
1065 			scmd->device->channel,
1066 			scmd->device->id,
1067 			(type == LEAPRAID_TM_TASKTYPE_TARGET_RESET ?
1068 				0 : scmd->device->lun),
1069 			type,
1070 			(type == LEAPRAID_TM_TASKTYPE_ABORT_TASK ?
1071 				io_req_tracker->taskid : 0),
1072 			LEAPRAID_TM_MSGFLAGS_LINK_RESET);
1073 
1074 out_eh_done:
1075 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
1076 		if (rc != SUCCESS)
1077 			dev_err(&adapter->pdev->dev,
1078 				"EH ABORT result: failed, scmd=0x%p\n",
1079 				scmd);
1080 	} else {
1081 		if (rc != SUCCESS)
1082 			dev_err(&adapter->pdev->dev,
1083 				"EH %s result: failed, scmd=0x%p\n",
1084 				str, scmd);
1085 		if (sas_dev)
1086 			leapraid_sdev_put(sas_dev);
1087 	}
1088 	return rc;
1089 }
1090 
1091 static int leapraid_eh_abort_handler(struct scsi_cmnd *scmd)
1092 {
1093 	return leapraid_error_handler(scmd, "ABORT TASK",
1094 				      LEAPRAID_TM_TASKTYPE_ABORT_TASK);
1095 }
1096 
1097 static int leapraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
1098 {
1099 	return leapraid_error_handler(scmd, "UNIT RESET",
1100 				      LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET);
1101 }
1102 
1103 static int leapraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
1104 {
1105 	return leapraid_error_handler(scmd, "TARGET RESET",
1106 				      LEAPRAID_TM_TASKTYPE_TARGET_RESET);
1107 }
1108 
1109 static int leapraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
1110 {
1111 	struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
1112 	int rc;
1113 
1114 	dev_info(&adapter->pdev->dev,
1115 		 "EH HOST RESET enter: scmd=%p, req tag=%d\n",
1116 		 scmd,
1117 		 scsi_cmd_to_rq(scmd)->tag);
1118 	scsi_print_command(scmd);
1119 
1120 	if (adapter->scan_dev_desc.driver_loading ||
1121 	    adapter->access_ctrl.host_removing) {
1122 		dev_err(&adapter->pdev->dev,
1123 			"EH HOST RESET failed: %s scmd=0x%p\n",
1124 			(adapter->access_ctrl.host_removing ?
1125 			"shost removing!" : "driver loading!"), scmd);
1126 		rc = FAILED;
1127 		goto out_host_reset_done;
1128 	}
1129 
1130 	dev_info(&adapter->pdev->dev, "%s:%d: Issuing hard reset\n",
1131 		 __func__, __LINE__);
1132 	if (leapraid_hard_reset_handler(adapter, FULL_RESET) < 0)
1133 		rc = FAILED;
1134 	else
1135 		rc = SUCCESS;
1136 
1137 out_host_reset_done:
1138 	if (rc != SUCCESS)
1139 		dev_err(&adapter->pdev->dev,
1140 			"EH HOST RESET result: failed, scmd=0x%p\n",
1141 			scmd);
1142 	return rc;
1143 }
1144 
1145 static int leapraid_sdev_init(struct scsi_device *sdev)
1146 {
1147 	struct leapraid_raid_volume *raid_volume;
1148 	struct leapraid_starget_priv *stgt_priv;
1149 	struct leapraid_sdev_priv *sdev_priv;
1150 	struct leapraid_adapter *adapter;
1151 	struct leapraid_sas_dev *sas_dev;
1152 	struct scsi_target *tgt;
1153 	struct Scsi_Host *shost;
1154 	unsigned long flags;
1155 
1156 	sdev_priv = kzalloc_obj(*sdev_priv);
1157 	if (!sdev_priv)
1158 		return -ENOMEM;
1159 
1160 	sdev_priv->lun = sdev->lun;
1161 	sdev_priv->flg = LEAPRAID_DEVICE_FLG_INIT;
1162 	tgt = scsi_target(sdev);
1163 	stgt_priv = tgt->hostdata;
1164 	stgt_priv->num_luns++;
1165 	sdev_priv->starget_priv = stgt_priv;
1166 	sdev->hostdata = sdev_priv;
1167 	if (stgt_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER)
1168 		sdev->no_uld_attach = LEAPRAID_NO_ULD_ATTACH;
1169 
1170 	shost = dev_to_shost(&tgt->dev);
1171 	adapter = shost_priv(shost);
1172 	if (tgt->channel == RAID_CHANNEL) {
1173 		raid_volume = leapraid_raid_volume_find_by_id(adapter,
1174 							      tgt->id,
1175 							      tgt->channel);
1176 		if (raid_volume) {
1177 			spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock,
1178 					  flags);
1179 			raid_volume->sdev = sdev;
1180 			spin_unlock_irqrestore(
1181 				&adapter->dev_topo.raid_volume_lock, flags);
1182 			leapraid_raid_volume_put(raid_volume);
1183 		}
1184 	}
1185 
1186 	if (!(stgt_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) {
1187 		spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1188 		sas_dev = leapraid_hold_lock_get_sas_dev_by_addr(
1189 				adapter,
1190 				stgt_priv->sas_address,
1191 				stgt_priv->card_port);
1192 		if (sas_dev && !sas_dev->starget) {
1193 			sdev_printk(KERN_INFO, sdev,
1194 				    "%s: Assign starget to sas_dev\n",
1195 				    __func__);
1196 			sas_dev->starget = tgt;
1197 		}
1198 
1199 		if (sas_dev)
1200 			leapraid_sdev_put(sas_dev);
1201 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1202 	}
1203 	return 0;
1204 }
1205 
1206 static bool leapraid_slave_cfg_volume(struct scsi_device *sdev,
1207 				      struct queue_limits *lim)
1208 {
1209 	struct Scsi_Host *shost = sdev->host;
1210 	struct leapraid_adapter *adapter = shost_priv(shost);
1211 	struct leapraid_raid_volume *raid_volume;
1212 	struct leapraid_starget_priv *starget_priv;
1213 	struct leapraid_sdev_priv *sdev_priv;
1214 	int qd;
1215 	u16 hdl;
1216 
1217 	sdev_priv = sdev->hostdata;
1218 	starget_priv = sdev_priv->starget_priv;
1219 	hdl = starget_priv->hdl;
1220 
1221 	raid_volume = leapraid_raid_volume_find_by_hdl(adapter, hdl);
1222 	if (!raid_volume) {
1223 		sdev_printk(KERN_WARNING, sdev,
1224 			    "%s: RAID volume not found, hdl=0x%x\n",
1225 			    __func__, hdl);
1226 		return 1;
1227 	}
1228 
1229 	if (leapraid_get_volume_cap(adapter, raid_volume)) {
1230 		sdev_printk(KERN_ERR, sdev,
1231 			    "%s: Failed to get volume cap, hdl=0x%x\n",
1232 			    __func__, hdl);
1233 		leapraid_raid_volume_put(raid_volume);
1234 		return 1;
1235 	}
1236 
1237 	qd = (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT) ?
1238 		adapter->adapter_attr.narrowport_max_queue_depth :
1239 		adapter->adapter_attr.sata_max_queue_depth;
1240 	if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0)
1241 		qd = adapter->adapter_attr.raid_volume_max_queue_depth;
1242 
1243 	sdev_printk(KERN_INFO, sdev,
1244 		    "RAID volume: hdl=0x%04x, wwid=0x%016llx\n",
1245 		    raid_volume->hdl, (unsigned long long)raid_volume->wwid);
1246 
1247 	if (shost->max_sectors > LEAPRAID_MAX_SECTORS)
1248 		lim->max_hw_sectors = LEAPRAID_MAX_SECTORS;
1249 
1250 	leapraid_change_queue_depth(sdev, qd);
1251 	leapraid_raid_volume_put(raid_volume);
1252 	return 0;
1253 }
1254 
1255 static bool leapraid_slave_configure_extra(struct scsi_device *sdev,
1256 					   struct leapraid_sas_dev **psas_dev,
1257 					   u16 vol_hdl, u64 volume_wwid,
1258 					   bool *is_target_ssp, int *qd)
1259 {
1260 	struct leapraid_sas_dev *sas_dev;
1261 	struct leapraid_sdev_priv *sdev_priv;
1262 	struct Scsi_Host *shost = sdev->host;
1263 	struct leapraid_adapter *adapter = shost_priv(shost);
1264 	unsigned long flags;
1265 
1266 	sdev_priv = sdev->hostdata;
1267 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1268 	*is_target_ssp = false;
1269 	sas_dev = leapraid_hold_lock_get_sas_dev_by_addr(
1270 			adapter,
1271 			sdev_priv->starget_priv->sas_address,
1272 			sdev_priv->starget_priv->card_port);
1273 	if (!sas_dev) {
1274 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1275 		sdev_printk(KERN_WARNING, sdev,
1276 			    "%s: SAS dev not found, sas=0x%llx\n",
1277 			    __func__, sdev_priv->starget_priv->sas_address);
1278 		return 1;
1279 	}
1280 
1281 	*psas_dev = sas_dev;
1282 	sas_dev->volume_hdl = vol_hdl;
1283 	sas_dev->volume_wwid = volume_wwid;
1284 	if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) {
1285 		*qd = (sas_dev->port_connection > 1) ?
1286 			adapter->adapter_attr.wideport_max_queue_depth :
1287 			adapter->adapter_attr.narrowport_max_queue_depth;
1288 		*is_target_ssp = true;
1289 		if (sas_dev->dev_info & LEAPRAID_DEVTYP_SEP)
1290 			sdev_priv->sep = 1;
1291 	} else {
1292 		*qd = adapter->adapter_attr.sata_max_queue_depth;
1293 	}
1294 
1295 	sdev_printk(KERN_INFO, sdev,
1296 		    "device name=0x%016llx, SAS addr=0x%016llx\n",
1297 		    (unsigned long long)sas_dev->dev_name,
1298 		    (unsigned long long)sas_dev->sas_addr);
1299 	leapraid_sdev_put(sas_dev);
1300 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1301 	return 0;
1302 }
1303 
1304 static int leapraid_sdev_configure(struct scsi_device *sdev,
1305 				   struct queue_limits *lim)
1306 {
1307 	struct leapraid_sas_dev *sas_dev;
1308 	struct leapraid_sdev_priv *sdev_priv;
1309 	struct Scsi_Host *shost = sdev->host;
1310 	struct leapraid_starget_priv *starget_priv;
1311 	struct leapraid_adapter *adapter;
1312 	u16 hdl, vol_hdl = 0;
1313 	bool is_target_ssp = false;
1314 	u64 volume_wwid = 0;
1315 	int qd = 1;
1316 
1317 	adapter = shost_priv(shost);
1318 	sdev_priv = sdev->hostdata;
1319 	sdev_priv->flg &= ~LEAPRAID_DEVICE_FLG_INIT;
1320 	starget_priv = sdev_priv->starget_priv;
1321 	hdl = starget_priv->hdl;
1322 	if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)
1323 		return leapraid_slave_cfg_volume(sdev, lim);
1324 
1325 	if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER) {
1326 		if (leapraid_cfg_get_volume_hdl(adapter, hdl, &vol_hdl)) {
1327 			sdev_printk(KERN_WARNING, sdev,
1328 				    "%s: Get volume hdl failed, hdl=0x%x\n",
1329 				    __func__, hdl);
1330 			return 1;
1331 		}
1332 
1333 		if (vol_hdl && leapraid_cfg_get_volume_wwid(adapter, vol_hdl,
1334 							    &volume_wwid)) {
1335 			sdev_printk(KERN_WARNING, sdev,
1336 				    "%s: Get wwid failed, volume_hdl=0x%x\n",
1337 				    __func__, vol_hdl);
1338 			return 1;
1339 		}
1340 	}
1341 
1342 	if (leapraid_slave_configure_extra(sdev, &sas_dev, vol_hdl,
1343 					   volume_wwid, &is_target_ssp, &qd)) {
1344 		sdev_printk(KERN_WARNING, sdev,
1345 			    "%s: slave_configure_extra failed\n", __func__);
1346 		return 1;
1347 	}
1348 
1349 	leapraid_change_queue_depth(sdev, qd);
1350 	if (is_target_ssp)
1351 		sas_read_port_mode_page(sdev);
1352 
1353 	return 0;
1354 }
1355 
1356 static void leapraid_sdev_destroy(struct scsi_device *sdev)
1357 {
1358 	struct leapraid_adapter *adapter;
1359 	struct Scsi_Host *shost;
1360 	struct leapraid_sas_dev *sas_dev;
1361 	struct leapraid_starget_priv *starget_priv;
1362 	struct scsi_target *stgt;
1363 	unsigned long flags;
1364 
1365 	if (!sdev->hostdata)
1366 		return;
1367 
1368 	stgt = scsi_target(sdev);
1369 	starget_priv = stgt->hostdata;
1370 	starget_priv->num_luns--;
1371 	shost = dev_to_shost(&stgt->dev);
1372 	adapter = shost_priv(shost);
1373 	if (!(starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) {
1374 		spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1375 		sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter,
1376 								  starget_priv);
1377 		if (sas_dev && !starget_priv->num_luns)
1378 			sas_dev->starget = NULL;
1379 		if (sas_dev)
1380 			leapraid_sdev_put(sas_dev);
1381 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1382 	}
1383 
1384 	kfree(sdev->hostdata);
1385 	sdev->hostdata = NULL;
1386 }
1387 
1388 static int leapraid_target_alloc_raid(struct scsi_target *tgt)
1389 {
1390 	struct leapraid_starget_priv *starget_priv;
1391 	struct leapraid_raid_volume *raid_volume;
1392 	struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
1393 	struct leapraid_adapter *adapter = shost_priv(shost);
1394 	unsigned long flags;
1395 
1396 	starget_priv = tgt->hostdata;
1397 	raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id,
1398 						      tgt->channel);
1399 	if (raid_volume) {
1400 		spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
1401 		starget_priv->hdl = raid_volume->hdl;
1402 		starget_priv->sas_address = raid_volume->wwid;
1403 		starget_priv->flg |= LEAPRAID_TGT_FLG_VOLUME;
1404 		raid_volume->starget = tgt;
1405 		spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock,
1406 				       flags);
1407 		leapraid_raid_volume_put(raid_volume);
1408 	}
1409 	return 0;
1410 }
1411 
1412 static int leapraid_target_alloc_sas(struct scsi_target *tgt)
1413 {
1414 	struct sas_rphy *rphy;
1415 	struct Scsi_Host *shost;
1416 	struct leapraid_sas_dev *sas_dev;
1417 	struct leapraid_adapter *adapter;
1418 	struct leapraid_starget_priv *starget_priv;
1419 	unsigned long flags;
1420 
1421 	shost = dev_to_shost(&tgt->dev);
1422 	adapter = shost_priv(shost);
1423 	starget_priv = tgt->hostdata;
1424 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1425 	rphy = dev_to_rphy(tgt->dev.parent);
1426 	sas_dev = leapraid_hold_lock_get_sas_dev_by_addr_and_rphy(
1427 			adapter,
1428 			rphy->identify.sas_address,
1429 			rphy);
1430 	if (sas_dev) {
1431 		starget_priv->sas_dev = sas_dev;
1432 		starget_priv->card_port = sas_dev->card_port;
1433 		starget_priv->sas_address = sas_dev->sas_addr;
1434 		starget_priv->hdl = sas_dev->hdl;
1435 		sas_dev->channel = tgt->channel;
1436 		sas_dev->id = tgt->id;
1437 		sas_dev->starget = tgt;
1438 		if (sas_dev->hdl &&
1439 		    sas_dev->hdl <=
1440 		    adapter->adapter_attr.features.max_dev_handle &&
1441 		    test_bit(sas_dev->hdl, adapter->dev_topo.pd_hdls))
1442 			starget_priv->flg |= LEAPRAID_TGT_FLG_RAID_MEMBER;
1443 	}
1444 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1445 
1446 	return 0;
1447 }
1448 
1449 static int leapraid_target_alloc(struct scsi_target *tgt)
1450 {
1451 	struct leapraid_starget_priv *starget_priv;
1452 
1453 	starget_priv = kzalloc_obj(*starget_priv);
1454 	if (!starget_priv)
1455 		return -ENOMEM;
1456 
1457 	tgt->hostdata = starget_priv;
1458 	starget_priv->starget = tgt;
1459 	starget_priv->hdl = LEAPRAID_INVALID_DEV_HANDLE;
1460 	if (tgt->channel == RAID_CHANNEL)
1461 		return leapraid_target_alloc_raid(tgt);
1462 
1463 	return leapraid_target_alloc_sas(tgt);
1464 }
1465 
1466 static void leapraid_target_destroy_raid(struct scsi_target *tgt)
1467 {
1468 	struct leapraid_raid_volume *raid_volume;
1469 	struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
1470 	struct leapraid_adapter *adapter = shost_priv(shost);
1471 	unsigned long flags;
1472 
1473 	raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id,
1474 						      tgt->channel);
1475 	if (raid_volume) {
1476 		spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
1477 		raid_volume->starget = NULL;
1478 		raid_volume->sdev = NULL;
1479 		spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock,
1480 				       flags);
1481 		leapraid_raid_volume_put(raid_volume);
1482 	}
1483 }
1484 
1485 static void leapraid_target_destroy_sas(struct scsi_target *tgt)
1486 {
1487 	struct leapraid_adapter *adapter;
1488 	struct leapraid_sas_dev *sas_dev;
1489 	struct leapraid_starget_priv *starget_priv;
1490 	struct Scsi_Host *shost;
1491 	unsigned long flags;
1492 
1493 	shost = dev_to_shost(&tgt->dev);
1494 	adapter = shost_priv(shost);
1495 	starget_priv = tgt->hostdata;
1496 
1497 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1498 	sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter,
1499 							  starget_priv);
1500 	if (sas_dev &&
1501 	    sas_dev->starget == tgt &&
1502 	    sas_dev->id == tgt->id &&
1503 	    sas_dev->channel == tgt->channel)
1504 		sas_dev->starget = NULL;
1505 
1506 	if (sas_dev) {
1507 		starget_priv->sas_dev = NULL;
1508 		leapraid_sdev_put(sas_dev);
1509 		leapraid_sdev_put(sas_dev);
1510 	}
1511 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1512 }
1513 
1514 static void leapraid_target_destroy(struct scsi_target *tgt)
1515 {
1516 	struct leapraid_starget_priv *starget_priv;
1517 
1518 	starget_priv = tgt->hostdata;
1519 	if (!starget_priv)
1520 		return;
1521 
1522 	if (tgt->channel == RAID_CHANNEL) {
1523 		leapraid_target_destroy_raid(tgt);
1524 		goto out_free;
1525 	}
1526 
1527 	leapraid_target_destroy_sas(tgt);
1528 
1529 out_free:
1530 	kfree(starget_priv);
1531 	tgt->hostdata = NULL;
1532 }
1533 
1534 static bool leapraid_scan_check_status(struct leapraid_adapter *adapter,
1535 				       bool *need_hard_reset)
1536 {
1537 	u32 adapter_state;
1538 
1539 	if (adapter->scan_dev_desc.scan_start) {
1540 		adapter_state = leapraid_get_adapter_state(adapter);
1541 		if (adapter_state == LEAPRAID_DB_FAULT) {
1542 			*need_hard_reset = true;
1543 			return true;
1544 		}
1545 		return false;
1546 	}
1547 
1548 	if (adapter->driver_cmds.scan_dev_cmd.status & LEAPRAID_CMD_RESET) {
1549 		dev_err(&adapter->pdev->dev,
1550 			"Device scan: Aborted due to reset\n");
1551 		adapter->driver_cmds.scan_dev_cmd.status =
1552 			LEAPRAID_CMD_NOT_USED;
1553 		adapter->scan_dev_desc.driver_loading = 0;
1554 		wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1555 		return true;
1556 	}
1557 
1558 	if (adapter->scan_dev_desc.scan_start_failed) {
1559 		dev_err(&adapter->pdev->dev,
1560 			"Device scan: Failed with adapter_status=0x%08x\n",
1561 			adapter->scan_dev_desc.scan_start_failed);
1562 		adapter->scan_dev_desc.driver_loading = 0;
1563 		wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1564 		adapter->scan_dev_desc.wait_scan_dev_done = 0;
1565 		adapter->access_ctrl.host_removing = 1;
1566 		return true;
1567 	}
1568 
1569 	adapter->driver_cmds.scan_dev_cmd.status = LEAPRAID_CMD_NOT_USED;
1570 	leapraid_scan_dev_done(adapter);
1571 	return true;
1572 }
1573 
1574 static int leapraid_scan_finished(struct Scsi_Host *shost, unsigned long time)
1575 {
1576 	struct leapraid_adapter *adapter = shost_priv(shost);
1577 	bool need_hard_reset = false;
1578 
1579 	if (time >= (LEAPRAID_SCAN_DEV_CMD_TIMEOUT * HZ)) {
1580 		adapter->driver_cmds.scan_dev_cmd.status =
1581 			LEAPRAID_CMD_NOT_USED;
1582 		dev_err(&adapter->pdev->dev,
1583 			"Device scan: Failed with timeout 300s\n");
1584 		adapter->scan_dev_desc.driver_loading = 0;
1585 		wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1586 		return 1;
1587 	}
1588 
1589 	if (!leapraid_scan_check_status(adapter, &need_hard_reset))
1590 		return 0;
1591 
1592 	if (need_hard_reset) {
1593 		adapter->driver_cmds.scan_dev_cmd.status =
1594 			LEAPRAID_CMD_NOT_USED;
1595 		dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
1596 			 __func__, __LINE__);
1597 		if (leapraid_hard_reset_handler(adapter, PART_RESET)) {
1598 			adapter->scan_dev_desc.driver_loading = 0;
1599 			wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1600 		}
1601 	}
1602 
1603 	return 1;
1604 }
1605 
1606 static void leapraid_scan_start(struct Scsi_Host *shost)
1607 {
1608 	struct leapraid_adapter *adapter = shost_priv(shost);
1609 
1610 	adapter->scan_dev_desc.scan_start = 1;
1611 	leapraid_scan_dev(adapter, true);
1612 }
1613 
1614 static u32 leapraid_get_raid_qd(struct leapraid_adapter *adapter,
1615 				const struct leapraid_raid_volume *raid_volume,
1616 				bool default_qd)
1617 {
1618 	const struct leapraid_adapter_attr *attr = &adapter->adapter_attr;
1619 
1620 	if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0)
1621 		return default_qd ? LEAPRAID_RAID_QUEUE_DEPTH :
1622 			attr->raid_volume_max_queue_depth;
1623 
1624 	if (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT)
1625 		return default_qd ? LEAPRAID_SAS_QUEUE_DEPTH :
1626 			attr->narrowport_max_queue_depth;
1627 
1628 	return default_qd ? LEAPRAID_SATA_QUEUE_DEPTH :
1629 		attr->sata_max_queue_depth;
1630 }
1631 
1632 static int leapraid_calc_max_queue_depth(struct scsi_device *sdev, int qdepth)
1633 {
1634 	struct Scsi_Host *shost;
1635 	struct leapraid_adapter *adapter;
1636 	struct leapraid_starget_priv *starget_priv;
1637 	struct leapraid_sdev_priv *sdev_priv;
1638 	struct leapraid_raid_volume *raid_volume;
1639 	struct leapraid_sas_dev *sas_dev;
1640 	int max_depth;
1641 	u32 default_qdepth = 0;
1642 	u32 fw_qdepth = 0;
1643 
1644 	shost = sdev->host;
1645 	adapter = shost_priv(shost);
1646 	max_depth = shost->can_queue;
1647 
1648 	sdev_priv = sdev->hostdata;
1649 	if (!sdev_priv)
1650 		goto out_tag_check;
1651 
1652 	starget_priv = sdev_priv->starget_priv;
1653 	if (!starget_priv)
1654 		goto out_tag_check;
1655 
1656 	if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
1657 		raid_volume = leapraid_raid_volume_find_by_hdl(
1658 					adapter, starget_priv->hdl);
1659 		if (raid_volume) {
1660 			default_qdepth = leapraid_get_raid_qd(
1661 					adapter, raid_volume, true);
1662 			fw_qdepth = leapraid_get_raid_qd(
1663 					adapter, raid_volume, false);
1664 			leapraid_raid_volume_put(raid_volume);
1665 		}
1666 		goto out_limit_check;
1667 	}
1668 
1669 	sas_dev = leapraid_get_sas_dev_from_tgt(adapter, starget_priv);
1670 	if (sas_dev) {
1671 		if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) {
1672 			default_qdepth = LEAPRAID_SAS_QUEUE_DEPTH;
1673 			fw_qdepth = (sas_dev->port_connection > 1) ?
1674 			      adapter->adapter_attr.wideport_max_queue_depth :
1675 			      adapter->adapter_attr.narrowport_max_queue_depth;
1676 		}
1677 		if (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV) {
1678 			default_qdepth = LEAPRAID_SATA_QUEUE_DEPTH;
1679 			fw_qdepth = adapter->adapter_attr.sata_max_queue_depth;
1680 		}
1681 		leapraid_sdev_put(sas_dev);
1682 	}
1683 
1684 out_limit_check:
1685 	if (fw_qdepth > shost->can_queue && default_qdepth)
1686 		fw_qdepth = default_qdepth;
1687 
1688 	if (fw_qdepth)
1689 		max_depth = min_t(int, max_depth, fw_qdepth);
1690 
1691 out_tag_check:
1692 	if (!sdev->tagged_supported)
1693 		max_depth = 1;
1694 
1695 	if (qdepth > max_depth)
1696 		qdepth = max_depth;
1697 
1698 	return qdepth;
1699 }
1700 
1701 int leapraid_change_queue_depth(struct scsi_device *sdev, int qdepth)
1702 {
1703 	qdepth = leapraid_calc_max_queue_depth(sdev, qdepth);
1704 	scsi_change_queue_depth(sdev, qdepth);
1705 	return sdev->queue_depth;
1706 }
1707 
1708 static void leapraid_map_queues(struct Scsi_Host *shost)
1709 {
1710 	struct leapraid_adapter *adapter;
1711 	struct blk_mq_queue_map *queue_map;
1712 	int msix_queue_count;
1713 	int poll_queue_count;
1714 	int queue_offset;
1715 	int map_index;
1716 
1717 	adapter = (struct leapraid_adapter *)shost->hostdata;
1718 	if (shost->nr_hw_queues == 1)
1719 		return;
1720 
1721 	msix_queue_count = adapter->notification_desc.iopoll_qdex;
1722 	poll_queue_count = adapter->adapter_attr.rq_cnt - msix_queue_count;
1723 
1724 	queue_offset = 0;
1725 	for (map_index = 0; map_index < shost->nr_maps; map_index++) {
1726 		queue_map = &shost->tag_set.map[map_index];
1727 		queue_map->nr_queues = 0;
1728 
1729 		switch (map_index) {
1730 		case HCTX_TYPE_DEFAULT:
1731 			queue_map->nr_queues = msix_queue_count;
1732 			queue_map->queue_offset = queue_offset;
1733 			WARN_ON_ONCE(!queue_map->nr_queues);
1734 			blk_mq_map_hw_queues(queue_map,
1735 					     &adapter->pdev->dev, 0);
1736 			break;
1737 		case HCTX_TYPE_POLL:
1738 			queue_map->nr_queues = poll_queue_count;
1739 			queue_map->queue_offset = queue_offset;
1740 			blk_mq_map_queues(queue_map);
1741 			break;
1742 		default:
1743 			queue_map->queue_offset = queue_offset;
1744 			blk_mq_map_hw_queues(queue_map,
1745 					     &adapter->pdev->dev, 0);
1746 			break;
1747 		}
1748 		queue_offset += queue_map->nr_queues;
1749 	}
1750 }
1751 
1752 int leapraid_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
1753 {
1754 	struct leapraid_adapter *adapter =
1755 		(struct leapraid_adapter *)shost->hostdata;
1756 	struct leapraid_blk_mq_poll_rq *blk_mq_poll_rq;
1757 	int num_entries;
1758 	int qid = queue_num - adapter->notification_desc.iopoll_qdex;
1759 
1760 	blk_mq_poll_rq = &adapter->notification_desc.blk_mq_poll_rqs[qid];
1761 	if (atomic_read(&blk_mq_poll_rq->pause) ||
1762 	    !atomic_add_unless(&blk_mq_poll_rq->busy, 1, 1))
1763 		return 0;
1764 
1765 	num_entries = leapraid_rep_queue_handler(&blk_mq_poll_rq->rq);
1766 	atomic_dec(&blk_mq_poll_rq->busy);
1767 	return num_entries;
1768 }
1769 
1770 static int leapraid_bios_param(struct scsi_device *sdev,
1771 			       struct gendisk *disk, sector_t capacity,
1772 			       int geom[])
1773 {
1774 	int heads;
1775 	int sectors;
1776 	sector_t cylinders;
1777 
1778 	if (scsi_partsize(disk, capacity, geom))
1779 		return 0;
1780 
1781 	if ((ulong)capacity >= LEAPRAID_LARGE_DISK_THRESHOLD) {
1782 		heads = LEAPRAID_LARGE_DISK_HEADS;
1783 		sectors = LEAPRAID_LARGE_DISK_SECTORS;
1784 	} else {
1785 		heads = LEAPRAID_SMALL_DISK_HEADS;
1786 		sectors = LEAPRAID_SMALL_DISK_SECTORS;
1787 	}
1788 
1789 	cylinders = capacity;
1790 	sector_div(cylinders, heads * sectors);
1791 
1792 	geom[0] = heads;
1793 	geom[1] = sectors;
1794 	geom[2] = cylinders;
1795 	return 0;
1796 }
1797 
1798 static ssize_t fw_queue_depth_show(struct device *cdev,
1799 				   struct device_attribute *attr,
1800 				   char *buf)
1801 {
1802 	struct Scsi_Host *shost = class_to_shost(cdev);
1803 	struct leapraid_adapter *adapter = shost_priv(shost);
1804 
1805 	return sysfs_emit(buf, "%02d\n",
1806 			  adapter->adapter_attr.features.req_slot);
1807 }
1808 
1809 static ssize_t host_sas_address_show(struct device *cdev,
1810 				     struct device_attribute *attr, char *buf)
1811 {
1812 	struct Scsi_Host *shost = class_to_shost(cdev);
1813 	struct leapraid_adapter *adapter = shost_priv(shost);
1814 
1815 	return sysfs_emit(buf, "0x%016llx\n",
1816 		(unsigned long long)adapter->dev_topo.card.sas_address);
1817 }
1818 
1819 static ssize_t board_name_show(struct device *cdev,
1820 			       struct device_attribute *attr, char *buf)
1821 {
1822 	struct Scsi_Host *shost = class_to_shost(cdev);
1823 	struct leapraid_adapter *adapter = shost_priv(shost);
1824 
1825 	return sysfs_emit(buf, "%s\n", adapter->adapter_attr.board_name);
1826 }
1827 
1828 static DEVICE_ATTR_RO(fw_queue_depth);
1829 static DEVICE_ATTR_RO(host_sas_address);
1830 static DEVICE_ATTR_RO(board_name);
1831 
1832 static struct attribute *leapraid_shost_attrs[] = {
1833 	&dev_attr_fw_queue_depth.attr,
1834 	&dev_attr_host_sas_address.attr,
1835 	&dev_attr_board_name.attr,
1836 	NULL,
1837 };
1838 
1839 ATTRIBUTE_GROUPS(leapraid_shost);
1840 
1841 static ssize_t sas_device_handle_show(struct device *dev,
1842 				      struct device_attribute *attr, char *buf)
1843 {
1844 	struct scsi_device *sdev = to_scsi_device(dev);
1845 	struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1846 
1847 	if (!sas_device_priv_data || !sas_device_priv_data->starget_priv) {
1848 		dev_err(&sdev->sdev_gendev,
1849 			"%s: Invalid sdev_priv or starget_priv\n", __func__);
1850 		return -EINVAL;
1851 	}
1852 
1853 	return sysfs_emit(buf, "0x%04x\n",
1854 			  sas_device_priv_data->starget_priv->hdl);
1855 }
1856 
1857 static ssize_t ncq_cmd_prio_enable_show(struct device *dev,
1858 					struct device_attribute *attr,
1859 					char *buf)
1860 {
1861 	struct scsi_device *sdev = to_scsi_device(dev);
1862 	struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1863 
1864 	if (!sas_device_priv_data) {
1865 		dev_err(&sdev->sdev_gendev,
1866 			"%s: Invalid sdev_priv\n", __func__);
1867 		return -EINVAL;
1868 	}
1869 
1870 	return sysfs_emit(buf, "%d\n",
1871 			  sas_device_priv_data->ncq_cmd_prio_enable);
1872 }
1873 
1874 static ssize_t ncq_cmd_prio_enable_store(struct device *dev,
1875 					 struct device_attribute *attr,
1876 					 const char *buf, size_t count)
1877 {
1878 	struct scsi_device *sdev = to_scsi_device(dev);
1879 	struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1880 	struct scsi_vpd *vpd_pg89;
1881 	int ncq_cmd_prio_enable;
1882 	bool ncq_supported;
1883 
1884 	if (!sas_device_priv_data) {
1885 		dev_err(&sdev->sdev_gendev,
1886 			"%s: Invalid sdev_priv\n", __func__);
1887 		return -EINVAL;
1888 	}
1889 
1890 	if (kstrtoint(buf, 0, &ncq_cmd_prio_enable))
1891 		return -EINVAL;
1892 
1893 	if (ncq_cmd_prio_enable != 0 && ncq_cmd_prio_enable != 1) {
1894 		dev_err(&sdev->sdev_gendev,
1895 			"%s: Invalid NCQ cmd prio %d (0/1 only)\n",
1896 			__func__, ncq_cmd_prio_enable);
1897 		return -EINVAL;
1898 	}
1899 
1900 	rcu_read_lock();
1901 	vpd_pg89 = rcu_dereference(sdev->vpd_pg89);
1902 	if (!vpd_pg89 || vpd_pg89->len < LEAPRAID_VPD_PG89_MIN_LEN) {
1903 		rcu_read_unlock();
1904 		return -EINVAL;
1905 	}
1906 
1907 	ncq_supported = (vpd_pg89->data[LEAPRAID_VPD_PG89_NCQ_BYTE_IDX] >>
1908 			 LEAPRAID_VPD_PG89_NCQ_BIT_SHIFT) &
1909 			LEAPRAID_VPD_PG89_NCQ_BIT_MASK;
1910 	rcu_read_unlock();
1911 	if (ncq_supported)
1912 		sas_device_priv_data->ncq_cmd_prio_enable =
1913 			ncq_cmd_prio_enable;
1914 	return count;
1915 }
1916 
1917 static DEVICE_ATTR_RO(sas_device_handle);
1918 
1919 static DEVICE_ATTR_RW(ncq_cmd_prio_enable);
1920 
1921 static struct attribute *leapraid_sdev_attrs[] = {
1922 	&dev_attr_sas_device_handle.attr,
1923 	&dev_attr_ncq_cmd_prio_enable.attr,
1924 	NULL,
1925 };
1926 
1927 ATTRIBUTE_GROUPS(leapraid_sdev);
1928 
1929 static struct scsi_host_template leapraid_driver_template = {
1930 	.module = THIS_MODULE,
1931 	.name = "LEAPIO RAID Host",
1932 	.proc_name = LEAPRAID_DRIVER_NAME,
1933 	.queuecommand = leapraid_queuecommand,
1934 	.cmd_size = sizeof(struct leapraid_io_req_tracker),
1935 	.init_cmd_priv = leapraid_init_cmd_priv,
1936 	.exit_cmd_priv = leapraid_exit_cmd_priv,
1937 	.eh_abort_handler = leapraid_eh_abort_handler,
1938 	.eh_device_reset_handler = leapraid_eh_device_reset_handler,
1939 	.eh_target_reset_handler = leapraid_eh_target_reset_handler,
1940 	.eh_host_reset_handler = leapraid_eh_host_reset_handler,
1941 	.sdev_init = leapraid_sdev_init,
1942 	.sdev_destroy = leapraid_sdev_destroy,
1943 	.sdev_configure = leapraid_sdev_configure,
1944 	.target_alloc = leapraid_target_alloc,
1945 	.target_destroy = leapraid_target_destroy,
1946 	.scan_finished = leapraid_scan_finished,
1947 	.scan_start = leapraid_scan_start,
1948 	.change_queue_depth = leapraid_change_queue_depth,
1949 	.map_queues = leapraid_map_queues,
1950 	.mq_poll = leapraid_blk_mq_poll,
1951 	.bios_param = leapraid_bios_param,
1952 	.can_queue = LEAPRAID_CAN_QUEUE_MIN,
1953 	.this_id = LEAPRAID_THIS_ID_NONE,
1954 	.sg_tablesize = LEAPRAID_SG_DEPTH,
1955 	.max_sectors = LEAPRAID_MAX_SECTORS,
1956 	.max_segment_size = LEAPRAID_MAX_SEGMENT_SIZE,
1957 	.cmd_per_lun = LEAPRAID_CMD_PER_LUN,
1958 	.shost_groups = leapraid_shost_groups,
1959 	.sdev_groups = leapraid_sdev_groups,
1960 	.track_queue_depth = 1,
1961 };
1962 
1963 static void leapraid_lock_init(struct leapraid_adapter *adapter)
1964 {
1965 	mutex_init(&adapter->reset_desc.adapter_reset_mutex);
1966 	mutex_init(&adapter->reset_desc.host_diag_mutex);
1967 	mutex_init(&adapter->access_ctrl.pci_access_lock);
1968 
1969 	spin_lock_init(&adapter->reset_desc.adapter_reset_lock);
1970 	spin_lock_init(&adapter->dynamic_task_desc.task_lock);
1971 	spin_lock_init(&adapter->dev_topo.sas_dev_lock);
1972 	spin_lock_init(&adapter->dev_topo.topo_node_lock);
1973 	spin_lock_init(&adapter->fw_evt_s.fw_evt_lock);
1974 	spin_lock_init(&adapter->dev_topo.raid_volume_lock);
1975 	spin_lock_init(&adapter->dev_topo.enc_lock);
1976 	spin_lock_init(&adapter->boot_devs.lock);
1977 }
1978 
1979 static void leapraid_list_init(struct leapraid_adapter *adapter)
1980 {
1981 	INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_list);
1982 	INIT_LIST_HEAD(&adapter->dev_topo.card_port_list);
1983 	INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_init_list);
1984 	INIT_LIST_HEAD(&adapter->dev_topo.exp_list);
1985 	INIT_LIST_HEAD(&adapter->dev_topo.enc_list);
1986 	INIT_LIST_HEAD(&adapter->fw_evt_s.fw_evt_list);
1987 	INIT_LIST_HEAD(&adapter->dev_topo.raid_volume_list);
1988 	INIT_LIST_HEAD(&adapter->dev_topo.card.sas_port_list);
1989 }
1990 
1991 static int leapraid_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1992 {
1993 	struct leapraid_adapter *adapter;
1994 	struct Scsi_Host *shost;
1995 	int iopoll_q_count;
1996 	int rc;
1997 
1998 	shost = scsi_host_alloc(&leapraid_driver_template,
1999 				sizeof(struct leapraid_adapter));
2000 	if (!shost) {
2001 		dev_err(&pdev->dev,
2002 			"%s: SCSI host alloc failed\n", __func__);
2003 		return -ENODEV;
2004 	}
2005 
2006 	adapter = shost_priv(shost);
2007 	memset(adapter, 0, sizeof(struct leapraid_adapter));
2008 
2009 	init_waitqueue_head(&adapter->access_ctrl.recovery_waitq);
2010 	adapter->adapter_attr.id = atomic_inc_return(&leapraid_ids) - 1;
2011 
2012 	adapter->adapter_attr.enable_mp = enable_mp;
2013 
2014 	adapter = shost_priv(shost);
2015 	INIT_LIST_HEAD(&adapter->list);
2016 
2017 	adapter->shost = shost;
2018 	adapter->pdev = pdev;
2019 	adapter->fw_log_desc.open_pcie_trace = open_pcie_trace;
2020 	atomic_set(&adapter->fw_log_desc.mmap_refcnt, 0);
2021 	init_waitqueue_head(&adapter->fw_log_desc.mmap_waitq);
2022 	leapraid_lock_init(adapter);
2023 	leapraid_list_init(adapter);
2024 	snprintf(adapter->adapter_attr.name, LEAPRAID_NAME_LENGTH, "%s%d",
2025 		 LEAPRAID_DRIVER_NAME, adapter->adapter_attr.id);
2026 
2027 	shost->max_cmd_len = LEAPRAID_MAX_CDB_LEN;
2028 	shost->max_lun = LEAPRAID_MAX_LUNS;
2029 	shost->transportt = leapraid_transport_template;
2030 	shost->unique_id = adapter->adapter_attr.id;
2031 
2032 	snprintf(adapter->fw_evt_s.fw_evt_name,
2033 		 sizeof(adapter->fw_evt_s.fw_evt_name),
2034 		 "fw_event_%s%d", LEAPRAID_DRIVER_NAME,
2035 		 adapter->adapter_attr.id);
2036 	adapter->fw_evt_s.fw_evt_thread =
2037 		alloc_ordered_workqueue(adapter->fw_evt_s.fw_evt_name, 0);
2038 	if (!adapter->fw_evt_s.fw_evt_thread) {
2039 		dev_err(&adapter->pdev->dev,
2040 			"%s: Failed to create fw event workqueue\n", __func__);
2041 		rc = -ENODEV;
2042 		goto evt_wq_fail;
2043 	}
2044 
2045 	shost->host_tagset = 1;
2046 	init_waitqueue_head(&adapter->scan_dev_desc.wait_driver_loading);
2047 	adapter->scan_dev_desc.driver_loading = 1;
2048 	if (leapraid_ctrl_init(adapter)) {
2049 		dev_err(&adapter->pdev->dev,
2050 			"%s: Adapter init failed\n", __func__);
2051 		rc = -ENODEV;
2052 		goto ctrl_init_fail;
2053 	}
2054 
2055 	shost->nr_hw_queues = 1;
2056 	if (shost->host_tagset) {
2057 		shost->nr_hw_queues = adapter->adapter_attr.rq_cnt;
2058 		iopoll_q_count = adapter->adapter_attr.rq_cnt -
2059 				 adapter->notification_desc.iopoll_qdex;
2060 		shost->nr_maps = iopoll_q_count ? 3 : 1;
2061 		dev_info(&adapter->pdev->dev,
2062 			 "Max scsi I/O cmds %d shared with nr_hw_queues=%d\n",
2063 			 shost->can_queue, shost->nr_hw_queues);
2064 	}
2065 
2066 	rc = scsi_add_host(shost, &pdev->dev);
2067 	if (rc) {
2068 		dev_err(&pdev->dev,
2069 			"%s: SCSI host add failed\n", __func__);
2070 		goto scsi_add_shost_fail;
2071 	}
2072 
2073 	spin_lock(&leapraid_adapter_lock);
2074 	list_add_tail(&adapter->list, &leapraid_adapter_list);
2075 	spin_unlock(&leapraid_adapter_lock);
2076 
2077 	scsi_scan_host(shost);
2078 	return 0;
2079 
2080 scsi_add_shost_fail:
2081 	leapraid_remove_ctrl(adapter);
2082 ctrl_init_fail:
2083 	leapraid_overheat_cleanup(adapter);
2084 	destroy_workqueue(adapter->fw_evt_s.fw_evt_thread);
2085 evt_wq_fail:
2086 	scsi_host_put(shost);
2087 	return rc;
2088 }
2089 
2090 void leapraid_cleanup_lists(struct leapraid_adapter *adapter)
2091 {
2092 	struct leapraid_raid_volume *raid_volume, *next_raid_volume;
2093 	struct leapraid_starget_priv *starget_priv_data;
2094 	struct leapraid_sas_port *leapraid_port, *next_port;
2095 	struct leapraid_card_port *port, *port_next;
2096 	struct leapraid_vphy *vphy, *vphy_next;
2097 
2098 	list_for_each_entry_safe(raid_volume, next_raid_volume,
2099 				 &adapter->dev_topo.raid_volume_list, list) {
2100 		if (raid_volume->starget) {
2101 			starget_priv_data = raid_volume->starget->hostdata;
2102 			if (starget_priv_data)
2103 				starget_priv_data->deleted = 1;
2104 			scsi_remove_target(&raid_volume->starget->dev);
2105 		}
2106 		dev_info(&adapter->pdev->dev,
2107 			 "removing hdl=0x%04x, wwid=0x%016llx\n",
2108 			 raid_volume->hdl,
2109 			 (unsigned long long)raid_volume->wwid);
2110 		leapraid_raid_volume_remove(adapter, raid_volume);
2111 	}
2112 
2113 	list_for_each_entry_safe(leapraid_port, next_port,
2114 				 &adapter->dev_topo.card.sas_port_list,
2115 				 port_list) {
2116 		if (leapraid_port->remote_identify.device_type ==
2117 		    SAS_END_DEVICE)
2118 			leapraid_sas_dev_remove_by_sas_address(
2119 				adapter,
2120 				leapraid_port->remote_identify.sas_address,
2121 				leapraid_port->card_port);
2122 		else if (leapraid_port->remote_identify.device_type ==
2123 				SAS_EDGE_EXPANDER_DEVICE ||
2124 			 leapraid_port->remote_identify.device_type ==
2125 				SAS_FANOUT_EXPANDER_DEVICE)
2126 			leapraid_exp_rm(
2127 				adapter,
2128 				leapraid_port->remote_identify.sas_address,
2129 				leapraid_port->card_port);
2130 	}
2131 
2132 	list_for_each_entry_safe(port, port_next,
2133 				 &adapter->dev_topo.card_port_list, list) {
2134 		if (port->vphys_mask)
2135 			list_for_each_entry_safe(vphy, vphy_next,
2136 						 &port->vphys_list, list) {
2137 				list_del(&vphy->list);
2138 				kfree(vphy);
2139 			}
2140 		list_del(&port->list);
2141 		kfree(port);
2142 	}
2143 
2144 	if (adapter->dev_topo.card.phys_num) {
2145 		kfree(adapter->dev_topo.card.card_phy);
2146 		adapter->dev_topo.card.card_phy = NULL;
2147 		adapter->dev_topo.card.phys_num = 0;
2148 	}
2149 }
2150 
2151 static void leapraid_remove(struct pci_dev *pdev)
2152 {
2153 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2154 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2155 	struct workqueue_struct *wq;
2156 	unsigned long flags;
2157 
2158 	if (!shost || !adapter) {
2159 		dev_err(&pdev->dev, "Unable to remove!\n");
2160 		return;
2161 	}
2162 
2163 	wait_event(adapter->scan_dev_desc.wait_driver_loading,
2164 		   !adapter->scan_dev_desc.driver_loading);
2165 	wait_event(adapter->scan_dev_desc.wait_driver_loading,
2166 		   !atomic_read(&adapter->overheat_desc.thermal_alert));
2167 
2168 	WRITE_ONCE(adapter->access_ctrl.host_removing, 1);
2169 	spin_lock(&leapraid_adapter_lock);
2170 	list_del(&adapter->list);
2171 	spin_unlock(&leapraid_adapter_lock);
2172 
2173 	leapraid_wait_cmds_done(adapter);
2174 
2175 	if (leapraid_pci_removed(adapter)) {
2176 		leapraid_mq_polling_pause(adapter);
2177 		leapraid_clean_active_scsi_cmds(adapter);
2178 	}
2179 	leapraid_clean_active_fw_evt(adapter);
2180 
2181 	spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags);
2182 	wq = adapter->fw_evt_s.fw_evt_thread;
2183 	adapter->fw_evt_s.fw_evt_thread = NULL;
2184 	spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags);
2185 	if (wq)
2186 		destroy_workqueue(wq);
2187 
2188 	sas_remove_host(shost);
2189 	leapraid_cleanup_lists(adapter);
2190 	leapraid_remove_ctrl(adapter);
2191 	scsi_host_put(shost);
2192 }
2193 
2194 static void leapraid_shutdown(struct pci_dev *pdev)
2195 {
2196 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2197 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2198 	struct workqueue_struct *wq;
2199 	unsigned long flags;
2200 
2201 	if (!shost || !adapter) {
2202 		dev_err(&pdev->dev, "Unable to shutdown!\n");
2203 		return;
2204 	}
2205 
2206 	adapter->access_ctrl.host_removing = 1;
2207 	leapraid_wait_cmds_done(adapter);
2208 	leapraid_clean_active_fw_evt(adapter);
2209 	leapraid_overheat_cleanup(adapter);
2210 	leapraid_fw_log_stop(adapter);
2211 	spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags);
2212 	wq = adapter->fw_evt_s.fw_evt_thread;
2213 	adapter->fw_evt_s.fw_evt_thread = NULL;
2214 	spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags);
2215 	if (wq)
2216 		destroy_workqueue(wq);
2217 
2218 	leapraid_disable_controller(adapter);
2219 }
2220 
2221 static pci_ers_result_t leapraid_pci_error_detected(struct pci_dev *pdev,
2222 						    pci_channel_state_t state)
2223 {
2224 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2225 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2226 
2227 	if (!shost || !adapter) {
2228 		dev_err(&pdev->dev, "Failed to error detected for device\n");
2229 		return PCI_ERS_RESULT_DISCONNECT;
2230 	}
2231 
2232 	dev_err(&pdev->dev, "%s: PCI error detected, state=%d\n",
2233 		adapter->adapter_attr.name, state);
2234 
2235 	switch (state) {
2236 	case pci_channel_io_normal:
2237 		return PCI_ERS_RESULT_CAN_RECOVER;
2238 	case pci_channel_io_frozen:
2239 		adapter->access_ctrl.pcie_recovering = 1;
2240 		scsi_block_requests(adapter->shost);
2241 		leapraid_overheat_cleanup(adapter);
2242 		leapraid_check_scheduled_fault_stop(adapter);
2243 		leapraid_fw_log_stop(adapter);
2244 		leapraid_disable_controller(adapter);
2245 		return PCI_ERS_RESULT_NEED_RESET;
2246 	case pci_channel_io_perm_failure:
2247 		adapter->access_ctrl.pcie_recovering = 1;
2248 		leapraid_overheat_cleanup(adapter);
2249 		leapraid_check_scheduled_fault_stop(adapter);
2250 		leapraid_fw_log_stop(adapter);
2251 		leapraid_mq_polling_pause(adapter);
2252 		leapraid_clean_active_scsi_cmds(adapter);
2253 		return PCI_ERS_RESULT_DISCONNECT;
2254 	}
2255 
2256 	return PCI_ERS_RESULT_NEED_RESET;
2257 }
2258 
2259 static pci_ers_result_t leapraid_pci_mmio_enabled(struct pci_dev *pdev)
2260 {
2261 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2262 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2263 
2264 	if (!shost || !adapter) {
2265 		dev_err(&pdev->dev,
2266 			"Failed to enable mmio for device\n");
2267 		return PCI_ERS_RESULT_DISCONNECT;
2268 	}
2269 
2270 	dev_info(&pdev->dev, "%s: PCI error mmio enabled\n",
2271 		 adapter->adapter_attr.name);
2272 
2273 	return PCI_ERS_RESULT_RECOVERED;
2274 }
2275 
2276 static pci_ers_result_t leapraid_pci_slot_reset(struct pci_dev *pdev)
2277 {
2278 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2279 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2280 	int rc;
2281 
2282 	if (!shost || !adapter) {
2283 		dev_err(&pdev->dev,
2284 			"Failed to slot reset for device\n");
2285 		return PCI_ERS_RESULT_DISCONNECT;
2286 	}
2287 
2288 	dev_err(&pdev->dev, "%s PCI error slot reset\n",
2289 		adapter->adapter_attr.name);
2290 
2291 	adapter->pdev = pdev;
2292 	pci_restore_state(pdev);
2293 	if (leapraid_set_pcie_and_notification(adapter)) {
2294 		dev_err(&pdev->dev,
2295 			"%s: Failed to set PCIe state and notification\n",
2296 			__func__);
2297 		return PCI_ERS_RESULT_DISCONNECT;
2298 	}
2299 
2300 	adapter->access_ctrl.pcie_recovering = 0;
2301 	dev_info(&pdev->dev, "%s: Hard reset triggered by PCI slot reset\n",
2302 		 adapter->adapter_attr.name);
2303 	dev_info(&adapter->pdev->dev, "%s: %d: call hard_reset\n",
2304 		 __func__, __LINE__);
2305 	rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
2306 	if (rc)
2307 		dev_err(&pdev->dev, "%s hard reset: failed\n",
2308 			adapter->adapter_attr.name);
2309 
2310 	return rc == 0 ? PCI_ERS_RESULT_RECOVERED :
2311 		 PCI_ERS_RESULT_DISCONNECT;
2312 }
2313 
2314 static void leapraid_pci_resume(struct pci_dev *pdev)
2315 {
2316 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2317 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2318 
2319 	if (!shost || !adapter) {
2320 		dev_err(&pdev->dev, "Failed to resume\n");
2321 		return;
2322 	}
2323 
2324 	dev_err(&pdev->dev, "PCI error resume!\n");
2325 
2326 	pci_aer_clear_nonfatal_status(pdev);
2327 	leapraid_check_scheduled_fault_start(adapter);
2328 	leapraid_fw_log_start(adapter);
2329 	scsi_unblock_requests(adapter->shost);
2330 }
2331 
2332 MODULE_DEVICE_TABLE(pci, leapraid_pci_table);
2333 static struct pci_error_handlers leapraid_err_handler = {
2334 	.error_detected = leapraid_pci_error_detected,
2335 	.mmio_enabled = leapraid_pci_mmio_enabled,
2336 	.slot_reset = leapraid_pci_slot_reset,
2337 	.resume = leapraid_pci_resume,
2338 };
2339 
2340 #ifdef CONFIG_PM
2341 static int leapraid_suspend(struct pci_dev *pdev, pm_message_t state)
2342 {
2343 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2344 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2345 	pci_power_t device_state;
2346 
2347 	if (!shost || !adapter) {
2348 		dev_err(&pdev->dev,
2349 			"Suspend failed, invalid host or adapter\n");
2350 		return -ENXIO;
2351 	}
2352 
2353 	leapraid_overheat_cleanup(adapter);
2354 	leapraid_check_scheduled_fault_stop(adapter);
2355 	leapraid_fw_log_stop(adapter);
2356 	scsi_block_requests(shost);
2357 	device_state = pci_choose_state(pdev, state);
2358 
2359 	dev_info(&pdev->dev, "Entering PCI power state D%d, (slot=%s)\n",
2360 		 device_state, pci_name(pdev));
2361 
2362 	pci_save_state(pdev);
2363 	leapraid_disable_controller(adapter);
2364 	pci_set_power_state(pdev, device_state);
2365 	return 0;
2366 }
2367 
2368 static int leapraid_resume(struct pci_dev *pdev)
2369 {
2370 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2371 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2372 	pci_power_t device_state = pdev->current_state;
2373 	int rc;
2374 
2375 	if (!shost || !adapter) {
2376 		dev_err(&pdev->dev,
2377 			"Resume failed, invalid host or adapter\n");
2378 		return -ENXIO;
2379 	}
2380 
2381 	dev_info(&pdev->dev,
2382 		 "Resuming device %s, previous state D%d\n",
2383 		 pci_name(pdev), device_state);
2384 
2385 	pci_set_power_state(pdev, PCI_D0);
2386 	pci_enable_wake(pdev, PCI_D0, 0);
2387 	pci_restore_state(pdev);
2388 	adapter->pdev = pdev;
2389 	rc = leapraid_set_pcie_and_notification(adapter);
2390 	if (rc) {
2391 		dev_err(&pdev->dev,
2392 			"%s: Failed to set PCIe state and notification\n",
2393 			__func__);
2394 		return rc;
2395 	}
2396 
2397 	dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
2398 		 __func__, __LINE__);
2399 	rc = leapraid_hard_reset_handler(adapter, PART_RESET);
2400 	if (rc) {
2401 		dev_err(&adapter->pdev->dev,
2402 			"%s: Hard reset failed during resume, rc=%d\n",
2403 			__func__, rc);
2404 		return rc;
2405 	}
2406 	scsi_unblock_requests(shost);
2407 	leapraid_check_scheduled_fault_start(adapter);
2408 	leapraid_fw_log_start(adapter);
2409 	return 0;
2410 }
2411 #endif /* CONFIG_PM */
2412 
2413 static struct pci_driver leapraid_driver = {
2414 	.name = LEAPRAID_DRIVER_NAME,
2415 	.id_table = leapraid_pci_table,
2416 	.probe = leapraid_probe,
2417 	.remove = leapraid_remove,
2418 	.shutdown = leapraid_shutdown,
2419 	.err_handler = &leapraid_err_handler,
2420 #ifdef CONFIG_PM
2421 	.suspend = leapraid_suspend,
2422 	.resume = leapraid_resume,
2423 #endif /* CONFIG_PM */
2424 };
2425 
2426 static int __init leapraid_init(void)
2427 {
2428 	int error;
2429 
2430 	pr_info("%s initializing\n", LEAPRAID_DRIVER_NAME);
2431 
2432 	leapraid_transport_template =
2433 		sas_attach_transport(&leapraid_transport_functions);
2434 	if (!leapraid_transport_template) {
2435 		pr_err("%s: Failed to attach SAS transport\n",
2436 		       LEAPRAID_DRIVER_NAME);
2437 		return -ENODEV;
2438 	}
2439 
2440 	error = pci_register_driver(&leapraid_driver);
2441 	if (error) {
2442 		pr_err("%s: PCI driver registration failed: %d\n",
2443 		       LEAPRAID_DRIVER_NAME, error);
2444 		sas_release_transport(leapraid_transport_template);
2445 		return error;
2446 	}
2447 
2448 	error = leapraid_ctl_init();
2449 	if (error) {
2450 		pci_unregister_driver(&leapraid_driver);
2451 		sas_release_transport(leapraid_transport_template);
2452 		return error;
2453 	}
2454 
2455 	return 0;
2456 }
2457 
2458 static void __exit leapraid_exit(void)
2459 {
2460 	pr_info("%s exiting\n", LEAPRAID_DRIVER_NAME);
2461 
2462 	leapraid_ctl_exit();
2463 	pci_unregister_driver(&leapraid_driver);
2464 	sas_release_transport(leapraid_transport_template);
2465 }
2466 
2467 module_init(leapraid_init);
2468 module_exit(leapraid_exit);
2469