xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_os.c (revision 672ae26c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Broadcom MPI3 Storage Controllers
4  *
5  * Copyright (C) 2017-2021 Broadcom Inc.
6  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7  *
8  */
9 
10 #include "mpi3mr.h"
11 
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
15 static int mrioc_ids;
16 static int warn_non_secure_ctlr;
17 
18 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
19 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
20 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
21 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
22 
23 /* Module parameters*/
24 int logging_level;
25 module_param(logging_level, int, 0);
26 MODULE_PARM_DESC(logging_level,
27 	" bits for enabling additional logging info (default=0)");
28 
29 /* Forward declarations*/
30 /**
31  * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
32  * @mrioc: Adapter instance reference
33  * @scmd: SCSI command reference
34  *
35  * Calculate the host tag based on block tag for a given scmd.
36  *
37  * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
38  */
39 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
40 	struct scsi_cmnd *scmd)
41 {
42 	struct scmd_priv *priv = NULL;
43 	u32 unique_tag;
44 	u16 host_tag, hw_queue;
45 
46 	unique_tag = blk_mq_unique_tag(scmd->request);
47 
48 	hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
49 	if (hw_queue >= mrioc->num_op_reply_q)
50 		return MPI3MR_HOSTTAG_INVALID;
51 	host_tag = blk_mq_unique_tag_to_tag(unique_tag);
52 
53 	if (WARN_ON(host_tag >= mrioc->max_host_ios))
54 		return MPI3MR_HOSTTAG_INVALID;
55 
56 	priv = scsi_cmd_priv(scmd);
57 	/*host_tag 0 is invalid hence incrementing by 1*/
58 	priv->host_tag = host_tag + 1;
59 	priv->scmd = scmd;
60 	priv->in_lld_scope = 1;
61 	priv->req_q_idx = hw_queue;
62 	priv->chain_idx = -1;
63 	return priv->host_tag;
64 }
65 
66 /**
67  * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
68  * @mrioc: Adapter instance reference
69  * @host_tag: Host tag
70  * @qidx: Operational queue index
71  *
72  * Identify the block tag from the host tag and queue index and
73  * retrieve associated scsi command using scsi_host_find_tag().
74  *
75  * Return: SCSI command reference or NULL.
76  */
77 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
78 	struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
79 {
80 	struct scsi_cmnd *scmd = NULL;
81 	struct scmd_priv *priv = NULL;
82 	u32 unique_tag = host_tag - 1;
83 
84 	if (WARN_ON(host_tag > mrioc->max_host_ios))
85 		goto out;
86 
87 	unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
88 
89 	scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
90 	if (scmd) {
91 		priv = scsi_cmd_priv(scmd);
92 		if (!priv->in_lld_scope)
93 			scmd = NULL;
94 	}
95 out:
96 	return scmd;
97 }
98 
99 /**
100  * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
101  * @mrioc: Adapter instance reference
102  * @scmd: SCSI command reference
103  *
104  * Invalidate the SCSI command private data to mark the command
105  * is not in LLD scope anymore.
106  *
107  * Return: Nothing.
108  */
109 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
110 	struct scsi_cmnd *scmd)
111 {
112 	struct scmd_priv *priv = NULL;
113 
114 	priv = scsi_cmd_priv(scmd);
115 
116 	if (WARN_ON(priv->in_lld_scope == 0))
117 		return;
118 	priv->host_tag = MPI3MR_HOSTTAG_INVALID;
119 	priv->req_q_idx = 0xFFFF;
120 	priv->scmd = NULL;
121 	priv->in_lld_scope = 0;
122 	if (priv->chain_idx >= 0) {
123 		clear_bit(priv->chain_idx, mrioc->chain_bitmap);
124 		priv->chain_idx = -1;
125 	}
126 }
127 
128 /**
129  * mpi3mr_process_op_reply_desc - reply descriptor handler
130  * @mrioc: Adapter instance reference
131  * @reply_desc: Operational reply descriptor
132  * @reply_dma: place holder for reply DMA address
133  * @qidx: Operational queue index
134  *
135  * Process the operational reply descriptor and identifies the
136  * descriptor type. Based on the descriptor map the MPI3 request
137  * status to a SCSI command status and calls scsi_done call
138  * back.
139  *
140  * Return: Nothing
141  */
142 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
143 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
144 {
145 	u16 reply_desc_type, host_tag = 0;
146 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
147 	u32 ioc_loginfo = 0;
148 	struct mpi3_status_reply_descriptor *status_desc = NULL;
149 	struct mpi3_address_reply_descriptor *addr_desc = NULL;
150 	struct mpi3_success_reply_descriptor *success_desc = NULL;
151 	struct mpi3_scsi_io_reply *scsi_reply = NULL;
152 	struct scsi_cmnd *scmd = NULL;
153 	struct scmd_priv *priv = NULL;
154 	u8 *sense_buf = NULL;
155 	u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
156 	u32 xfer_count = 0, sense_count = 0, resp_data = 0;
157 	u16 dev_handle = 0xFFFF;
158 	struct scsi_sense_hdr sshdr;
159 
160 	*reply_dma = 0;
161 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
162 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
163 	switch (reply_desc_type) {
164 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
165 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
166 		host_tag = le16_to_cpu(status_desc->host_tag);
167 		ioc_status = le16_to_cpu(status_desc->ioc_status);
168 		if (ioc_status &
169 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
170 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
171 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
172 		break;
173 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
174 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
175 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
176 		scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
177 		    *reply_dma);
178 		if (!scsi_reply) {
179 			panic("%s: scsi_reply is NULL, this shouldn't happen\n",
180 			    mrioc->name);
181 			goto out;
182 		}
183 		host_tag = le16_to_cpu(scsi_reply->host_tag);
184 		ioc_status = le16_to_cpu(scsi_reply->ioc_status);
185 		scsi_status = scsi_reply->scsi_status;
186 		scsi_state = scsi_reply->scsi_state;
187 		dev_handle = le16_to_cpu(scsi_reply->dev_handle);
188 		sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
189 		xfer_count = le32_to_cpu(scsi_reply->transfer_count);
190 		sense_count = le32_to_cpu(scsi_reply->sense_count);
191 		resp_data = le32_to_cpu(scsi_reply->response_data);
192 		sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
193 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
194 		if (ioc_status &
195 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
196 			ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
197 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
198 		if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
199 			panic("%s: Ran out of sense buffers\n", mrioc->name);
200 		break;
201 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
202 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
203 		host_tag = le16_to_cpu(success_desc->host_tag);
204 		break;
205 	default:
206 		break;
207 	}
208 	scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
209 	if (!scmd) {
210 		panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
211 		    mrioc->name, host_tag);
212 		goto out;
213 	}
214 	priv = scsi_cmd_priv(scmd);
215 	if (success_desc) {
216 		scmd->result = DID_OK << 16;
217 		goto out_success;
218 	}
219 	if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
220 	    xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
221 	    scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
222 	    scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
223 		ioc_status = MPI3_IOCSTATUS_SUCCESS;
224 
225 	if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
226 	    sense_buf) {
227 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
228 
229 		memcpy(scmd->sense_buffer, sense_buf, sz);
230 	}
231 
232 	switch (ioc_status) {
233 	case MPI3_IOCSTATUS_BUSY:
234 	case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
235 		scmd->result = SAM_STAT_BUSY;
236 		break;
237 	case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
238 		scmd->result = DID_NO_CONNECT << 16;
239 		break;
240 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
241 		scmd->result = DID_SOFT_ERROR << 16;
242 		break;
243 	case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
244 	case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
245 		scmd->result = DID_RESET << 16;
246 		break;
247 	case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
248 		if ((xfer_count == 0) || (scmd->underflow > xfer_count))
249 			scmd->result = DID_SOFT_ERROR << 16;
250 		else
251 			scmd->result = (DID_OK << 16) | scsi_status;
252 		break;
253 	case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
254 		scmd->result = (DID_OK << 16) | scsi_status;
255 		if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
256 			break;
257 		if (xfer_count < scmd->underflow) {
258 			if (scsi_status == SAM_STAT_BUSY)
259 				scmd->result = SAM_STAT_BUSY;
260 			else
261 				scmd->result = DID_SOFT_ERROR << 16;
262 		} else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
263 		    (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
264 			scmd->result = DID_SOFT_ERROR << 16;
265 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
266 			scmd->result = DID_RESET << 16;
267 		break;
268 	case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
269 		scsi_set_resid(scmd, 0);
270 		fallthrough;
271 	case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
272 	case MPI3_IOCSTATUS_SUCCESS:
273 		scmd->result = (DID_OK << 16) | scsi_status;
274 		if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
275 		    (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
276 			(sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
277 			scmd->result = DID_SOFT_ERROR << 16;
278 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
279 			scmd->result = DID_RESET << 16;
280 		break;
281 	case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
282 	case MPI3_IOCSTATUS_INVALID_FUNCTION:
283 	case MPI3_IOCSTATUS_INVALID_SGL:
284 	case MPI3_IOCSTATUS_INTERNAL_ERROR:
285 	case MPI3_IOCSTATUS_INVALID_FIELD:
286 	case MPI3_IOCSTATUS_INVALID_STATE:
287 	case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
288 	case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
289 	case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
290 	default:
291 		scmd->result = DID_SOFT_ERROR << 16;
292 		break;
293 	}
294 
295 	if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
296 	    (scmd->cmnd[0] != ATA_16)) {
297 		ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
298 		    scmd->result);
299 		scsi_print_command(scmd);
300 		ioc_info(mrioc,
301 		    "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
302 		    __func__, dev_handle, ioc_status, ioc_loginfo,
303 		    priv->req_q_idx + 1);
304 		ioc_info(mrioc,
305 		    " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
306 		    host_tag, scsi_state, scsi_status, xfer_count, resp_data);
307 		if (sense_buf) {
308 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
309 			ioc_info(mrioc,
310 			    "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
311 			    __func__, sense_count, sshdr.sense_key,
312 			    sshdr.asc, sshdr.ascq);
313 		}
314 	}
315 out_success:
316 	mpi3mr_clear_scmd_priv(mrioc, scmd);
317 	scsi_dma_unmap(scmd);
318 	scmd->scsi_done(scmd);
319 out:
320 	if (sense_buf)
321 		mpi3mr_repost_sense_buf(mrioc,
322 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
323 }
324 
325 /**
326  * mpi3mr_get_chain_idx - get free chain buffer index
327  * @mrioc: Adapter instance reference
328  *
329  * Try to get a free chain buffer index from the free pool.
330  *
331  * Return: -1 on failure or the free chain buffer index
332  */
333 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
334 {
335 	u8 retry_count = 5;
336 	int cmd_idx = -1;
337 
338 	do {
339 		spin_lock(&mrioc->chain_buf_lock);
340 		cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
341 		    mrioc->chain_buf_count);
342 		if (cmd_idx < mrioc->chain_buf_count) {
343 			set_bit(cmd_idx, mrioc->chain_bitmap);
344 			spin_unlock(&mrioc->chain_buf_lock);
345 			break;
346 		}
347 		spin_unlock(&mrioc->chain_buf_lock);
348 		cmd_idx = -1;
349 	} while (retry_count--);
350 	return cmd_idx;
351 }
352 
353 /**
354  * mpi3mr_prepare_sg_scmd - build scatter gather list
355  * @mrioc: Adapter instance reference
356  * @scmd: SCSI command reference
357  * @scsiio_req: MPI3 SCSI IO request
358  *
359  * This function maps SCSI command's data and protection SGEs to
360  * MPI request SGEs. If required additional 4K chain buffer is
361  * used to send the SGEs.
362  *
363  * Return: 0 on success, -ENOMEM on dma_map_sg failure
364  */
365 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
366 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
367 {
368 	dma_addr_t chain_dma;
369 	struct scatterlist *sg_scmd;
370 	void *sg_local, *chain;
371 	u32 chain_length;
372 	int sges_left, chain_idx;
373 	u32 sges_in_segment;
374 	u8 simple_sgl_flags;
375 	u8 simple_sgl_flags_last;
376 	u8 last_chain_sgl_flags;
377 	struct chain_element *chain_req;
378 	struct scmd_priv *priv = NULL;
379 
380 	priv = scsi_cmd_priv(scmd);
381 
382 	simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
383 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
384 	simple_sgl_flags_last = simple_sgl_flags |
385 	    MPI3_SGE_FLAGS_END_OF_LIST;
386 	last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
387 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
388 
389 	sg_local = &scsiio_req->sgl;
390 
391 	if (!scsiio_req->data_length) {
392 		mpi3mr_build_zero_len_sge(sg_local);
393 		return 0;
394 	}
395 
396 	sg_scmd = scsi_sglist(scmd);
397 	sges_left = scsi_dma_map(scmd);
398 
399 	if (sges_left < 0) {
400 		sdev_printk(KERN_ERR, scmd->device,
401 		    "scsi_dma_map failed: request for %d bytes!\n",
402 		    scsi_bufflen(scmd));
403 		return -ENOMEM;
404 	}
405 	if (sges_left > MPI3MR_SG_DEPTH) {
406 		sdev_printk(KERN_ERR, scmd->device,
407 		    "scsi_dma_map returned unsupported sge count %d!\n",
408 		    sges_left);
409 		return -ENOMEM;
410 	}
411 
412 	sges_in_segment = (mrioc->facts.op_req_sz -
413 	    offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
414 
415 	if (sges_left <= sges_in_segment)
416 		goto fill_in_last_segment;
417 
418 	/* fill in main message segment when there is a chain following */
419 	while (sges_in_segment > 1) {
420 		mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
421 		    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
422 		sg_scmd = sg_next(sg_scmd);
423 		sg_local += sizeof(struct mpi3_sge_common);
424 		sges_left--;
425 		sges_in_segment--;
426 	}
427 
428 	chain_idx = mpi3mr_get_chain_idx(mrioc);
429 	if (chain_idx < 0)
430 		return -1;
431 	chain_req = &mrioc->chain_sgl_list[chain_idx];
432 	priv->chain_idx = chain_idx;
433 
434 	chain = chain_req->addr;
435 	chain_dma = chain_req->dma_addr;
436 	sges_in_segment = sges_left;
437 	chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
438 
439 	mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
440 	    chain_length, chain_dma);
441 
442 	sg_local = chain;
443 
444 fill_in_last_segment:
445 	while (sges_left > 0) {
446 		if (sges_left == 1)
447 			mpi3mr_add_sg_single(sg_local,
448 			    simple_sgl_flags_last, sg_dma_len(sg_scmd),
449 			    sg_dma_address(sg_scmd));
450 		else
451 			mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
452 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
453 		sg_scmd = sg_next(sg_scmd);
454 		sg_local += sizeof(struct mpi3_sge_common);
455 		sges_left--;
456 	}
457 
458 	return 0;
459 }
460 
461 /**
462  * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
463  * @mrioc: Adapter instance reference
464  * @scmd: SCSI command reference
465  * @scsiio_req: MPI3 SCSI IO request
466  *
467  * This function calls mpi3mr_prepare_sg_scmd for constructing
468  * both data SGEs and protection information SGEs in the MPI
469  * format from the SCSI Command as appropriate .
470  *
471  * Return: return value of mpi3mr_prepare_sg_scmd.
472  */
473 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
474 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
475 {
476 	int ret;
477 
478 	ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
479 	if (ret)
480 		return ret;
481 
482 	return ret;
483 }
484 
485 /**
486  * mpi3mr_map_queues - Map queues callback handler
487  * @shost: SCSI host reference
488  *
489  * Call the blk_mq_pci_map_queues with from which operational
490  * queue the mapping has to be done
491  *
492  * Return: return of blk_mq_pci_map_queues
493  */
494 static int mpi3mr_map_queues(struct Scsi_Host *shost)
495 {
496 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
497 
498 	return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
499 	    mrioc->pdev, mrioc->op_reply_q_offset);
500 }
501 
502 /**
503  * mpi3mr_scan_start - Scan start callback handler
504  * @shost: SCSI host reference
505  *
506  * Issue port enable request asynchronously.
507  *
508  * Return: Nothing
509  */
510 static void mpi3mr_scan_start(struct Scsi_Host *shost)
511 {
512 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
513 
514 	mrioc->scan_started = 1;
515 	ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
516 	if (mpi3mr_issue_port_enable(mrioc, 1)) {
517 		ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
518 		mrioc->scan_started = 0;
519 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
520 	}
521 }
522 
523 /**
524  * mpi3mr_scan_finished - Scan finished callback handler
525  * @shost: SCSI host reference
526  * @time: Jiffies from the scan start
527  *
528  * Checks whether the port enable is completed or timedout or
529  * failed and set the scan status accordingly after taking any
530  * recovery if required.
531  *
532  * Return: 1 on scan finished or timed out, 0 for in progress
533  */
534 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
535 	unsigned long time)
536 {
537 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
538 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
539 
540 	if (time >= (pe_timeout * HZ)) {
541 		mrioc->init_cmds.is_waiting = 0;
542 		mrioc->init_cmds.callback = NULL;
543 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
544 		ioc_err(mrioc, "%s :port enable request timed out\n", __func__);
545 		mrioc->is_driver_loading = 0;
546 		mpi3mr_soft_reset_handler(mrioc,
547 		    MPI3MR_RESET_FROM_PE_TIMEOUT, 1);
548 	}
549 
550 	if (mrioc->scan_failed) {
551 		ioc_err(mrioc,
552 		    "%s :port enable failed with (ioc_status=0x%08x)\n",
553 		    __func__, mrioc->scan_failed);
554 		mrioc->is_driver_loading = 0;
555 		mrioc->stop_drv_processing = 1;
556 		return 1;
557 	}
558 
559 	if (mrioc->scan_started)
560 		return 0;
561 	ioc_info(mrioc, "%s :port enable: SUCCESS\n", __func__);
562 	mpi3mr_start_watchdog(mrioc);
563 	mrioc->is_driver_loading = 0;
564 
565 	return 1;
566 }
567 
568 /**
569  * mpi3mr_slave_destroy - Slave destroy callback handler
570  * @sdev: SCSI device reference
571  *
572  * Cleanup and free per device(lun) private data.
573  *
574  * Return: Nothing.
575  */
576 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
577 {
578 }
579 
580 /**
581  * mpi3mr_target_destroy - Target destroy callback handler
582  * @starget: SCSI target reference
583  *
584  * Cleanup and free per target private data.
585  *
586  * Return: Nothing.
587  */
588 static void mpi3mr_target_destroy(struct scsi_target *starget)
589 {
590 }
591 
592 /**
593  * mpi3mr_slave_configure - Slave configure callback handler
594  * @sdev: SCSI device reference
595  *
596  * Configure queue depth, max hardware sectors and virt boundary
597  * as required
598  *
599  * Return: 0 always.
600  */
601 static int mpi3mr_slave_configure(struct scsi_device *sdev)
602 {
603 	int retval = 0;
604 	return retval;
605 }
606 
607 /**
608  * mpi3mr_slave_alloc -Slave alloc callback handler
609  * @sdev: SCSI device reference
610  *
611  * Allocate per device(lun) private data and initialize it.
612  *
613  * Return: 0 on success -ENOMEM on memory allocation failure.
614  */
615 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
616 {
617 	int retval = 0;
618 	return retval;
619 }
620 
621 /**
622  * mpi3mr_target_alloc - Target alloc callback handler
623  * @starget: SCSI target reference
624  *
625  * Allocate per target private data and initialize it.
626  *
627  * Return: 0 on success -ENOMEM on memory allocation failure.
628  */
629 static int mpi3mr_target_alloc(struct scsi_target *starget)
630 {
631 	int retval = -ENODEV;
632 	return retval;
633 }
634 
635 /**
636  * mpi3mr_qcmd - I/O request despatcher
637  * @shost: SCSI Host reference
638  * @scmd: SCSI Command reference
639  *
640  * Issues the SCSI Command as an MPI3 request.
641  *
642  * Return: 0 on successful queueing of the request or if the
643  *         request is completed with failure.
644  *         SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
645  *         SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
646  */
647 static int mpi3mr_qcmd(struct Scsi_Host *shost,
648 	struct scsi_cmnd *scmd)
649 {
650 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
651 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
652 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
653 	struct scmd_priv *scmd_priv_data = NULL;
654 	struct mpi3_scsi_io_request *scsiio_req = NULL;
655 	struct op_req_qinfo *op_req_q = NULL;
656 	int retval = 0;
657 	u16 dev_handle;
658 	u16 host_tag;
659 	u32 scsiio_flags = 0;
660 	struct request *rq = scmd->request;
661 	int iprio_class;
662 
663 	sdev_priv_data = scmd->device->hostdata;
664 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
665 		scmd->result = DID_NO_CONNECT << 16;
666 		scmd->scsi_done(scmd);
667 		goto out;
668 	}
669 
670 	if (mrioc->stop_drv_processing) {
671 		scmd->result = DID_NO_CONNECT << 16;
672 		scmd->scsi_done(scmd);
673 		goto out;
674 	}
675 
676 	if (mrioc->reset_in_progress) {
677 		retval = SCSI_MLQUEUE_HOST_BUSY;
678 		goto out;
679 	}
680 
681 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
682 
683 	dev_handle = stgt_priv_data->dev_handle;
684 	if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
685 		scmd->result = DID_NO_CONNECT << 16;
686 		scmd->scsi_done(scmd);
687 		goto out;
688 	}
689 	if (stgt_priv_data->dev_removed) {
690 		scmd->result = DID_NO_CONNECT << 16;
691 		scmd->scsi_done(scmd);
692 		goto out;
693 	}
694 
695 	if (atomic_read(&stgt_priv_data->block_io)) {
696 		if (mrioc->stop_drv_processing) {
697 			scmd->result = DID_NO_CONNECT << 16;
698 			scmd->scsi_done(scmd);
699 			goto out;
700 		}
701 		retval = SCSI_MLQUEUE_DEVICE_BUSY;
702 		goto out;
703 	}
704 
705 	host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
706 	if (host_tag == MPI3MR_HOSTTAG_INVALID) {
707 		scmd->result = DID_ERROR << 16;
708 		scmd->scsi_done(scmd);
709 		goto out;
710 	}
711 
712 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
713 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
714 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
715 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
716 	else
717 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
718 
719 	scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
720 
721 	if (sdev_priv_data->ncq_prio_enable) {
722 		iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
723 		if (iprio_class == IOPRIO_CLASS_RT)
724 			scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
725 	}
726 
727 	if (scmd->cmd_len > 16)
728 		scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
729 
730 	scmd_priv_data = scsi_cmd_priv(scmd);
731 	memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
732 	scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
733 	scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
734 	scsiio_req->host_tag = cpu_to_le16(host_tag);
735 
736 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
737 	scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
738 	scsiio_req->dev_handle = cpu_to_le16(dev_handle);
739 	scsiio_req->flags = cpu_to_le32(scsiio_flags);
740 	int_to_scsilun(sdev_priv_data->lun_id,
741 	    (struct scsi_lun *)scsiio_req->lun);
742 
743 	if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
744 		mpi3mr_clear_scmd_priv(mrioc, scmd);
745 		retval = SCSI_MLQUEUE_HOST_BUSY;
746 		goto out;
747 	}
748 	op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
749 
750 	if (mpi3mr_op_request_post(mrioc, op_req_q,
751 	    scmd_priv_data->mpi3mr_scsiio_req)) {
752 		mpi3mr_clear_scmd_priv(mrioc, scmd);
753 		retval = SCSI_MLQUEUE_HOST_BUSY;
754 		goto out;
755 	}
756 
757 out:
758 	return retval;
759 }
760 
761 static struct scsi_host_template mpi3mr_driver_template = {
762 	.module				= THIS_MODULE,
763 	.name				= "MPI3 Storage Controller",
764 	.proc_name			= MPI3MR_DRIVER_NAME,
765 	.queuecommand			= mpi3mr_qcmd,
766 	.target_alloc			= mpi3mr_target_alloc,
767 	.slave_alloc			= mpi3mr_slave_alloc,
768 	.slave_configure		= mpi3mr_slave_configure,
769 	.target_destroy			= mpi3mr_target_destroy,
770 	.slave_destroy			= mpi3mr_slave_destroy,
771 	.scan_finished			= mpi3mr_scan_finished,
772 	.scan_start			= mpi3mr_scan_start,
773 	.map_queues			= mpi3mr_map_queues,
774 	.no_write_same			= 1,
775 	.can_queue			= 1,
776 	.this_id			= -1,
777 	.sg_tablesize			= MPI3MR_SG_DEPTH,
778 	/* max xfer supported is 1M (2K in 512 byte sized sectors)
779 	 */
780 	.max_sectors			= 2048,
781 	.cmd_per_lun			= MPI3MR_MAX_CMDS_LUN,
782 	.track_queue_depth		= 1,
783 	.cmd_size			= sizeof(struct scmd_priv),
784 };
785 
786 /**
787  * mpi3mr_init_drv_cmd - Initialize internal command tracker
788  * @cmdptr: Internal command tracker
789  * @host_tag: Host tag used for the specific command
790  *
791  * Initialize the internal command tracker structure with
792  * specified host tag.
793  *
794  * Return: Nothing.
795  */
796 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
797 	u16 host_tag)
798 {
799 	mutex_init(&cmdptr->mutex);
800 	cmdptr->reply = NULL;
801 	cmdptr->state = MPI3MR_CMD_NOTUSED;
802 	cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
803 	cmdptr->host_tag = host_tag;
804 }
805 
806 /**
807  * mpi3mr_probe - PCI probe callback
808  * @pdev: PCI device instance
809  * @id: PCI device ID details
810  *
811  * controller initialization routine. Checks the security status
812  * of the controller and if it is invalid or tampered return the
813  * probe without initializing the controller. Otherwise,
814  * allocate per adapter instance through shost_priv and
815  * initialize controller specific data structures, initializae
816  * the controller hardware, add shost to the SCSI subsystem.
817  *
818  * Return: 0 on success, non-zero on failure.
819  */
820 
821 static int
822 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
823 {
824 	struct mpi3mr_ioc *mrioc = NULL;
825 	struct Scsi_Host *shost = NULL;
826 	int retval = 0;
827 
828 	shost = scsi_host_alloc(&mpi3mr_driver_template,
829 	    sizeof(struct mpi3mr_ioc));
830 	if (!shost) {
831 		retval = -ENODEV;
832 		goto shost_failed;
833 	}
834 
835 	mrioc = shost_priv(shost);
836 	mrioc->id = mrioc_ids++;
837 	sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
838 	sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
839 	INIT_LIST_HEAD(&mrioc->list);
840 	spin_lock(&mrioc_list_lock);
841 	list_add_tail(&mrioc->list, &mrioc_list);
842 	spin_unlock(&mrioc_list_lock);
843 
844 	spin_lock_init(&mrioc->admin_req_lock);
845 	spin_lock_init(&mrioc->reply_free_queue_lock);
846 	spin_lock_init(&mrioc->sbq_lock);
847 	spin_lock_init(&mrioc->watchdog_lock);
848 	spin_lock_init(&mrioc->chain_buf_lock);
849 
850 	mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
851 
852 	if (pdev->revision)
853 		mrioc->enable_segqueue = true;
854 
855 	mrioc->logging_level = logging_level;
856 	mrioc->shost = shost;
857 	mrioc->pdev = pdev;
858 
859 	/* init shost parameters */
860 	shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
861 	shost->max_lun = -1;
862 	shost->unique_id = mrioc->id;
863 
864 	shost->max_channel = 1;
865 	shost->max_id = 0xFFFFFFFF;
866 
867 	mrioc->is_driver_loading = 1;
868 	if (mpi3mr_init_ioc(mrioc)) {
869 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
870 		    __FILE__, __LINE__, __func__);
871 		retval = -ENODEV;
872 		goto out_iocinit_failed;
873 	}
874 
875 	shost->nr_hw_queues = mrioc->num_op_reply_q;
876 	shost->can_queue = mrioc->max_host_ios;
877 	shost->sg_tablesize = MPI3MR_SG_DEPTH;
878 	shost->max_id = mrioc->facts.max_perids;
879 
880 	retval = scsi_add_host(shost, &pdev->dev);
881 	if (retval) {
882 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
883 		    __FILE__, __LINE__, __func__);
884 		goto addhost_failed;
885 	}
886 
887 	scsi_scan_host(shost);
888 	return retval;
889 
890 addhost_failed:
891 	mpi3mr_cleanup_ioc(mrioc);
892 out_iocinit_failed:
893 	spin_lock(&mrioc_list_lock);
894 	list_del(&mrioc->list);
895 	spin_unlock(&mrioc_list_lock);
896 	scsi_host_put(shost);
897 shost_failed:
898 	return retval;
899 }
900 
901 /**
902  * mpi3mr_remove - PCI remove callback
903  * @pdev: PCI device instance
904  *
905  * Free up all memory and resources associated with the
906  * controllerand target devices, unregister the shost.
907  *
908  * Return: Nothing.
909  */
910 static void mpi3mr_remove(struct pci_dev *pdev)
911 {
912 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
913 	struct mpi3mr_ioc *mrioc;
914 
915 	mrioc = shost_priv(shost);
916 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
917 		ssleep(1);
918 
919 	mrioc->stop_drv_processing = 1;
920 	scsi_remove_host(shost);
921 
922 	mpi3mr_cleanup_ioc(mrioc);
923 
924 	spin_lock(&mrioc_list_lock);
925 	list_del(&mrioc->list);
926 	spin_unlock(&mrioc_list_lock);
927 
928 	scsi_host_put(shost);
929 }
930 
931 /**
932  * mpi3mr_shutdown - PCI shutdown callback
933  * @pdev: PCI device instance
934  *
935  * Free up all memory and resources associated with the
936  * controller
937  *
938  * Return: Nothing.
939  */
940 static void mpi3mr_shutdown(struct pci_dev *pdev)
941 {
942 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
943 	struct mpi3mr_ioc *mrioc;
944 
945 	if (!shost)
946 		return;
947 
948 	mrioc = shost_priv(shost);
949 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
950 		ssleep(1);
951 
952 	mrioc->stop_drv_processing = 1;
953 	mpi3mr_cleanup_ioc(mrioc);
954 }
955 
956 static const struct pci_device_id mpi3mr_pci_id_table[] = {
957 	{
958 		PCI_DEVICE_SUB(PCI_VENDOR_ID_LSI_LOGIC, 0x00A5,
959 		    PCI_ANY_ID, PCI_ANY_ID)
960 	},
961 	{ 0 }
962 };
963 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
964 
965 static struct pci_driver mpi3mr_pci_driver = {
966 	.name = MPI3MR_DRIVER_NAME,
967 	.id_table = mpi3mr_pci_id_table,
968 	.probe = mpi3mr_probe,
969 	.remove = mpi3mr_remove,
970 	.shutdown = mpi3mr_shutdown,
971 };
972 
973 static int __init mpi3mr_init(void)
974 {
975 	int ret_val;
976 
977 	pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
978 	    MPI3MR_DRIVER_VERSION);
979 
980 	ret_val = pci_register_driver(&mpi3mr_pci_driver);
981 
982 	return ret_val;
983 }
984 
985 static void __exit mpi3mr_exit(void)
986 {
987 	if (warn_non_secure_ctlr)
988 		pr_warn(
989 		    "Unloading %s version %s while managing a non secure controller\n",
990 		    MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
991 	else
992 		pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
993 		    MPI3MR_DRIVER_VERSION);
994 
995 	pci_unregister_driver(&mpi3mr_pci_driver);
996 }
997 
998 module_init(mpi3mr_init);
999 module_exit(mpi3mr_exit);
1000