xref: /linux/drivers/scsi/leapraid/leapraid_os.c (revision 04feb5fd2c23894ebc2cdaf4a4cc38b8667b17ac)
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_set_sense(scmd, 0, ILLEGAL_REQUEST,
386 			       INVALID_COMMAND_OP_CODE);
387 	}
388 }
389 
390 static void leapraid_handle_success_status(
391 		struct leapraid_scsiio_rep *scsiio_rep,
392 		struct scsi_cmnd *scmd,
393 		u32 response_code)
394 {
395 	u8 scsi_status = scsiio_rep->scsi_status;
396 	u8 scsi_state = scsiio_rep->scsi_state;
397 
398 	scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) | scsi_status;
399 
400 	if (response_code == LEAPRAID_TM_RSP_INVALID_FRAME ||
401 	    (scsi_state & (LEAPRAID_SCSI_STATE_AUTOSENSE_FAILED |
402 			   LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)))
403 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
404 	else if (scsi_state & LEAPRAID_SCSI_STATE_TERMINATED)
405 		scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
406 }
407 
408 static void leapraid_scsiio_done_dispatch(
409 		struct leapraid_adapter *adapter,
410 		struct leapraid_scsiio_rep *scsiio_rep,
411 		struct leapraid_sdev_priv *sdev_priv,
412 		struct scsi_cmnd *scmd,
413 		u16 taskid, u32 response_code)
414 {
415 	u8 scsi_status = scsiio_rep->scsi_status;
416 	u8 scsi_state = scsiio_rep->scsi_state;
417 	u16 adapter_status;
418 	u32 xfer_cnt;
419 	u32 sz;
420 
421 	adapter_status = le16_to_cpu(scsiio_rep->adapter_status) &
422 				     LEAPRAID_ADAPTER_STATUS_MASK;
423 
424 	xfer_cnt = le32_to_cpu(scsiio_rep->transfer_count);
425 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
426 
427 	if (adapter_status == LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN &&
428 	    xfer_cnt == 0 &&
429 	    (scsi_status == LEAPRAID_SCSI_STATUS_BUSY ||
430 	    scsi_status == LEAPRAID_SCSI_STATUS_RESERVATION_CONFLICT ||
431 	    scsi_status == LEAPRAID_SCSI_STATUS_TASK_SET_FULL))
432 		adapter_status = LEAPRAID_ADAPTER_STATUS_SUCCESS;
433 
434 	switch (adapter_status) {
435 	case LEAPRAID_ADAPTER_STATUS_SCSI_DEVICE_NOT_THERE:
436 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
437 		break;
438 
439 	case LEAPRAID_ADAPTER_STATUS_BUSY:
440 	case LEAPRAID_ADAPTER_STATUS_INSUFFICIENT_RESOURCES:
441 		scmd->result = SAM_STAT_BUSY;
442 		break;
443 
444 	case LEAPRAID_ADAPTER_STATUS_SCSI_RESIDUAL_MISMATCH:
445 		if (xfer_cnt == 0 || scmd->underflow > xfer_cnt)
446 			scmd->result = DID_SOFT_ERROR <<
447 				LEAPRAID_SCSI_HOST_SHIFT;
448 		else
449 			scmd->result = (DID_OK << LEAPRAID_SCSI_HOST_SHIFT) |
450 				scsi_status;
451 		break;
452 
453 	case LEAPRAID_ADAPTER_STATUS_SCSI_ADAPTER_TERMINATED:
454 		if (sdev_priv->block) {
455 			scmd->result = DID_TRANSPORT_DISRUPTED <<
456 				LEAPRAID_SCSI_HOST_SHIFT;
457 			return;
458 		}
459 
460 		if (scmd->device->channel == RAID_CHANNEL &&
461 		    scsi_state == (LEAPRAID_SCSI_STATE_TERMINATED |
462 				    LEAPRAID_SCSI_STATE_NO_SCSI_STATUS)) {
463 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
464 			break;
465 		}
466 
467 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
468 		break;
469 
470 	case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_TERMINATED:
471 	case LEAPRAID_ADAPTER_STATUS_SCSI_EXT_TERMINATED:
472 		scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
473 		break;
474 
475 	case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_UNDERRUN:
476 		leapraid_handle_data_underrun(scsiio_rep, scmd, xfer_cnt);
477 		break;
478 
479 	case LEAPRAID_ADAPTER_STATUS_SCSI_DATA_OVERRUN:
480 		scsi_set_resid(scmd, 0);
481 		leapraid_handle_success_status(scsiio_rep, scmd,
482 					       response_code);
483 		break;
484 	case LEAPRAID_ADAPTER_STATUS_SCSI_RECOVERED_ERROR:
485 	case LEAPRAID_ADAPTER_STATUS_SUCCESS:
486 		leapraid_handle_success_status(scsiio_rep, scmd,
487 					       response_code);
488 		break;
489 
490 	case LEAPRAID_ADAPTER_STATUS_SCSI_PROTOCOL_ERROR:
491 	case LEAPRAID_ADAPTER_STATUS_INTERNAL_ERROR:
492 	case LEAPRAID_ADAPTER_STATUS_SCSI_IO_DATA_ERROR:
493 	case LEAPRAID_ADAPTER_STATUS_SCSI_TASK_MGMT_FAILED:
494 	default:
495 		scmd->result = DID_SOFT_ERROR << LEAPRAID_SCSI_HOST_SHIFT;
496 		break;
497 	}
498 
499 	if (!scmd->result)
500 		return;
501 
502 	scsi_print_command(scmd);
503 	dev_warn(&adapter->pdev->dev,
504 		 "SCSI I/O: hdl=0x%x, status: 0x%x, 0x%x, 0x%x\n",
505 		 le16_to_cpu(scsiio_rep->dev_hdl), adapter_status,
506 		 scsi_status, scsi_state);
507 
508 	if (scsi_state & LEAPRAID_SCSI_STATE_AUTOSENSE_VALID) {
509 		struct scsi_sense_hdr sshdr;
510 
511 		sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
512 			   le32_to_cpu(scsiio_rep->sense_count));
513 		if (scsi_normalize_sense(scmd->sense_buffer, sz,
514 					 &sshdr))
515 			dev_warn(&adapter->pdev->dev,
516 				 "Sense: key=0x%x asc=0x%x ascq=0x%x\n",
517 				 sshdr.sense_key,
518 				 scsi_sense_asc(&sshdr),
519 				 scsi_sense_ascq(&sshdr));
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_set_sense(scmd, 0, UNIT_ATTENTION,
814 			       I_T_NEXUS_LOSS_OCCURRED);
815 		goto scsiio_done;
816 	}
817 
818 	if (adapter->access_ctrl.shost_recovering ||
819 	    adapter->reset_desc.adapter_link_resetting) {
820 		*rc = SCSI_MLQUEUE_HOST_BUSY;
821 		return false;
822 	}
823 
824 	if (starget_priv->deleted || sdev_priv->deleted)
825 		goto no_connect;
826 
827 	if (starget_priv->tm_busy || sdev_priv->block) {
828 		*rc = SCSI_MLQUEUE_DEVICE_BUSY;
829 		return false;
830 	}
831 
832 	return true;
833 
834 no_connect:
835 	scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
836 scsiio_done:
837 	scsi_done(scmd);
838 
839 	return false;
840 }
841 
842 static u32 build_scsiio_req_control(struct scsi_cmnd *scmd,
843 				    struct leapraid_sdev_priv *sdev_priv)
844 {
845 	u32 control;
846 
847 	switch (scmd->sc_data_direction) {
848 	case DMA_FROM_DEVICE:
849 		control = LEAPRAID_SCSIIO_CTRL_READ;
850 		break;
851 	case DMA_TO_DEVICE:
852 		control = LEAPRAID_SCSIIO_CTRL_WRITE;
853 		break;
854 	default:
855 		control = LEAPRAID_SCSIIO_CTRL_NODATATRANSFER;
856 		break;
857 	}
858 
859 	control |= LEAPRAID_SCSIIO_CTRL_SIMPLEQ;
860 
861 	if (sdev_priv->ncq_prio_enable &&
862 	    (IOPRIO_PRIO_CLASS(req_get_ioprio(scsi_cmd_to_rq(scmd))) ==
863 	     IOPRIO_CLASS_RT))
864 		control |= LEAPRAID_SCSIIO_CTRL_CMDPRI;
865 	if (scmd->cmd_len == 32)
866 		control |= LEAPRAID_SCSIIO_CTRL_CDB_32BYTE <<
867 				LEAPRAID_SCSIIO_CTRL_CDB_LEN_SHIFT;
868 
869 	return control;
870 }
871 
872 enum scsi_qc_status leapraid_queuecommand(struct Scsi_Host *shost,
873 					  struct scsi_cmnd *scmd)
874 {
875 	struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
876 	struct leapraid_sdev_priv *sdev_priv = scmd->device->hostdata;
877 	struct leapraid_starget_priv *starget_priv;
878 	struct leapraid_scsiio_req *scsiio_req;
879 	u32 control;
880 	u16 taskid;
881 	u16 hdl;
882 	enum scsi_qc_status rc = 0;
883 
884 	if (!leapraid_should_queuecommand(adapter, sdev_priv, scmd, &rc))
885 		return rc;
886 
887 	starget_priv = sdev_priv->starget_priv;
888 	hdl = starget_priv->hdl;
889 	control = build_scsiio_req_control(scmd, sdev_priv);
890 
891 	taskid = leapraid_alloc_scsiio_taskid(adapter, scmd);
892 	scsiio_req = leapraid_get_task_desc(adapter, taskid);
893 
894 	scsiio_req->func = LEAPRAID_FUNC_SCSIIO;
895 	if (sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER)
896 		scsiio_req->func = LEAPRAID_FUNC_SCSIIO_RAID_PASSTHROUGH;
897 	else
898 		scsiio_req->func = LEAPRAID_FUNC_SCSIIO;
899 
900 	scsiio_req->dev_hdl = cpu_to_le16(hdl);
901 	scsiio_req->data_len = cpu_to_le32(scsi_bufflen(scmd));
902 	scsiio_req->ctrl = cpu_to_le32(control);
903 	scsiio_req->io_flg = cpu_to_le16(scmd->cmd_len);
904 	scsiio_req->msg_flg = 0;
905 	scsiio_req->sense_buffer_len = SCSI_SENSE_BUFFERSIZE;
906 	scsiio_req->sense_buffer_low_add =
907 		leapraid_get_sense_buffer_dma(adapter, taskid);
908 	scsiio_req->sgl_offset0 =
909 		offsetof(struct leapraid_scsiio_req, sgl) /
910 		LEAPRAID_DWORDS_BYTE_SIZE;
911 	int_to_scsilun(sdev_priv->lun, (struct scsi_lun *)scsiio_req->lun);
912 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
913 	if (scsiio_req->data_len) {
914 		if (leapraid_build_scmd_ieee_sg(adapter, scmd, taskid)) {
915 			leapraid_free_taskid(adapter, taskid);
916 			return SCSI_MLQUEUE_HOST_BUSY;
917 		}
918 	} else {
919 		leapraid_build_ieee_nodata_sg(adapter, &scsiio_req->sgl);
920 	}
921 
922 	if (likely(scsiio_req->func == LEAPRAID_FUNC_SCSIIO))
923 		leapraid_fire_scsi_io(adapter, taskid,
924 				      le16_to_cpu(scsiio_req->dev_hdl));
925 	else
926 		leapraid_fire_task(adapter, taskid);
927 
928 	dev_dbg(&adapter->pdev->dev,
929 		"LEAPRAID_SCSIIO: Send Descriptor taskid %d, req type 0x%x\n",
930 		taskid, scsiio_req->func);
931 	return rc;
932 }
933 
934 static int leapraid_init_cmd_priv(struct Scsi_Host *shost,
935 				  struct scsi_cmnd *scmd)
936 {
937 	struct leapraid_adapter *adapter = shost_priv(shost);
938 
939 	return leapraid_internal_init_cmd_priv(adapter, scsi_cmd_priv(scmd));
940 }
941 
942 static int leapraid_exit_cmd_priv(struct Scsi_Host *shost,
943 				  struct scsi_cmnd *scmd)
944 {
945 	struct leapraid_adapter *adapter = shost_priv(shost);
946 
947 	leapraid_internal_exit_cmd_priv(adapter, scsi_cmd_priv(scmd));
948 
949 	return 0;
950 }
951 
952 static int leapraid_error_handler(struct scsi_cmnd *scmd,
953 				  const char *str, u8 type)
954 {
955 	struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
956 	struct scsi_target *starget = scmd->device->sdev_target;
957 	struct leapraid_starget_priv *starget_priv = starget->hostdata;
958 	struct leapraid_io_req_tracker *io_req_tracker = NULL;
959 	struct leapraid_sdev_priv *sdev_priv;
960 	struct leapraid_sas_dev *sas_dev = NULL;
961 	u16 hdl;
962 	int rc;
963 
964 	dev_info(&adapter->pdev->dev,
965 		 "EH enter: type=%s, scmd=0x%p, req tag=%d\n", str, scmd,
966 		 scsi_cmd_to_rq(scmd)->tag);
967 	scsi_print_command(scmd);
968 
969 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
970 		io_req_tracker = scsi_cmd_priv(scmd);
971 		dev_info(&adapter->pdev->dev,
972 			 "EH ABORT: scmd=0x%p, pend=%ums, tout=%ums, tag=%d\n",
973 			 scmd,
974 			 jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc),
975 			 (scsi_cmd_to_rq(scmd)->timeout / HZ) * 1000,
976 			 scsi_cmd_to_rq(scmd)->tag);
977 	}
978 
979 	if (leapraid_pci_removed(adapter) ||
980 	    adapter->access_ctrl.host_removing) {
981 		dev_err(&adapter->pdev->dev,
982 			"EH %s failed: %s scmd=0x%p\n", str,
983 			(adapter->access_ctrl.host_removing ?
984 			"shost removing!" : "pci_dev removed!"), scmd);
985 		if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK &&
986 		    io_req_tracker && io_req_tracker->taskid)
987 			leapraid_free_taskid(adapter, io_req_tracker->taskid);
988 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
989 #ifdef FAST_IO_FAIL
990 		rc = FAST_IO_FAIL;
991 #else
992 		rc = FAILED;
993 #endif
994 		goto out_eh_done;
995 	}
996 
997 	sdev_priv = scmd->device->hostdata;
998 	if (!sdev_priv || !sdev_priv->starget_priv) {
999 		dev_warn(&adapter->pdev->dev,
1000 			 "EH %s: SAS dev or starget gone, scmd=0x%p\n",
1001 			 str, scmd);
1002 		scmd->result = DID_NO_CONNECT << LEAPRAID_SCSI_HOST_SHIFT;
1003 		scsi_done(scmd);
1004 		rc = SUCCESS;
1005 		goto out_eh_done;
1006 	}
1007 
1008 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
1009 		if (!io_req_tracker) {
1010 			dev_warn(&adapter->pdev->dev,
1011 				 "EH ABORT: No I/O tracker, scmd 0x%p\n", scmd);
1012 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1013 			rc = SUCCESS;
1014 			goto out_eh_done;
1015 		}
1016 
1017 		if (sdev_priv->starget_priv->flg &
1018 			LEAPRAID_TGT_FLG_RAID_MEMBER ||
1019 		    sdev_priv->starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
1020 			dev_err(&adapter->pdev->dev,
1021 				"EH ABORT: Skip RAID/VOLUME, scmd=0x%p\n",
1022 				scmd);
1023 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1024 			rc = FAILED;
1025 			goto out_eh_done;
1026 		}
1027 
1028 		hdl = sdev_priv->starget_priv->hdl;
1029 	} else {
1030 		hdl = 0;
1031 		if (sdev_priv->starget_priv->flg &
1032 		    LEAPRAID_TGT_FLG_RAID_MEMBER) {
1033 			sas_dev = leapraid_get_sas_dev_from_tgt(adapter,
1034 								starget_priv);
1035 			if (sas_dev)
1036 				hdl = sas_dev->volume_hdl;
1037 		} else {
1038 			hdl = sdev_priv->starget_priv->hdl;
1039 		}
1040 
1041 		if (!hdl) {
1042 			dev_err(&adapter->pdev->dev,
1043 				"EH %s failed: Target handle 0, scmd=0x%p\n",
1044 				str, scmd);
1045 			scmd->result = DID_RESET << LEAPRAID_SCSI_HOST_SHIFT;
1046 			rc = FAILED;
1047 			goto out_eh_done;
1048 		}
1049 	}
1050 
1051 	dev_info(&adapter->pdev->dev,
1052 		 "EH issue TM: type=%s, scmd=0x%p, hdl=0x%x\n",
1053 		 str, scmd, hdl);
1054 
1055 	rc = leapraid_issue_locked_tm(
1056 			adapter,
1057 			hdl,
1058 			scmd->device->channel,
1059 			scmd->device->id,
1060 			(type == LEAPRAID_TM_TASKTYPE_TARGET_RESET ?
1061 				0 : scmd->device->lun),
1062 			type,
1063 			(type == LEAPRAID_TM_TASKTYPE_ABORT_TASK ?
1064 				io_req_tracker->taskid : 0),
1065 			LEAPRAID_TM_MSGFLAGS_LINK_RESET);
1066 
1067 out_eh_done:
1068 	if (type == LEAPRAID_TM_TASKTYPE_ABORT_TASK) {
1069 		if (rc != SUCCESS)
1070 			dev_err(&adapter->pdev->dev,
1071 				"EH ABORT result: failed, scmd=0x%p\n",
1072 				scmd);
1073 	} else {
1074 		if (rc != SUCCESS)
1075 			dev_err(&adapter->pdev->dev,
1076 				"EH %s result: failed, scmd=0x%p\n",
1077 				str, scmd);
1078 		if (sas_dev)
1079 			leapraid_sdev_put(sas_dev);
1080 	}
1081 	return rc;
1082 }
1083 
1084 static int leapraid_eh_abort_handler(struct scsi_cmnd *scmd)
1085 {
1086 	return leapraid_error_handler(scmd, "ABORT TASK",
1087 				      LEAPRAID_TM_TASKTYPE_ABORT_TASK);
1088 }
1089 
1090 static int leapraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
1091 {
1092 	return leapraid_error_handler(scmd, "UNIT RESET",
1093 				      LEAPRAID_TM_TASKTYPE_LOGICAL_UNIT_RESET);
1094 }
1095 
1096 static int leapraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
1097 {
1098 	return leapraid_error_handler(scmd, "TARGET RESET",
1099 				      LEAPRAID_TM_TASKTYPE_TARGET_RESET);
1100 }
1101 
1102 static int leapraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
1103 {
1104 	struct leapraid_adapter *adapter = shost_priv(scmd->device->host);
1105 	int rc;
1106 
1107 	dev_info(&adapter->pdev->dev,
1108 		 "EH HOST RESET enter: scmd=%p, req tag=%d\n",
1109 		 scmd,
1110 		 scsi_cmd_to_rq(scmd)->tag);
1111 	scsi_print_command(scmd);
1112 
1113 	if (adapter->scan_dev_desc.driver_loading ||
1114 	    adapter->access_ctrl.host_removing) {
1115 		dev_err(&adapter->pdev->dev,
1116 			"EH HOST RESET failed: %s scmd=0x%p\n",
1117 			(adapter->access_ctrl.host_removing ?
1118 			"shost removing!" : "driver loading!"), scmd);
1119 		rc = FAILED;
1120 		goto out_host_reset_done;
1121 	}
1122 
1123 	dev_info(&adapter->pdev->dev, "%s:%d: Issuing hard reset\n",
1124 		 __func__, __LINE__);
1125 	if (leapraid_hard_reset_handler(adapter, FULL_RESET) < 0)
1126 		rc = FAILED;
1127 	else
1128 		rc = SUCCESS;
1129 
1130 out_host_reset_done:
1131 	if (rc != SUCCESS)
1132 		dev_err(&adapter->pdev->dev,
1133 			"EH HOST RESET result: failed, scmd=0x%p\n",
1134 			scmd);
1135 	return rc;
1136 }
1137 
1138 static int leapraid_sdev_init(struct scsi_device *sdev)
1139 {
1140 	struct leapraid_raid_volume *raid_volume;
1141 	struct leapraid_starget_priv *stgt_priv;
1142 	struct leapraid_sdev_priv *sdev_priv;
1143 	struct leapraid_adapter *adapter;
1144 	struct leapraid_sas_dev *sas_dev;
1145 	struct scsi_target *tgt;
1146 	struct Scsi_Host *shost;
1147 	unsigned long flags;
1148 
1149 	sdev_priv = kzalloc_obj(*sdev_priv);
1150 	if (!sdev_priv)
1151 		return -ENOMEM;
1152 
1153 	sdev_priv->lun = sdev->lun;
1154 	sdev_priv->flg = LEAPRAID_DEVICE_FLG_INIT;
1155 	tgt = scsi_target(sdev);
1156 	stgt_priv = tgt->hostdata;
1157 	stgt_priv->num_luns++;
1158 	sdev_priv->starget_priv = stgt_priv;
1159 	sdev->hostdata = sdev_priv;
1160 	if (stgt_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER)
1161 		sdev->no_uld_attach = LEAPRAID_NO_ULD_ATTACH;
1162 
1163 	shost = dev_to_shost(&tgt->dev);
1164 	adapter = shost_priv(shost);
1165 	if (tgt->channel == RAID_CHANNEL) {
1166 		raid_volume = leapraid_raid_volume_find_by_id(adapter,
1167 							      tgt->id,
1168 							      tgt->channel);
1169 		if (raid_volume) {
1170 			spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock,
1171 					  flags);
1172 			raid_volume->sdev = sdev;
1173 			spin_unlock_irqrestore(
1174 				&adapter->dev_topo.raid_volume_lock, flags);
1175 			leapraid_raid_volume_put(raid_volume);
1176 		}
1177 	}
1178 
1179 	if (!(stgt_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) {
1180 		spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1181 		sas_dev = leapraid_hold_lock_get_sas_dev_by_addr(
1182 				adapter,
1183 				stgt_priv->sas_address,
1184 				stgt_priv->card_port);
1185 		if (sas_dev && !sas_dev->starget) {
1186 			sdev_printk(KERN_INFO, sdev,
1187 				    "%s: Assign starget to sas_dev\n",
1188 				    __func__);
1189 			sas_dev->starget = tgt;
1190 		}
1191 
1192 		if (sas_dev)
1193 			leapraid_sdev_put(sas_dev);
1194 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1195 	}
1196 	return 0;
1197 }
1198 
1199 static bool leapraid_slave_cfg_volume(struct scsi_device *sdev,
1200 				      struct queue_limits *lim)
1201 {
1202 	struct Scsi_Host *shost = sdev->host;
1203 	struct leapraid_adapter *adapter = shost_priv(shost);
1204 	struct leapraid_raid_volume *raid_volume;
1205 	struct leapraid_starget_priv *starget_priv;
1206 	struct leapraid_sdev_priv *sdev_priv;
1207 	int qd;
1208 	u16 hdl;
1209 
1210 	sdev_priv = sdev->hostdata;
1211 	starget_priv = sdev_priv->starget_priv;
1212 	hdl = starget_priv->hdl;
1213 
1214 	raid_volume = leapraid_raid_volume_find_by_hdl(adapter, hdl);
1215 	if (!raid_volume) {
1216 		sdev_printk(KERN_WARNING, sdev,
1217 			    "%s: RAID volume not found, hdl=0x%x\n",
1218 			    __func__, hdl);
1219 		return 1;
1220 	}
1221 
1222 	if (leapraid_get_volume_cap(adapter, raid_volume)) {
1223 		sdev_printk(KERN_ERR, sdev,
1224 			    "%s: Failed to get volume cap, hdl=0x%x\n",
1225 			    __func__, hdl);
1226 		leapraid_raid_volume_put(raid_volume);
1227 		return 1;
1228 	}
1229 
1230 	qd = (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT) ?
1231 		adapter->adapter_attr.narrowport_max_queue_depth :
1232 		adapter->adapter_attr.sata_max_queue_depth;
1233 	if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0)
1234 		qd = adapter->adapter_attr.raid_volume_max_queue_depth;
1235 
1236 	sdev_printk(KERN_INFO, sdev,
1237 		    "RAID volume: hdl=0x%04x, wwid=0x%016llx\n",
1238 		    raid_volume->hdl, (unsigned long long)raid_volume->wwid);
1239 
1240 	if (shost->max_sectors > LEAPRAID_MAX_SECTORS)
1241 		lim->max_hw_sectors = LEAPRAID_MAX_SECTORS;
1242 
1243 	leapraid_change_queue_depth(sdev, qd);
1244 	leapraid_raid_volume_put(raid_volume);
1245 	return 0;
1246 }
1247 
1248 static bool leapraid_slave_configure_extra(struct scsi_device *sdev,
1249 					   struct leapraid_sas_dev **psas_dev,
1250 					   u16 vol_hdl, u64 volume_wwid,
1251 					   bool *is_target_ssp, int *qd)
1252 {
1253 	struct leapraid_sas_dev *sas_dev;
1254 	struct leapraid_sdev_priv *sdev_priv;
1255 	struct Scsi_Host *shost = sdev->host;
1256 	struct leapraid_adapter *adapter = shost_priv(shost);
1257 	unsigned long flags;
1258 
1259 	sdev_priv = sdev->hostdata;
1260 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1261 	*is_target_ssp = false;
1262 	sas_dev = leapraid_hold_lock_get_sas_dev_by_addr(
1263 			adapter,
1264 			sdev_priv->starget_priv->sas_address,
1265 			sdev_priv->starget_priv->card_port);
1266 	if (!sas_dev) {
1267 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1268 		sdev_printk(KERN_WARNING, sdev,
1269 			    "%s: SAS dev not found, sas=0x%llx\n",
1270 			    __func__, sdev_priv->starget_priv->sas_address);
1271 		return 1;
1272 	}
1273 
1274 	*psas_dev = sas_dev;
1275 	sas_dev->volume_hdl = vol_hdl;
1276 	sas_dev->volume_wwid = volume_wwid;
1277 	if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) {
1278 		*qd = (sas_dev->port_connection > 1) ?
1279 			adapter->adapter_attr.wideport_max_queue_depth :
1280 			adapter->adapter_attr.narrowport_max_queue_depth;
1281 		*is_target_ssp = true;
1282 		if (sas_dev->dev_info & LEAPRAID_DEVTYP_SEP)
1283 			sdev_priv->sep = 1;
1284 	} else {
1285 		*qd = adapter->adapter_attr.sata_max_queue_depth;
1286 	}
1287 
1288 	sdev_printk(KERN_INFO, sdev,
1289 		    "device name=0x%016llx, SAS addr=0x%016llx\n",
1290 		    (unsigned long long)sas_dev->dev_name,
1291 		    (unsigned long long)sas_dev->sas_addr);
1292 	leapraid_sdev_put(sas_dev);
1293 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1294 	return 0;
1295 }
1296 
1297 static int leapraid_sdev_configure(struct scsi_device *sdev,
1298 				   struct queue_limits *lim)
1299 {
1300 	struct leapraid_sas_dev *sas_dev;
1301 	struct leapraid_sdev_priv *sdev_priv;
1302 	struct Scsi_Host *shost = sdev->host;
1303 	struct leapraid_starget_priv *starget_priv;
1304 	struct leapraid_adapter *adapter;
1305 	u16 hdl, vol_hdl = 0;
1306 	bool is_target_ssp = false;
1307 	u64 volume_wwid = 0;
1308 	int qd = 1;
1309 
1310 	adapter = shost_priv(shost);
1311 	sdev_priv = sdev->hostdata;
1312 	sdev_priv->flg &= ~LEAPRAID_DEVICE_FLG_INIT;
1313 	starget_priv = sdev_priv->starget_priv;
1314 	hdl = starget_priv->hdl;
1315 	if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)
1316 		return leapraid_slave_cfg_volume(sdev, lim);
1317 
1318 	if (starget_priv->flg & LEAPRAID_TGT_FLG_RAID_MEMBER) {
1319 		if (leapraid_cfg_get_volume_hdl(adapter, hdl, &vol_hdl)) {
1320 			sdev_printk(KERN_WARNING, sdev,
1321 				    "%s: Get volume hdl failed, hdl=0x%x\n",
1322 				    __func__, hdl);
1323 			return 1;
1324 		}
1325 
1326 		if (vol_hdl && leapraid_cfg_get_volume_wwid(adapter, vol_hdl,
1327 							    &volume_wwid)) {
1328 			sdev_printk(KERN_WARNING, sdev,
1329 				    "%s: Get wwid failed, volume_hdl=0x%x\n",
1330 				    __func__, vol_hdl);
1331 			return 1;
1332 		}
1333 	}
1334 
1335 	if (leapraid_slave_configure_extra(sdev, &sas_dev, vol_hdl,
1336 					   volume_wwid, &is_target_ssp, &qd)) {
1337 		sdev_printk(KERN_WARNING, sdev,
1338 			    "%s: slave_configure_extra failed\n", __func__);
1339 		return 1;
1340 	}
1341 
1342 	leapraid_change_queue_depth(sdev, qd);
1343 	if (is_target_ssp)
1344 		sas_read_port_mode_page(sdev);
1345 
1346 	return 0;
1347 }
1348 
1349 static void leapraid_sdev_destroy(struct scsi_device *sdev)
1350 {
1351 	struct leapraid_adapter *adapter;
1352 	struct Scsi_Host *shost;
1353 	struct leapraid_sas_dev *sas_dev;
1354 	struct leapraid_starget_priv *starget_priv;
1355 	struct scsi_target *stgt;
1356 	unsigned long flags;
1357 
1358 	if (!sdev->hostdata)
1359 		return;
1360 
1361 	stgt = scsi_target(sdev);
1362 	starget_priv = stgt->hostdata;
1363 	starget_priv->num_luns--;
1364 	shost = dev_to_shost(&stgt->dev);
1365 	adapter = shost_priv(shost);
1366 	if (!(starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME)) {
1367 		spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1368 		sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter,
1369 								  starget_priv);
1370 		if (sas_dev && !starget_priv->num_luns)
1371 			sas_dev->starget = NULL;
1372 		if (sas_dev)
1373 			leapraid_sdev_put(sas_dev);
1374 		spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1375 	}
1376 
1377 	kfree(sdev->hostdata);
1378 	sdev->hostdata = NULL;
1379 }
1380 
1381 static int leapraid_target_alloc_raid(struct scsi_target *tgt)
1382 {
1383 	struct leapraid_starget_priv *starget_priv;
1384 	struct leapraid_raid_volume *raid_volume;
1385 	struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
1386 	struct leapraid_adapter *adapter = shost_priv(shost);
1387 	unsigned long flags;
1388 
1389 	starget_priv = tgt->hostdata;
1390 	raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id,
1391 						      tgt->channel);
1392 	if (raid_volume) {
1393 		spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
1394 		starget_priv->hdl = raid_volume->hdl;
1395 		starget_priv->sas_address = raid_volume->wwid;
1396 		starget_priv->flg |= LEAPRAID_TGT_FLG_VOLUME;
1397 		raid_volume->starget = tgt;
1398 		spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock,
1399 				       flags);
1400 		leapraid_raid_volume_put(raid_volume);
1401 	}
1402 	return 0;
1403 }
1404 
1405 static int leapraid_target_alloc_sas(struct scsi_target *tgt)
1406 {
1407 	struct sas_rphy *rphy;
1408 	struct Scsi_Host *shost;
1409 	struct leapraid_sas_dev *sas_dev;
1410 	struct leapraid_adapter *adapter;
1411 	struct leapraid_starget_priv *starget_priv;
1412 	unsigned long flags;
1413 
1414 	shost = dev_to_shost(&tgt->dev);
1415 	adapter = shost_priv(shost);
1416 	starget_priv = tgt->hostdata;
1417 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1418 	rphy = dev_to_rphy(tgt->dev.parent);
1419 	sas_dev = leapraid_hold_lock_get_sas_dev_by_addr_and_rphy(
1420 			adapter,
1421 			rphy->identify.sas_address,
1422 			rphy);
1423 	if (sas_dev) {
1424 		starget_priv->sas_dev = sas_dev;
1425 		starget_priv->card_port = sas_dev->card_port;
1426 		starget_priv->sas_address = sas_dev->sas_addr;
1427 		starget_priv->hdl = sas_dev->hdl;
1428 		sas_dev->channel = tgt->channel;
1429 		sas_dev->id = tgt->id;
1430 		sas_dev->starget = tgt;
1431 		if (sas_dev->hdl &&
1432 		    sas_dev->hdl <=
1433 		    adapter->adapter_attr.features.max_dev_handle &&
1434 		    test_bit(sas_dev->hdl, adapter->dev_topo.pd_hdls))
1435 			starget_priv->flg |= LEAPRAID_TGT_FLG_RAID_MEMBER;
1436 	}
1437 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1438 
1439 	return 0;
1440 }
1441 
1442 static int leapraid_target_alloc(struct scsi_target *tgt)
1443 {
1444 	struct leapraid_starget_priv *starget_priv;
1445 
1446 	starget_priv = kzalloc_obj(*starget_priv);
1447 	if (!starget_priv)
1448 		return -ENOMEM;
1449 
1450 	tgt->hostdata = starget_priv;
1451 	starget_priv->starget = tgt;
1452 	starget_priv->hdl = LEAPRAID_INVALID_DEV_HANDLE;
1453 	if (tgt->channel == RAID_CHANNEL)
1454 		return leapraid_target_alloc_raid(tgt);
1455 
1456 	return leapraid_target_alloc_sas(tgt);
1457 }
1458 
1459 static void leapraid_target_destroy_raid(struct scsi_target *tgt)
1460 {
1461 	struct leapraid_raid_volume *raid_volume;
1462 	struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
1463 	struct leapraid_adapter *adapter = shost_priv(shost);
1464 	unsigned long flags;
1465 
1466 	raid_volume = leapraid_raid_volume_find_by_id(adapter, tgt->id,
1467 						      tgt->channel);
1468 	if (raid_volume) {
1469 		spin_lock_irqsave(&adapter->dev_topo.raid_volume_lock, flags);
1470 		raid_volume->starget = NULL;
1471 		raid_volume->sdev = NULL;
1472 		spin_unlock_irqrestore(&adapter->dev_topo.raid_volume_lock,
1473 				       flags);
1474 		leapraid_raid_volume_put(raid_volume);
1475 	}
1476 }
1477 
1478 static void leapraid_target_destroy_sas(struct scsi_target *tgt)
1479 {
1480 	struct leapraid_adapter *adapter;
1481 	struct leapraid_sas_dev *sas_dev;
1482 	struct leapraid_starget_priv *starget_priv;
1483 	struct Scsi_Host *shost;
1484 	unsigned long flags;
1485 
1486 	shost = dev_to_shost(&tgt->dev);
1487 	adapter = shost_priv(shost);
1488 	starget_priv = tgt->hostdata;
1489 
1490 	spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
1491 	sas_dev = leapraid_hold_lock_get_sas_dev_from_tgt(adapter,
1492 							  starget_priv);
1493 	if (sas_dev &&
1494 	    sas_dev->starget == tgt &&
1495 	    sas_dev->id == tgt->id &&
1496 	    sas_dev->channel == tgt->channel)
1497 		sas_dev->starget = NULL;
1498 
1499 	if (sas_dev) {
1500 		starget_priv->sas_dev = NULL;
1501 		leapraid_sdev_put(sas_dev);
1502 		leapraid_sdev_put(sas_dev);
1503 	}
1504 	spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
1505 }
1506 
1507 static void leapraid_target_destroy(struct scsi_target *tgt)
1508 {
1509 	struct leapraid_starget_priv *starget_priv;
1510 
1511 	starget_priv = tgt->hostdata;
1512 	if (!starget_priv)
1513 		return;
1514 
1515 	if (tgt->channel == RAID_CHANNEL) {
1516 		leapraid_target_destroy_raid(tgt);
1517 		goto out_free;
1518 	}
1519 
1520 	leapraid_target_destroy_sas(tgt);
1521 
1522 out_free:
1523 	kfree(starget_priv);
1524 	tgt->hostdata = NULL;
1525 }
1526 
1527 static bool leapraid_scan_check_status(struct leapraid_adapter *adapter,
1528 				       bool *need_hard_reset)
1529 {
1530 	u32 adapter_state;
1531 
1532 	if (adapter->scan_dev_desc.scan_start) {
1533 		adapter_state = leapraid_get_adapter_state(adapter);
1534 		if (adapter_state == LEAPRAID_DB_FAULT) {
1535 			*need_hard_reset = true;
1536 			return true;
1537 		}
1538 		return false;
1539 	}
1540 
1541 	if (adapter->driver_cmds.scan_dev_cmd.status & LEAPRAID_CMD_RESET) {
1542 		dev_err(&adapter->pdev->dev,
1543 			"Device scan: Aborted due to reset\n");
1544 		adapter->driver_cmds.scan_dev_cmd.status =
1545 			LEAPRAID_CMD_NOT_USED;
1546 		adapter->scan_dev_desc.driver_loading = 0;
1547 		wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1548 		return true;
1549 	}
1550 
1551 	if (adapter->scan_dev_desc.scan_start_failed) {
1552 		dev_err(&adapter->pdev->dev,
1553 			"Device scan: Failed with adapter_status=0x%08x\n",
1554 			adapter->scan_dev_desc.scan_start_failed);
1555 		adapter->scan_dev_desc.driver_loading = 0;
1556 		wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1557 		adapter->scan_dev_desc.wait_scan_dev_done = 0;
1558 		adapter->access_ctrl.host_removing = 1;
1559 		return true;
1560 	}
1561 
1562 	adapter->driver_cmds.scan_dev_cmd.status = LEAPRAID_CMD_NOT_USED;
1563 	leapraid_scan_dev_done(adapter);
1564 	return true;
1565 }
1566 
1567 static int leapraid_scan_finished(struct Scsi_Host *shost, unsigned long time)
1568 {
1569 	struct leapraid_adapter *adapter = shost_priv(shost);
1570 	bool need_hard_reset = false;
1571 
1572 	if (time >= (LEAPRAID_SCAN_DEV_CMD_TIMEOUT * HZ)) {
1573 		adapter->driver_cmds.scan_dev_cmd.status =
1574 			LEAPRAID_CMD_NOT_USED;
1575 		dev_err(&adapter->pdev->dev,
1576 			"Device scan: Failed with timeout 300s\n");
1577 		adapter->scan_dev_desc.driver_loading = 0;
1578 		wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1579 		return 1;
1580 	}
1581 
1582 	if (!leapraid_scan_check_status(adapter, &need_hard_reset))
1583 		return 0;
1584 
1585 	if (need_hard_reset) {
1586 		adapter->driver_cmds.scan_dev_cmd.status =
1587 			LEAPRAID_CMD_NOT_USED;
1588 		dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
1589 			 __func__, __LINE__);
1590 		if (leapraid_hard_reset_handler(adapter, PART_RESET)) {
1591 			adapter->scan_dev_desc.driver_loading = 0;
1592 			wake_up(&adapter->scan_dev_desc.wait_driver_loading);
1593 		}
1594 	}
1595 
1596 	return 1;
1597 }
1598 
1599 static void leapraid_scan_start(struct Scsi_Host *shost)
1600 {
1601 	struct leapraid_adapter *adapter = shost_priv(shost);
1602 
1603 	adapter->scan_dev_desc.scan_start = 1;
1604 	leapraid_scan_dev(adapter, true);
1605 }
1606 
1607 static u32 leapraid_get_raid_qd(struct leapraid_adapter *adapter,
1608 				const struct leapraid_raid_volume *raid_volume,
1609 				bool default_qd)
1610 {
1611 	const struct leapraid_adapter_attr *attr = &adapter->adapter_attr;
1612 
1613 	if (raid_volume->vol_type != LEAPRAID_VOL_TYPE_RAID0)
1614 		return default_qd ? LEAPRAID_RAID_QUEUE_DEPTH :
1615 			attr->raid_volume_max_queue_depth;
1616 
1617 	if (raid_volume->dev_info & LEAPRAID_DEVTYP_SSP_TGT)
1618 		return default_qd ? LEAPRAID_SAS_QUEUE_DEPTH :
1619 			attr->narrowport_max_queue_depth;
1620 
1621 	return default_qd ? LEAPRAID_SATA_QUEUE_DEPTH :
1622 		attr->sata_max_queue_depth;
1623 }
1624 
1625 static int leapraid_calc_max_queue_depth(struct scsi_device *sdev, int qdepth)
1626 {
1627 	struct Scsi_Host *shost;
1628 	struct leapraid_adapter *adapter;
1629 	struct leapraid_starget_priv *starget_priv;
1630 	struct leapraid_sdev_priv *sdev_priv;
1631 	struct leapraid_raid_volume *raid_volume;
1632 	struct leapraid_sas_dev *sas_dev;
1633 	int max_depth;
1634 	u32 default_qdepth = 0;
1635 	u32 fw_qdepth = 0;
1636 
1637 	shost = sdev->host;
1638 	adapter = shost_priv(shost);
1639 	max_depth = shost->can_queue;
1640 
1641 	sdev_priv = sdev->hostdata;
1642 	if (!sdev_priv)
1643 		goto out_tag_check;
1644 
1645 	starget_priv = sdev_priv->starget_priv;
1646 	if (!starget_priv)
1647 		goto out_tag_check;
1648 
1649 	if (starget_priv->flg & LEAPRAID_TGT_FLG_VOLUME) {
1650 		raid_volume = leapraid_raid_volume_find_by_hdl(
1651 					adapter, starget_priv->hdl);
1652 		if (raid_volume) {
1653 			default_qdepth = leapraid_get_raid_qd(
1654 					adapter, raid_volume, true);
1655 			fw_qdepth = leapraid_get_raid_qd(
1656 					adapter, raid_volume, false);
1657 			leapraid_raid_volume_put(raid_volume);
1658 		}
1659 		goto out_limit_check;
1660 	}
1661 
1662 	sas_dev = leapraid_get_sas_dev_from_tgt(adapter, starget_priv);
1663 	if (sas_dev) {
1664 		if (sas_dev->dev_info & LEAPRAID_DEVTYP_SSP_TGT) {
1665 			default_qdepth = LEAPRAID_SAS_QUEUE_DEPTH;
1666 			fw_qdepth = (sas_dev->port_connection > 1) ?
1667 			      adapter->adapter_attr.wideport_max_queue_depth :
1668 			      adapter->adapter_attr.narrowport_max_queue_depth;
1669 		}
1670 		if (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV) {
1671 			default_qdepth = LEAPRAID_SATA_QUEUE_DEPTH;
1672 			fw_qdepth = adapter->adapter_attr.sata_max_queue_depth;
1673 		}
1674 		leapraid_sdev_put(sas_dev);
1675 	}
1676 
1677 out_limit_check:
1678 	if (fw_qdepth > shost->can_queue && default_qdepth)
1679 		fw_qdepth = default_qdepth;
1680 
1681 	if (fw_qdepth)
1682 		max_depth = min_t(int, max_depth, fw_qdepth);
1683 
1684 out_tag_check:
1685 	if (!sdev->tagged_supported)
1686 		max_depth = 1;
1687 
1688 	if (qdepth > max_depth)
1689 		qdepth = max_depth;
1690 
1691 	return qdepth;
1692 }
1693 
1694 int leapraid_change_queue_depth(struct scsi_device *sdev, int qdepth)
1695 {
1696 	qdepth = leapraid_calc_max_queue_depth(sdev, qdepth);
1697 	scsi_change_queue_depth(sdev, qdepth);
1698 	return sdev->queue_depth;
1699 }
1700 
1701 static void leapraid_map_queues(struct Scsi_Host *shost)
1702 {
1703 	struct leapraid_adapter *adapter;
1704 	struct blk_mq_queue_map *queue_map;
1705 	int msix_queue_count;
1706 	int poll_queue_count;
1707 	int queue_offset;
1708 	int map_index;
1709 
1710 	adapter = (struct leapraid_adapter *)shost->hostdata;
1711 	if (shost->nr_hw_queues == 1)
1712 		return;
1713 
1714 	msix_queue_count = adapter->notification_desc.iopoll_qdex;
1715 	poll_queue_count = adapter->adapter_attr.rq_cnt - msix_queue_count;
1716 
1717 	queue_offset = 0;
1718 	for (map_index = 0; map_index < shost->nr_maps; map_index++) {
1719 		queue_map = &shost->tag_set.map[map_index];
1720 		queue_map->nr_queues = 0;
1721 
1722 		switch (map_index) {
1723 		case HCTX_TYPE_DEFAULT:
1724 			queue_map->nr_queues = msix_queue_count;
1725 			queue_map->queue_offset = queue_offset;
1726 			WARN_ON_ONCE(!queue_map->nr_queues);
1727 			blk_mq_map_hw_queues(queue_map,
1728 					     &adapter->pdev->dev, 0);
1729 			break;
1730 		case HCTX_TYPE_POLL:
1731 			queue_map->nr_queues = poll_queue_count;
1732 			queue_map->queue_offset = queue_offset;
1733 			blk_mq_map_queues(queue_map);
1734 			break;
1735 		default:
1736 			queue_map->queue_offset = queue_offset;
1737 			blk_mq_map_hw_queues(queue_map,
1738 					     &adapter->pdev->dev, 0);
1739 			break;
1740 		}
1741 		queue_offset += queue_map->nr_queues;
1742 	}
1743 }
1744 
1745 int leapraid_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
1746 {
1747 	struct leapraid_adapter *adapter =
1748 		(struct leapraid_adapter *)shost->hostdata;
1749 	struct leapraid_blk_mq_poll_rq *blk_mq_poll_rq;
1750 	int num_entries;
1751 	int qid = queue_num - adapter->notification_desc.iopoll_qdex;
1752 
1753 	blk_mq_poll_rq = &adapter->notification_desc.blk_mq_poll_rqs[qid];
1754 	if (atomic_read(&blk_mq_poll_rq->pause) ||
1755 	    !atomic_add_unless(&blk_mq_poll_rq->busy, 1, 1))
1756 		return 0;
1757 
1758 	num_entries = leapraid_rep_queue_handler(&blk_mq_poll_rq->rq);
1759 	atomic_dec(&blk_mq_poll_rq->busy);
1760 	return num_entries;
1761 }
1762 
1763 static int leapraid_bios_param(struct scsi_device *sdev,
1764 			       struct gendisk *disk, sector_t capacity,
1765 			       int geom[])
1766 {
1767 	int heads;
1768 	int sectors;
1769 	sector_t cylinders;
1770 
1771 	if (scsi_partsize(disk, capacity, geom))
1772 		return 0;
1773 
1774 	if ((ulong)capacity >= LEAPRAID_LARGE_DISK_THRESHOLD) {
1775 		heads = LEAPRAID_LARGE_DISK_HEADS;
1776 		sectors = LEAPRAID_LARGE_DISK_SECTORS;
1777 	} else {
1778 		heads = LEAPRAID_SMALL_DISK_HEADS;
1779 		sectors = LEAPRAID_SMALL_DISK_SECTORS;
1780 	}
1781 
1782 	cylinders = capacity;
1783 	sector_div(cylinders, heads * sectors);
1784 
1785 	geom[0] = heads;
1786 	geom[1] = sectors;
1787 	geom[2] = cylinders;
1788 	return 0;
1789 }
1790 
1791 static ssize_t fw_queue_depth_show(struct device *cdev,
1792 				   struct device_attribute *attr,
1793 				   char *buf)
1794 {
1795 	struct Scsi_Host *shost = class_to_shost(cdev);
1796 	struct leapraid_adapter *adapter = shost_priv(shost);
1797 
1798 	return sysfs_emit(buf, "%02d\n",
1799 			  adapter->adapter_attr.features.req_slot);
1800 }
1801 
1802 static ssize_t host_sas_address_show(struct device *cdev,
1803 				     struct device_attribute *attr, char *buf)
1804 {
1805 	struct Scsi_Host *shost = class_to_shost(cdev);
1806 	struct leapraid_adapter *adapter = shost_priv(shost);
1807 
1808 	return sysfs_emit(buf, "0x%016llx\n",
1809 		(unsigned long long)adapter->dev_topo.card.sas_address);
1810 }
1811 
1812 static ssize_t board_name_show(struct device *cdev,
1813 			       struct device_attribute *attr, char *buf)
1814 {
1815 	struct Scsi_Host *shost = class_to_shost(cdev);
1816 	struct leapraid_adapter *adapter = shost_priv(shost);
1817 
1818 	return sysfs_emit(buf, "%s\n", adapter->adapter_attr.board_name);
1819 }
1820 
1821 static DEVICE_ATTR_RO(fw_queue_depth);
1822 static DEVICE_ATTR_RO(host_sas_address);
1823 static DEVICE_ATTR_RO(board_name);
1824 
1825 static struct attribute *leapraid_shost_attrs[] = {
1826 	&dev_attr_fw_queue_depth.attr,
1827 	&dev_attr_host_sas_address.attr,
1828 	&dev_attr_board_name.attr,
1829 	NULL,
1830 };
1831 
1832 ATTRIBUTE_GROUPS(leapraid_shost);
1833 
1834 static ssize_t sas_device_handle_show(struct device *dev,
1835 				      struct device_attribute *attr, char *buf)
1836 {
1837 	struct scsi_device *sdev = to_scsi_device(dev);
1838 	struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1839 
1840 	if (!sas_device_priv_data || !sas_device_priv_data->starget_priv) {
1841 		dev_err(&sdev->sdev_gendev,
1842 			"%s: Invalid sdev_priv or starget_priv\n", __func__);
1843 		return -EINVAL;
1844 	}
1845 
1846 	return sysfs_emit(buf, "0x%04x\n",
1847 			  sas_device_priv_data->starget_priv->hdl);
1848 }
1849 
1850 static ssize_t sas_ncq_prio_supported_show(struct device *dev,
1851 					   struct device_attribute *attr,
1852 					   char *buf)
1853 {
1854 	struct scsi_device *sdev = to_scsi_device(dev);
1855 
1856 	return sysfs_emit(buf, "%d\n", sas_ata_ncq_prio_supported(sdev));
1857 }
1858 
1859 static ssize_t sas_ncq_prio_enable_show(struct device *dev,
1860 					struct device_attribute *attr,
1861 					char *buf)
1862 {
1863 	struct scsi_device *sdev = to_scsi_device(dev);
1864 	struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1865 
1866 	if (!sas_device_priv_data) {
1867 		dev_err(&sdev->sdev_gendev,
1868 			"%s: Invalid sdev_priv\n", __func__);
1869 		return -EINVAL;
1870 	}
1871 
1872 	return sysfs_emit(buf, "%d\n", sas_device_priv_data->ncq_prio_enable);
1873 }
1874 
1875 static ssize_t sas_ncq_prio_enable_store(struct device *dev,
1876 					 struct device_attribute *attr,
1877 					 const char *buf, size_t count)
1878 {
1879 	struct scsi_device *sdev = to_scsi_device(dev);
1880 	struct leapraid_sdev_priv *sas_device_priv_data = sdev->hostdata;
1881 	bool enable;
1882 
1883 	if (!sas_device_priv_data) {
1884 		dev_err(&sdev->sdev_gendev,
1885 			"%s: Invalid sdev_priv\n", __func__);
1886 		return -EINVAL;
1887 	}
1888 
1889 	if (kstrtobool(buf, &enable))
1890 		return -EINVAL;
1891 
1892 	if (!sas_ata_ncq_prio_supported(sdev))
1893 		return -EINVAL;
1894 
1895 	sas_device_priv_data->ncq_prio_enable = enable;
1896 	return count;
1897 }
1898 
1899 static DEVICE_ATTR_RO(sas_device_handle);
1900 static DEVICE_ATTR_RO(sas_ncq_prio_supported);
1901 static DEVICE_ATTR_RW(sas_ncq_prio_enable);
1902 
1903 static bool leapraid_sdev_is_sata(struct scsi_device *sdev)
1904 {
1905 	struct scsi_target *starget = sdev->sdev_target;
1906 	struct leapraid_starget_priv *starget_priv = starget->hostdata;
1907 	struct leapraid_sas_dev *sas_dev;
1908 
1909 	if (!starget_priv)
1910 		return false;
1911 
1912 	sas_dev = starget_priv->sas_dev;
1913 	return sas_dev && (sas_dev->dev_info & LEAPRAID_DEVTYP_SATA_DEV);
1914 }
1915 
1916 static struct attribute *leapraid_sdev_attrs[] = {
1917 	&dev_attr_sas_device_handle.attr,
1918 	&dev_attr_sas_ncq_prio_supported.attr,
1919 	&dev_attr_sas_ncq_prio_enable.attr,
1920 	NULL,
1921 };
1922 
1923 static umode_t leapraid_sdev_attr_is_visible(struct kobject *kobj,
1924 					     struct attribute *attr, int i)
1925 {
1926 	struct device *dev = kobj_to_dev(kobj);
1927 	struct scsi_device *sdev = to_scsi_device(dev);
1928 
1929 	if (attr == &dev_attr_sas_ncq_prio_supported.attr ||
1930 	    attr == &dev_attr_sas_ncq_prio_enable.attr)
1931 		if (!leapraid_sdev_is_sata(sdev))
1932 			return 0;
1933 
1934 	return attr->mode;
1935 }
1936 
1937 static const struct attribute_group leapraid_sdev_attr_group = {
1938 	.attrs = leapraid_sdev_attrs,
1939 	.is_visible = leapraid_sdev_attr_is_visible,
1940 };
1941 
1942 static const struct attribute_group *leapraid_sdev_groups[] = {
1943 	&leapraid_sdev_attr_group,
1944 	NULL,
1945 };
1946 
1947 static struct scsi_host_template leapraid_driver_template = {
1948 	.module = THIS_MODULE,
1949 	.name = "LEAPIO RAID Host",
1950 	.proc_name = LEAPRAID_DRIVER_NAME,
1951 	.queuecommand = leapraid_queuecommand,
1952 	.cmd_size = sizeof(struct leapraid_io_req_tracker),
1953 	.init_cmd_priv = leapraid_init_cmd_priv,
1954 	.exit_cmd_priv = leapraid_exit_cmd_priv,
1955 	.eh_abort_handler = leapraid_eh_abort_handler,
1956 	.eh_device_reset_handler = leapraid_eh_device_reset_handler,
1957 	.eh_target_reset_handler = leapraid_eh_target_reset_handler,
1958 	.eh_host_reset_handler = leapraid_eh_host_reset_handler,
1959 	.sdev_init = leapraid_sdev_init,
1960 	.sdev_destroy = leapraid_sdev_destroy,
1961 	.sdev_configure = leapraid_sdev_configure,
1962 	.target_alloc = leapraid_target_alloc,
1963 	.target_destroy = leapraid_target_destroy,
1964 	.scan_finished = leapraid_scan_finished,
1965 	.scan_start = leapraid_scan_start,
1966 	.change_queue_depth = leapraid_change_queue_depth,
1967 	.map_queues = leapraid_map_queues,
1968 	.mq_poll = leapraid_blk_mq_poll,
1969 	.bios_param = leapraid_bios_param,
1970 	.can_queue = LEAPRAID_CAN_QUEUE_MIN,
1971 	.this_id = LEAPRAID_THIS_ID_NONE,
1972 	.sg_tablesize = LEAPRAID_SG_DEPTH,
1973 	.max_sectors = LEAPRAID_MAX_SECTORS,
1974 	.max_segment_size = LEAPRAID_MAX_SEGMENT_SIZE,
1975 	.cmd_per_lun = LEAPRAID_CMD_PER_LUN,
1976 	.shost_groups = leapraid_shost_groups,
1977 	.sdev_groups = leapraid_sdev_groups,
1978 	.track_queue_depth = 1,
1979 };
1980 
1981 static void leapraid_lock_init(struct leapraid_adapter *adapter)
1982 {
1983 	mutex_init(&adapter->reset_desc.adapter_reset_mutex);
1984 	mutex_init(&adapter->reset_desc.host_diag_mutex);
1985 	mutex_init(&adapter->access_ctrl.pci_access_lock);
1986 
1987 	spin_lock_init(&adapter->reset_desc.adapter_reset_lock);
1988 	spin_lock_init(&adapter->dynamic_task_desc.task_lock);
1989 	spin_lock_init(&adapter->dev_topo.sas_dev_lock);
1990 	spin_lock_init(&adapter->dev_topo.topo_node_lock);
1991 	spin_lock_init(&adapter->fw_evt_s.fw_evt_lock);
1992 	spin_lock_init(&adapter->dev_topo.raid_volume_lock);
1993 	spin_lock_init(&adapter->dev_topo.enc_lock);
1994 	spin_lock_init(&adapter->boot_devs.lock);
1995 }
1996 
1997 static void leapraid_list_init(struct leapraid_adapter *adapter)
1998 {
1999 	INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_list);
2000 	INIT_LIST_HEAD(&adapter->dev_topo.card_port_list);
2001 	INIT_LIST_HEAD(&adapter->dev_topo.sas_dev_init_list);
2002 	INIT_LIST_HEAD(&adapter->dev_topo.exp_list);
2003 	INIT_LIST_HEAD(&adapter->dev_topo.enc_list);
2004 	INIT_LIST_HEAD(&adapter->fw_evt_s.fw_evt_list);
2005 	INIT_LIST_HEAD(&adapter->dev_topo.raid_volume_list);
2006 	INIT_LIST_HEAD(&adapter->dev_topo.card.sas_port_list);
2007 }
2008 
2009 static int leapraid_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2010 {
2011 	struct leapraid_adapter *adapter;
2012 	struct Scsi_Host *shost;
2013 	int iopoll_q_count;
2014 	int rc;
2015 
2016 	shost = scsi_host_alloc(&leapraid_driver_template,
2017 				sizeof(struct leapraid_adapter));
2018 	if (!shost) {
2019 		dev_err(&pdev->dev,
2020 			"%s: SCSI host alloc failed\n", __func__);
2021 		return -ENODEV;
2022 	}
2023 
2024 	adapter = shost_priv(shost);
2025 	memset(adapter, 0, sizeof(struct leapraid_adapter));
2026 
2027 	init_waitqueue_head(&adapter->access_ctrl.recovery_waitq);
2028 	adapter->adapter_attr.id = atomic_inc_return(&leapraid_ids) - 1;
2029 
2030 	adapter->adapter_attr.enable_mp = enable_mp;
2031 
2032 	adapter = shost_priv(shost);
2033 	INIT_LIST_HEAD(&adapter->list);
2034 
2035 	adapter->shost = shost;
2036 	adapter->pdev = pdev;
2037 	adapter->fw_log_desc.open_pcie_trace = open_pcie_trace;
2038 	atomic_set(&adapter->fw_log_desc.mmap_refcnt, 0);
2039 	init_waitqueue_head(&adapter->fw_log_desc.mmap_waitq);
2040 	leapraid_lock_init(adapter);
2041 	leapraid_list_init(adapter);
2042 	snprintf(adapter->adapter_attr.name, LEAPRAID_NAME_LENGTH, "%s%d",
2043 		 LEAPRAID_DRIVER_NAME, adapter->adapter_attr.id);
2044 
2045 	shost->max_cmd_len = LEAPRAID_MAX_CDB_LEN;
2046 	shost->max_lun = LEAPRAID_MAX_LUNS;
2047 	shost->transportt = leapraid_transport_template;
2048 	shost->unique_id = adapter->adapter_attr.id;
2049 
2050 	adapter->fw_evt_s.fw_evt_thread =
2051 		alloc_ordered_workqueue("fw_event_%s%d", 0,
2052 					LEAPRAID_DRIVER_NAME,
2053 					adapter->adapter_attr.id);
2054 	if (!adapter->fw_evt_s.fw_evt_thread) {
2055 		dev_err(&adapter->pdev->dev,
2056 			"%s: Failed to create fw event workqueue\n", __func__);
2057 		rc = -ENODEV;
2058 		goto evt_wq_fail;
2059 	}
2060 
2061 	shost->host_tagset = 1;
2062 	init_waitqueue_head(&adapter->scan_dev_desc.wait_driver_loading);
2063 	adapter->scan_dev_desc.driver_loading = 1;
2064 	if (leapraid_ctrl_init(adapter)) {
2065 		dev_err(&adapter->pdev->dev,
2066 			"%s: Adapter init failed\n", __func__);
2067 		rc = -ENODEV;
2068 		goto ctrl_init_fail;
2069 	}
2070 
2071 	shost->nr_hw_queues = 1;
2072 	if (shost->host_tagset) {
2073 		shost->nr_hw_queues = adapter->adapter_attr.rq_cnt;
2074 		iopoll_q_count = adapter->adapter_attr.rq_cnt -
2075 				 adapter->notification_desc.iopoll_qdex;
2076 		shost->nr_maps = iopoll_q_count ? 3 : 1;
2077 		dev_info(&adapter->pdev->dev,
2078 			 "Max scsi I/O cmds %d shared with nr_hw_queues=%d\n",
2079 			 shost->can_queue, shost->nr_hw_queues);
2080 	}
2081 
2082 	rc = scsi_add_host(shost, &pdev->dev);
2083 	if (rc) {
2084 		dev_err(&pdev->dev,
2085 			"%s: SCSI host add failed\n", __func__);
2086 		goto scsi_add_shost_fail;
2087 	}
2088 
2089 	spin_lock(&leapraid_adapter_lock);
2090 	list_add_tail(&adapter->list, &leapraid_adapter_list);
2091 	spin_unlock(&leapraid_adapter_lock);
2092 
2093 	scsi_scan_host(shost);
2094 	return 0;
2095 
2096 scsi_add_shost_fail:
2097 	leapraid_remove_ctrl(adapter);
2098 ctrl_init_fail:
2099 	leapraid_overheat_cleanup(adapter);
2100 	destroy_workqueue(adapter->fw_evt_s.fw_evt_thread);
2101 evt_wq_fail:
2102 	scsi_host_put(shost);
2103 	return rc;
2104 }
2105 
2106 void leapraid_cleanup_lists(struct leapraid_adapter *adapter)
2107 {
2108 	struct leapraid_raid_volume *raid_volume, *next_raid_volume;
2109 	struct leapraid_starget_priv *starget_priv_data;
2110 	struct leapraid_sas_port *leapraid_port, *next_port;
2111 	struct leapraid_card_port *port, *port_next;
2112 	struct leapraid_vphy *vphy, *vphy_next;
2113 
2114 	list_for_each_entry_safe(raid_volume, next_raid_volume,
2115 				 &adapter->dev_topo.raid_volume_list, list) {
2116 		if (raid_volume->starget) {
2117 			starget_priv_data = raid_volume->starget->hostdata;
2118 			if (starget_priv_data)
2119 				starget_priv_data->deleted = 1;
2120 			scsi_remove_target(&raid_volume->starget->dev);
2121 		}
2122 		dev_info(&adapter->pdev->dev,
2123 			 "removing hdl=0x%04x, wwid=0x%016llx\n",
2124 			 raid_volume->hdl,
2125 			 (unsigned long long)raid_volume->wwid);
2126 		leapraid_raid_volume_remove(adapter, raid_volume);
2127 	}
2128 
2129 	list_for_each_entry_safe(leapraid_port, next_port,
2130 				 &adapter->dev_topo.card.sas_port_list,
2131 				 port_list) {
2132 		if (leapraid_port->remote_identify.device_type ==
2133 		    SAS_END_DEVICE)
2134 			leapraid_sas_dev_remove_by_sas_address(
2135 				adapter,
2136 				leapraid_port->remote_identify.sas_address,
2137 				leapraid_port->card_port);
2138 		else if (leapraid_port->remote_identify.device_type ==
2139 				SAS_EDGE_EXPANDER_DEVICE ||
2140 			 leapraid_port->remote_identify.device_type ==
2141 				SAS_FANOUT_EXPANDER_DEVICE)
2142 			leapraid_exp_rm(
2143 				adapter,
2144 				leapraid_port->remote_identify.sas_address,
2145 				leapraid_port->card_port);
2146 	}
2147 
2148 	list_for_each_entry_safe(port, port_next,
2149 				 &adapter->dev_topo.card_port_list, list) {
2150 		if (port->vphys_mask)
2151 			list_for_each_entry_safe(vphy, vphy_next,
2152 						 &port->vphys_list, list) {
2153 				list_del(&vphy->list);
2154 				kfree(vphy);
2155 			}
2156 		list_del(&port->list);
2157 		kfree(port);
2158 	}
2159 
2160 	if (adapter->dev_topo.card.phys_num) {
2161 		kfree(adapter->dev_topo.card.card_phy);
2162 		adapter->dev_topo.card.card_phy = NULL;
2163 		adapter->dev_topo.card.phys_num = 0;
2164 	}
2165 }
2166 
2167 static void leapraid_remove(struct pci_dev *pdev)
2168 {
2169 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2170 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2171 	struct workqueue_struct *wq;
2172 	unsigned long flags;
2173 
2174 	if (!shost || !adapter) {
2175 		dev_err(&pdev->dev, "Unable to remove!\n");
2176 		return;
2177 	}
2178 
2179 	wait_event(adapter->scan_dev_desc.wait_driver_loading,
2180 		   !adapter->scan_dev_desc.driver_loading);
2181 	wait_event(adapter->scan_dev_desc.wait_driver_loading,
2182 		   !atomic_read(&adapter->overheat_desc.thermal_alert));
2183 
2184 	WRITE_ONCE(adapter->access_ctrl.host_removing, 1);
2185 	spin_lock(&leapraid_adapter_lock);
2186 	list_del(&adapter->list);
2187 	spin_unlock(&leapraid_adapter_lock);
2188 
2189 	leapraid_wait_cmds_done(adapter);
2190 
2191 	if (leapraid_pci_removed(adapter)) {
2192 		leapraid_mq_polling_pause(adapter);
2193 		leapraid_clean_active_scsi_cmds(adapter);
2194 	}
2195 	leapraid_clean_active_fw_evt(adapter);
2196 
2197 	spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags);
2198 	wq = adapter->fw_evt_s.fw_evt_thread;
2199 	adapter->fw_evt_s.fw_evt_thread = NULL;
2200 	spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags);
2201 	if (wq)
2202 		destroy_workqueue(wq);
2203 
2204 	sas_remove_host(shost);
2205 	leapraid_cleanup_lists(adapter);
2206 	leapraid_remove_ctrl(adapter);
2207 	scsi_host_put(shost);
2208 }
2209 
2210 static void leapraid_shutdown(struct pci_dev *pdev)
2211 {
2212 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2213 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2214 	struct workqueue_struct *wq;
2215 	unsigned long flags;
2216 
2217 	if (!shost || !adapter) {
2218 		dev_err(&pdev->dev, "Unable to shutdown!\n");
2219 		return;
2220 	}
2221 
2222 	adapter->access_ctrl.host_removing = 1;
2223 	leapraid_wait_cmds_done(adapter);
2224 	leapraid_clean_active_fw_evt(adapter);
2225 	leapraid_overheat_cleanup(adapter);
2226 	leapraid_fw_log_stop(adapter);
2227 	spin_lock_irqsave(&adapter->fw_evt_s.fw_evt_lock, flags);
2228 	wq = adapter->fw_evt_s.fw_evt_thread;
2229 	adapter->fw_evt_s.fw_evt_thread = NULL;
2230 	spin_unlock_irqrestore(&adapter->fw_evt_s.fw_evt_lock, flags);
2231 	if (wq)
2232 		destroy_workqueue(wq);
2233 
2234 	leapraid_disable_controller(adapter);
2235 }
2236 
2237 static pci_ers_result_t leapraid_pci_error_detected(struct pci_dev *pdev,
2238 						    pci_channel_state_t state)
2239 {
2240 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2241 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2242 
2243 	if (!shost || !adapter) {
2244 		dev_err(&pdev->dev, "Failed to error detected for device\n");
2245 		return PCI_ERS_RESULT_DISCONNECT;
2246 	}
2247 
2248 	dev_err(&pdev->dev, "%s: PCI error detected, state=%d\n",
2249 		adapter->adapter_attr.name, state);
2250 
2251 	switch (state) {
2252 	case pci_channel_io_normal:
2253 		return PCI_ERS_RESULT_CAN_RECOVER;
2254 	case pci_channel_io_frozen:
2255 		adapter->access_ctrl.pcie_recovering = 1;
2256 		scsi_block_requests(adapter->shost);
2257 		leapraid_overheat_cleanup(adapter);
2258 		leapraid_check_scheduled_fault_stop(adapter);
2259 		leapraid_fw_log_stop(adapter);
2260 		leapraid_disable_controller(adapter);
2261 		return PCI_ERS_RESULT_NEED_RESET;
2262 	case pci_channel_io_perm_failure:
2263 		adapter->access_ctrl.pcie_recovering = 1;
2264 		leapraid_overheat_cleanup(adapter);
2265 		leapraid_check_scheduled_fault_stop(adapter);
2266 		leapraid_fw_log_stop(adapter);
2267 		leapraid_mq_polling_pause(adapter);
2268 		leapraid_clean_active_scsi_cmds(adapter);
2269 		return PCI_ERS_RESULT_DISCONNECT;
2270 	}
2271 
2272 	return PCI_ERS_RESULT_NEED_RESET;
2273 }
2274 
2275 static pci_ers_result_t leapraid_pci_mmio_enabled(struct pci_dev *pdev)
2276 {
2277 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2278 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2279 
2280 	if (!shost || !adapter) {
2281 		dev_err(&pdev->dev,
2282 			"Failed to enable mmio for device\n");
2283 		return PCI_ERS_RESULT_DISCONNECT;
2284 	}
2285 
2286 	dev_info(&pdev->dev, "%s: PCI error mmio enabled\n",
2287 		 adapter->adapter_attr.name);
2288 
2289 	return PCI_ERS_RESULT_RECOVERED;
2290 }
2291 
2292 static pci_ers_result_t leapraid_pci_slot_reset(struct pci_dev *pdev)
2293 {
2294 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2295 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2296 	int rc;
2297 
2298 	if (!shost || !adapter) {
2299 		dev_err(&pdev->dev,
2300 			"Failed to slot reset for device\n");
2301 		return PCI_ERS_RESULT_DISCONNECT;
2302 	}
2303 
2304 	dev_err(&pdev->dev, "%s PCI error slot reset\n",
2305 		adapter->adapter_attr.name);
2306 
2307 	adapter->pdev = pdev;
2308 	pci_restore_state(pdev);
2309 	if (leapraid_set_pcie_and_notification(adapter)) {
2310 		dev_err(&pdev->dev,
2311 			"%s: Failed to set PCIe state and notification\n",
2312 			__func__);
2313 		return PCI_ERS_RESULT_DISCONNECT;
2314 	}
2315 
2316 	adapter->access_ctrl.pcie_recovering = 0;
2317 	dev_info(&pdev->dev, "%s: Hard reset triggered by PCI slot reset\n",
2318 		 adapter->adapter_attr.name);
2319 	dev_info(&adapter->pdev->dev, "%s: %d: call hard_reset\n",
2320 		 __func__, __LINE__);
2321 	rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
2322 	if (rc)
2323 		dev_err(&pdev->dev, "%s hard reset: failed\n",
2324 			adapter->adapter_attr.name);
2325 
2326 	return rc == 0 ? PCI_ERS_RESULT_RECOVERED :
2327 		 PCI_ERS_RESULT_DISCONNECT;
2328 }
2329 
2330 static void leapraid_pci_resume(struct pci_dev *pdev)
2331 {
2332 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2333 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2334 
2335 	if (!shost || !adapter) {
2336 		dev_err(&pdev->dev, "Failed to resume\n");
2337 		return;
2338 	}
2339 
2340 	dev_err(&pdev->dev, "PCI error resume!\n");
2341 
2342 	pci_aer_clear_nonfatal_status(pdev);
2343 	leapraid_check_scheduled_fault_start(adapter);
2344 	leapraid_fw_log_start(adapter);
2345 	scsi_unblock_requests(adapter->shost);
2346 }
2347 
2348 MODULE_DEVICE_TABLE(pci, leapraid_pci_table);
2349 static struct pci_error_handlers leapraid_err_handler = {
2350 	.error_detected = leapraid_pci_error_detected,
2351 	.mmio_enabled = leapraid_pci_mmio_enabled,
2352 	.slot_reset = leapraid_pci_slot_reset,
2353 	.resume = leapraid_pci_resume,
2354 };
2355 
2356 #ifdef CONFIG_PM
2357 static int leapraid_suspend(struct pci_dev *pdev, pm_message_t state)
2358 {
2359 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2360 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2361 	pci_power_t device_state;
2362 
2363 	if (!shost || !adapter) {
2364 		dev_err(&pdev->dev,
2365 			"Suspend failed, invalid host or adapter\n");
2366 		return -ENXIO;
2367 	}
2368 
2369 	leapraid_overheat_cleanup(adapter);
2370 	leapraid_check_scheduled_fault_stop(adapter);
2371 	leapraid_fw_log_stop(adapter);
2372 	scsi_block_requests(shost);
2373 	device_state = pci_choose_state(pdev, state);
2374 
2375 	dev_info(&pdev->dev, "Entering PCI power state D%d, (slot=%s)\n",
2376 		 device_state, pci_name(pdev));
2377 
2378 	pci_save_state(pdev);
2379 	leapraid_disable_controller(adapter);
2380 	pci_set_power_state(pdev, device_state);
2381 	return 0;
2382 }
2383 
2384 static int leapraid_resume(struct pci_dev *pdev)
2385 {
2386 	struct leapraid_adapter *adapter = pdev_to_adapter(pdev);
2387 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
2388 	pci_power_t device_state = pdev->current_state;
2389 	int rc;
2390 
2391 	if (!shost || !adapter) {
2392 		dev_err(&pdev->dev,
2393 			"Resume failed, invalid host or adapter\n");
2394 		return -ENXIO;
2395 	}
2396 
2397 	dev_info(&pdev->dev,
2398 		 "Resuming device %s, previous state D%d\n",
2399 		 pci_name(pdev), device_state);
2400 
2401 	pci_set_power_state(pdev, PCI_D0);
2402 	pci_enable_wake(pdev, PCI_D0, 0);
2403 	pci_restore_state(pdev);
2404 	adapter->pdev = pdev;
2405 	rc = leapraid_set_pcie_and_notification(adapter);
2406 	if (rc) {
2407 		dev_err(&pdev->dev,
2408 			"%s: Failed to set PCIe state and notification\n",
2409 			__func__);
2410 		return rc;
2411 	}
2412 
2413 	dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
2414 		 __func__, __LINE__);
2415 	rc = leapraid_hard_reset_handler(adapter, PART_RESET);
2416 	if (rc) {
2417 		dev_err(&adapter->pdev->dev,
2418 			"%s: Hard reset failed during resume, rc=%d\n",
2419 			__func__, rc);
2420 		return rc;
2421 	}
2422 	scsi_unblock_requests(shost);
2423 	leapraid_check_scheduled_fault_start(adapter);
2424 	leapraid_fw_log_start(adapter);
2425 	return 0;
2426 }
2427 #endif /* CONFIG_PM */
2428 
2429 static struct pci_driver leapraid_driver = {
2430 	.name = LEAPRAID_DRIVER_NAME,
2431 	.id_table = leapraid_pci_table,
2432 	.probe = leapraid_probe,
2433 	.remove = leapraid_remove,
2434 	.shutdown = leapraid_shutdown,
2435 	.err_handler = &leapraid_err_handler,
2436 #ifdef CONFIG_PM
2437 	.suspend = leapraid_suspend,
2438 	.resume = leapraid_resume,
2439 #endif /* CONFIG_PM */
2440 };
2441 
2442 static int __init leapraid_init(void)
2443 {
2444 	int error;
2445 
2446 	pr_info("%s initializing\n", LEAPRAID_DRIVER_NAME);
2447 
2448 	leapraid_transport_template =
2449 		sas_attach_transport(&leapraid_transport_functions);
2450 	if (!leapraid_transport_template) {
2451 		pr_err("%s: Failed to attach SAS transport\n",
2452 		       LEAPRAID_DRIVER_NAME);
2453 		return -ENODEV;
2454 	}
2455 
2456 	error = pci_register_driver(&leapraid_driver);
2457 	if (error) {
2458 		pr_err("%s: PCI driver registration failed: %d\n",
2459 		       LEAPRAID_DRIVER_NAME, error);
2460 		sas_release_transport(leapraid_transport_template);
2461 		return error;
2462 	}
2463 
2464 	error = leapraid_ctl_init();
2465 	if (error) {
2466 		pci_unregister_driver(&leapraid_driver);
2467 		sas_release_transport(leapraid_transport_template);
2468 		return error;
2469 	}
2470 
2471 	return 0;
2472 }
2473 
2474 static void __exit leapraid_exit(void)
2475 {
2476 	pr_info("%s exiting\n", LEAPRAID_DRIVER_NAME);
2477 
2478 	leapraid_ctl_exit();
2479 	pci_unregister_driver(&leapraid_driver);
2480 	sas_release_transport(leapraid_transport_template);
2481 }
2482 
2483 module_init(leapraid_init);
2484 module_exit(leapraid_exit);
2485