xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_app.c (revision 47ebd031)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Broadcom MPI3 Storage Controllers
4  *
5  * Copyright (C) 2017-2022 Broadcom Inc.
6  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7  *
8  */
9 
10 #include "mpi3mr.h"
11 #include <linux/bsg-lib.h>
12 #include <uapi/scsi/scsi_bsg_mpi3mr.h>
13 
14 /**
15  * mpi3mr_bsg_pel_abort - sends PEL abort request
16  * @mrioc: Adapter instance reference
17  *
18  * This function sends PEL abort request to the firmware through
19  * admin request queue.
20  *
21  * Return: 0 on success, -1 on failure
22  */
23 static int mpi3mr_bsg_pel_abort(struct mpi3mr_ioc *mrioc)
24 {
25 	struct mpi3_pel_req_action_abort pel_abort_req;
26 	struct mpi3_pel_reply *pel_reply;
27 	int retval = 0;
28 	u16 pe_log_status;
29 
30 	if (mrioc->reset_in_progress) {
31 		dprint_bsg_err(mrioc, "%s: reset in progress\n", __func__);
32 		return -1;
33 	}
34 	if (mrioc->stop_bsgs) {
35 		dprint_bsg_err(mrioc, "%s: bsgs are blocked\n", __func__);
36 		return -1;
37 	}
38 
39 	memset(&pel_abort_req, 0, sizeof(pel_abort_req));
40 	mutex_lock(&mrioc->pel_abort_cmd.mutex);
41 	if (mrioc->pel_abort_cmd.state & MPI3MR_CMD_PENDING) {
42 		dprint_bsg_err(mrioc, "%s: command is in use\n", __func__);
43 		mutex_unlock(&mrioc->pel_abort_cmd.mutex);
44 		return -1;
45 	}
46 	mrioc->pel_abort_cmd.state = MPI3MR_CMD_PENDING;
47 	mrioc->pel_abort_cmd.is_waiting = 1;
48 	mrioc->pel_abort_cmd.callback = NULL;
49 	pel_abort_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_ABORT);
50 	pel_abort_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
51 	pel_abort_req.action = MPI3_PEL_ACTION_ABORT;
52 	pel_abort_req.abort_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
53 
54 	mrioc->pel_abort_requested = 1;
55 	init_completion(&mrioc->pel_abort_cmd.done);
56 	retval = mpi3mr_admin_request_post(mrioc, &pel_abort_req,
57 	    sizeof(pel_abort_req), 0);
58 	if (retval) {
59 		retval = -1;
60 		dprint_bsg_err(mrioc, "%s: admin request post failed\n",
61 		    __func__);
62 		mrioc->pel_abort_requested = 0;
63 		goto out_unlock;
64 	}
65 
66 	wait_for_completion_timeout(&mrioc->pel_abort_cmd.done,
67 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
68 	if (!(mrioc->pel_abort_cmd.state & MPI3MR_CMD_COMPLETE)) {
69 		mrioc->pel_abort_cmd.is_waiting = 0;
70 		dprint_bsg_err(mrioc, "%s: command timedout\n", __func__);
71 		if (!(mrioc->pel_abort_cmd.state & MPI3MR_CMD_RESET))
72 			mpi3mr_soft_reset_handler(mrioc,
73 			    MPI3MR_RESET_FROM_PELABORT_TIMEOUT, 1);
74 		retval = -1;
75 		goto out_unlock;
76 	}
77 	if ((mrioc->pel_abort_cmd.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
78 	     != MPI3_IOCSTATUS_SUCCESS) {
79 		dprint_bsg_err(mrioc,
80 		    "%s: command failed, ioc_status(0x%04x) log_info(0x%08x)\n",
81 		    __func__, (mrioc->pel_abort_cmd.ioc_status &
82 		    MPI3_IOCSTATUS_STATUS_MASK),
83 		    mrioc->pel_abort_cmd.ioc_loginfo);
84 		retval = -1;
85 		goto out_unlock;
86 	}
87 	if (mrioc->pel_abort_cmd.state & MPI3MR_CMD_REPLY_VALID) {
88 		pel_reply = (struct mpi3_pel_reply *)mrioc->pel_abort_cmd.reply;
89 		pe_log_status = le16_to_cpu(pel_reply->pe_log_status);
90 		if (pe_log_status != MPI3_PEL_STATUS_SUCCESS) {
91 			dprint_bsg_err(mrioc,
92 			    "%s: command failed, pel_status(0x%04x)\n",
93 			    __func__, pe_log_status);
94 			retval = -1;
95 		}
96 	}
97 
98 out_unlock:
99 	mrioc->pel_abort_cmd.state = MPI3MR_CMD_NOTUSED;
100 	mutex_unlock(&mrioc->pel_abort_cmd.mutex);
101 	return retval;
102 }
103 /**
104  * mpi3mr_bsg_verify_adapter - verify adapter number is valid
105  * @ioc_number: Adapter number
106  *
107  * This function returns the adapter instance pointer of given
108  * adapter number. If adapter number does not match with the
109  * driver's adapter list, driver returns NULL.
110  *
111  * Return: adapter instance reference
112  */
113 static struct mpi3mr_ioc *mpi3mr_bsg_verify_adapter(int ioc_number)
114 {
115 	struct mpi3mr_ioc *mrioc = NULL;
116 
117 	spin_lock(&mrioc_list_lock);
118 	list_for_each_entry(mrioc, &mrioc_list, list) {
119 		if (mrioc->id == ioc_number) {
120 			spin_unlock(&mrioc_list_lock);
121 			return mrioc;
122 		}
123 	}
124 	spin_unlock(&mrioc_list_lock);
125 	return NULL;
126 }
127 
128 /**
129  * mpi3mr_enable_logdata - Handler for log data enable
130  * @mrioc: Adapter instance reference
131  * @job: BSG job reference
132  *
133  * This function enables log data caching in the driver if not
134  * already enabled and return the maximum number of log data
135  * entries that can be cached in the driver.
136  *
137  * Return: 0 on success and proper error codes on failure
138  */
139 static long mpi3mr_enable_logdata(struct mpi3mr_ioc *mrioc,
140 	struct bsg_job *job)
141 {
142 	struct mpi3mr_logdata_enable logdata_enable;
143 
144 	if (!mrioc->logdata_buf) {
145 		mrioc->logdata_entry_sz =
146 		    (mrioc->reply_sz - (sizeof(struct mpi3_event_notification_reply) - 4))
147 		    + MPI3MR_BSG_LOGDATA_ENTRY_HEADER_SZ;
148 		mrioc->logdata_buf_idx = 0;
149 		mrioc->logdata_buf = kcalloc(MPI3MR_BSG_LOGDATA_MAX_ENTRIES,
150 		    mrioc->logdata_entry_sz, GFP_KERNEL);
151 
152 		if (!mrioc->logdata_buf)
153 			return -ENOMEM;
154 	}
155 
156 	memset(&logdata_enable, 0, sizeof(logdata_enable));
157 	logdata_enable.max_entries =
158 	    MPI3MR_BSG_LOGDATA_MAX_ENTRIES;
159 	if (job->request_payload.payload_len >= sizeof(logdata_enable)) {
160 		sg_copy_from_buffer(job->request_payload.sg_list,
161 				    job->request_payload.sg_cnt,
162 				    &logdata_enable, sizeof(logdata_enable));
163 		return 0;
164 	}
165 
166 	return -EINVAL;
167 }
168 /**
169  * mpi3mr_get_logdata - Handler for get log data
170  * @mrioc: Adapter instance reference
171  * @job: BSG job pointer
172  * This function copies the log data entries to the user buffer
173  * when log caching is enabled in the driver.
174  *
175  * Return: 0 on success and proper error codes on failure
176  */
177 static long mpi3mr_get_logdata(struct mpi3mr_ioc *mrioc,
178 	struct bsg_job *job)
179 {
180 	u16 num_entries, sz, entry_sz = mrioc->logdata_entry_sz;
181 
182 	if ((!mrioc->logdata_buf) || (job->request_payload.payload_len < entry_sz))
183 		return -EINVAL;
184 
185 	num_entries = job->request_payload.payload_len / entry_sz;
186 	if (num_entries > MPI3MR_BSG_LOGDATA_MAX_ENTRIES)
187 		num_entries = MPI3MR_BSG_LOGDATA_MAX_ENTRIES;
188 	sz = num_entries * entry_sz;
189 
190 	if (job->request_payload.payload_len >= sz) {
191 		sg_copy_from_buffer(job->request_payload.sg_list,
192 				    job->request_payload.sg_cnt,
193 				    mrioc->logdata_buf, sz);
194 		return 0;
195 	}
196 	return -EINVAL;
197 }
198 
199 /**
200  * mpi3mr_bsg_pel_enable - Handler for PEL enable driver
201  * @mrioc: Adapter instance reference
202  * @job: BSG job pointer
203  *
204  * This function is the handler for PEL enable driver.
205  * Validates the application given class and locale and if
206  * requires aborts the existing PEL wait request and/or issues
207  * new PEL wait request to the firmware and returns.
208  *
209  * Return: 0 on success and proper error codes on failure.
210  */
211 static long mpi3mr_bsg_pel_enable(struct mpi3mr_ioc *mrioc,
212 				  struct bsg_job *job)
213 {
214 	long rval = -EINVAL;
215 	struct mpi3mr_bsg_out_pel_enable pel_enable;
216 	u8 issue_pel_wait;
217 	u8 tmp_class;
218 	u16 tmp_locale;
219 
220 	if (job->request_payload.payload_len != sizeof(pel_enable)) {
221 		dprint_bsg_err(mrioc, "%s: invalid size argument\n",
222 		    __func__);
223 		return rval;
224 	}
225 
226 	sg_copy_to_buffer(job->request_payload.sg_list,
227 			  job->request_payload.sg_cnt,
228 			  &pel_enable, sizeof(pel_enable));
229 
230 	if (pel_enable.pel_class > MPI3_PEL_CLASS_FAULT) {
231 		dprint_bsg_err(mrioc, "%s: out of range class %d sent\n",
232 			__func__, pel_enable.pel_class);
233 		rval = 0;
234 		goto out;
235 	}
236 	if (!mrioc->pel_enabled)
237 		issue_pel_wait = 1;
238 	else {
239 		if ((mrioc->pel_class <= pel_enable.pel_class) &&
240 		    !((mrioc->pel_locale & pel_enable.pel_locale) ^
241 		      pel_enable.pel_locale)) {
242 			issue_pel_wait = 0;
243 			rval = 0;
244 		} else {
245 			pel_enable.pel_locale |= mrioc->pel_locale;
246 
247 			if (mrioc->pel_class < pel_enable.pel_class)
248 				pel_enable.pel_class = mrioc->pel_class;
249 
250 			rval = mpi3mr_bsg_pel_abort(mrioc);
251 			if (rval) {
252 				dprint_bsg_err(mrioc,
253 				    "%s: pel_abort failed, status(%ld)\n",
254 				    __func__, rval);
255 				goto out;
256 			}
257 			issue_pel_wait = 1;
258 		}
259 	}
260 	if (issue_pel_wait) {
261 		tmp_class = mrioc->pel_class;
262 		tmp_locale = mrioc->pel_locale;
263 		mrioc->pel_class = pel_enable.pel_class;
264 		mrioc->pel_locale = pel_enable.pel_locale;
265 		mrioc->pel_enabled = 1;
266 		rval = mpi3mr_pel_get_seqnum_post(mrioc, NULL);
267 		if (rval) {
268 			mrioc->pel_class = tmp_class;
269 			mrioc->pel_locale = tmp_locale;
270 			mrioc->pel_enabled = 0;
271 			dprint_bsg_err(mrioc,
272 			    "%s: pel get sequence number failed, status(%ld)\n",
273 			    __func__, rval);
274 		}
275 	}
276 
277 out:
278 	return rval;
279 }
280 /**
281  * mpi3mr_get_all_tgt_info - Get all target information
282  * @mrioc: Adapter instance reference
283  * @job: BSG job reference
284  *
285  * This function copies the driver managed target devices device
286  * handle, persistent ID, bus ID and taret ID to the user
287  * provided buffer for the specific controller. This function
288  * also provides the number of devices managed by the driver for
289  * the specific controller.
290  *
291  * Return: 0 on success and proper error codes on failure
292  */
293 static long mpi3mr_get_all_tgt_info(struct mpi3mr_ioc *mrioc,
294 	struct bsg_job *job)
295 {
296 	u16 num_devices = 0, i = 0, size;
297 	unsigned long flags;
298 	struct mpi3mr_tgt_dev *tgtdev;
299 	struct mpi3mr_device_map_info *devmap_info = NULL;
300 	struct mpi3mr_all_tgt_info *alltgt_info = NULL;
301 	uint32_t min_entrylen = 0, kern_entrylen = 0, usr_entrylen = 0;
302 
303 	if (job->request_payload.payload_len < sizeof(u32)) {
304 		dprint_bsg_err(mrioc, "%s: invalid size argument\n",
305 		    __func__);
306 		return -EINVAL;
307 	}
308 
309 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
310 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
311 		num_devices++;
312 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
313 
314 	if ((job->request_payload.payload_len <= sizeof(u64)) ||
315 		list_empty(&mrioc->tgtdev_list)) {
316 		sg_copy_from_buffer(job->request_payload.sg_list,
317 				    job->request_payload.sg_cnt,
318 				    &num_devices, sizeof(num_devices));
319 		return 0;
320 	}
321 
322 	kern_entrylen = num_devices * sizeof(*devmap_info);
323 	size = sizeof(u64) + kern_entrylen;
324 	alltgt_info = kzalloc(size, GFP_KERNEL);
325 	if (!alltgt_info)
326 		return -ENOMEM;
327 
328 	devmap_info = alltgt_info->dmi;
329 	memset((u8 *)devmap_info, 0xFF, kern_entrylen);
330 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
331 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
332 		if (i < num_devices) {
333 			devmap_info[i].handle = tgtdev->dev_handle;
334 			devmap_info[i].perst_id = tgtdev->perst_id;
335 			if (tgtdev->host_exposed && tgtdev->starget) {
336 				devmap_info[i].target_id = tgtdev->starget->id;
337 				devmap_info[i].bus_id =
338 				    tgtdev->starget->channel;
339 			}
340 			i++;
341 		}
342 	}
343 	num_devices = i;
344 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
345 
346 	alltgt_info->num_devices = num_devices;
347 
348 	usr_entrylen = (job->request_payload.payload_len - sizeof(u64)) /
349 		sizeof(*devmap_info);
350 	usr_entrylen *= sizeof(*devmap_info);
351 	min_entrylen = min(usr_entrylen, kern_entrylen);
352 
353 	sg_copy_from_buffer(job->request_payload.sg_list,
354 			    job->request_payload.sg_cnt,
355 			    alltgt_info, (min_entrylen + sizeof(u64)));
356 	kfree(alltgt_info);
357 	return 0;
358 }
359 /**
360  * mpi3mr_get_change_count - Get topology change count
361  * @mrioc: Adapter instance reference
362  * @job: BSG job reference
363  *
364  * This function copies the toplogy change count provided by the
365  * driver in events and cached in the driver to the user
366  * provided buffer for the specific controller.
367  *
368  * Return: 0 on success and proper error codes on failure
369  */
370 static long mpi3mr_get_change_count(struct mpi3mr_ioc *mrioc,
371 	struct bsg_job *job)
372 {
373 	struct mpi3mr_change_count chgcnt;
374 
375 	memset(&chgcnt, 0, sizeof(chgcnt));
376 	chgcnt.change_count = mrioc->change_count;
377 	if (job->request_payload.payload_len >= sizeof(chgcnt)) {
378 		sg_copy_from_buffer(job->request_payload.sg_list,
379 				    job->request_payload.sg_cnt,
380 				    &chgcnt, sizeof(chgcnt));
381 		return 0;
382 	}
383 	return -EINVAL;
384 }
385 
386 /**
387  * mpi3mr_bsg_adp_reset - Issue controller reset
388  * @mrioc: Adapter instance reference
389  * @job: BSG job reference
390  *
391  * This function identifies the user provided reset type and
392  * issues approporiate reset to the controller and wait for that
393  * to complete and reinitialize the controller and then returns
394  *
395  * Return: 0 on success and proper error codes on failure
396  */
397 static long mpi3mr_bsg_adp_reset(struct mpi3mr_ioc *mrioc,
398 	struct bsg_job *job)
399 {
400 	long rval = -EINVAL;
401 	u8 save_snapdump;
402 	struct mpi3mr_bsg_adp_reset adpreset;
403 
404 	if (job->request_payload.payload_len !=
405 			sizeof(adpreset)) {
406 		dprint_bsg_err(mrioc, "%s: invalid size argument\n",
407 		    __func__);
408 		goto out;
409 	}
410 
411 	sg_copy_to_buffer(job->request_payload.sg_list,
412 			  job->request_payload.sg_cnt,
413 			  &adpreset, sizeof(adpreset));
414 
415 	switch (adpreset.reset_type) {
416 	case MPI3MR_BSG_ADPRESET_SOFT:
417 		save_snapdump = 0;
418 		break;
419 	case MPI3MR_BSG_ADPRESET_DIAG_FAULT:
420 		save_snapdump = 1;
421 		break;
422 	default:
423 		dprint_bsg_err(mrioc, "%s: unknown reset_type(%d)\n",
424 		    __func__, adpreset.reset_type);
425 		goto out;
426 	}
427 
428 	rval = mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_APP,
429 	    save_snapdump);
430 
431 	if (rval)
432 		dprint_bsg_err(mrioc,
433 		    "%s: reset handler returned error(%ld) for reset type %d\n",
434 		    __func__, rval, adpreset.reset_type);
435 out:
436 	return rval;
437 }
438 
439 /**
440  * mpi3mr_bsg_populate_adpinfo - Get adapter info command handler
441  * @mrioc: Adapter instance reference
442  * @job: BSG job reference
443  *
444  * This function provides adapter information for the given
445  * controller
446  *
447  * Return: 0 on success and proper error codes on failure
448  */
449 static long mpi3mr_bsg_populate_adpinfo(struct mpi3mr_ioc *mrioc,
450 	struct bsg_job *job)
451 {
452 	enum mpi3mr_iocstate ioc_state;
453 	struct mpi3mr_bsg_in_adpinfo adpinfo;
454 
455 	memset(&adpinfo, 0, sizeof(adpinfo));
456 	adpinfo.adp_type = MPI3MR_BSG_ADPTYPE_AVGFAMILY;
457 	adpinfo.pci_dev_id = mrioc->pdev->device;
458 	adpinfo.pci_dev_hw_rev = mrioc->pdev->revision;
459 	adpinfo.pci_subsys_dev_id = mrioc->pdev->subsystem_device;
460 	adpinfo.pci_subsys_ven_id = mrioc->pdev->subsystem_vendor;
461 	adpinfo.pci_bus = mrioc->pdev->bus->number;
462 	adpinfo.pci_dev = PCI_SLOT(mrioc->pdev->devfn);
463 	adpinfo.pci_func = PCI_FUNC(mrioc->pdev->devfn);
464 	adpinfo.pci_seg_id = pci_domain_nr(mrioc->pdev->bus);
465 	adpinfo.app_intfc_ver = MPI3MR_IOCTL_VERSION;
466 
467 	ioc_state = mpi3mr_get_iocstate(mrioc);
468 	if (ioc_state == MRIOC_STATE_UNRECOVERABLE)
469 		adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_UNRECOVERABLE;
470 	else if ((mrioc->reset_in_progress) || (mrioc->stop_bsgs))
471 		adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_IN_RESET;
472 	else if (ioc_state == MRIOC_STATE_FAULT)
473 		adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_FAULT;
474 	else
475 		adpinfo.adp_state = MPI3MR_BSG_ADPSTATE_OPERATIONAL;
476 
477 	memcpy((u8 *)&adpinfo.driver_info, (u8 *)&mrioc->driver_info,
478 	    sizeof(adpinfo.driver_info));
479 
480 	if (job->request_payload.payload_len >= sizeof(adpinfo)) {
481 		sg_copy_from_buffer(job->request_payload.sg_list,
482 				    job->request_payload.sg_cnt,
483 				    &adpinfo, sizeof(adpinfo));
484 		return 0;
485 	}
486 	return -EINVAL;
487 }
488 
489 /**
490  * mpi3mr_bsg_process_drv_cmds - Driver Command handler
491  * @job: BSG job reference
492  *
493  * This function is the top level handler for driver commands,
494  * this does basic validation of the buffer and identifies the
495  * opcode and switches to correct sub handler.
496  *
497  * Return: 0 on success and proper error codes on failure
498  */
499 static long mpi3mr_bsg_process_drv_cmds(struct bsg_job *job)
500 {
501 	long rval = -EINVAL;
502 	struct mpi3mr_ioc *mrioc = NULL;
503 	struct mpi3mr_bsg_packet *bsg_req = NULL;
504 	struct mpi3mr_bsg_drv_cmd *drvrcmd = NULL;
505 
506 	bsg_req = job->request;
507 	drvrcmd = &bsg_req->cmd.drvrcmd;
508 
509 	mrioc = mpi3mr_bsg_verify_adapter(drvrcmd->mrioc_id);
510 	if (!mrioc)
511 		return -ENODEV;
512 
513 	if (drvrcmd->opcode == MPI3MR_DRVBSG_OPCODE_ADPINFO) {
514 		rval = mpi3mr_bsg_populate_adpinfo(mrioc, job);
515 		return rval;
516 	}
517 
518 	if (mutex_lock_interruptible(&mrioc->bsg_cmds.mutex))
519 		return -ERESTARTSYS;
520 
521 	switch (drvrcmd->opcode) {
522 	case MPI3MR_DRVBSG_OPCODE_ADPRESET:
523 		rval = mpi3mr_bsg_adp_reset(mrioc, job);
524 		break;
525 	case MPI3MR_DRVBSG_OPCODE_ALLTGTDEVINFO:
526 		rval = mpi3mr_get_all_tgt_info(mrioc, job);
527 		break;
528 	case MPI3MR_DRVBSG_OPCODE_GETCHGCNT:
529 		rval = mpi3mr_get_change_count(mrioc, job);
530 		break;
531 	case MPI3MR_DRVBSG_OPCODE_LOGDATAENABLE:
532 		rval = mpi3mr_enable_logdata(mrioc, job);
533 		break;
534 	case MPI3MR_DRVBSG_OPCODE_GETLOGDATA:
535 		rval = mpi3mr_get_logdata(mrioc, job);
536 		break;
537 	case MPI3MR_DRVBSG_OPCODE_PELENABLE:
538 		rval = mpi3mr_bsg_pel_enable(mrioc, job);
539 		break;
540 	case MPI3MR_DRVBSG_OPCODE_UNKNOWN:
541 	default:
542 		pr_err("%s: unsupported driver command opcode %d\n",
543 		    MPI3MR_DRIVER_NAME, drvrcmd->opcode);
544 		break;
545 	}
546 	mutex_unlock(&mrioc->bsg_cmds.mutex);
547 	return rval;
548 }
549 
550 /**
551  * mpi3mr_bsg_build_sgl - SGL construction for MPI commands
552  * @mpi_req: MPI request
553  * @sgl_offset: offset to start sgl in the MPI request
554  * @drv_bufs: DMA address of the buffers to be placed in sgl
555  * @bufcnt: Number of DMA buffers
556  * @is_rmc: Does the buffer list has management command buffer
557  * @is_rmr: Does the buffer list has management response buffer
558  * @num_datasges: Number of data buffers in the list
559  *
560  * This function places the DMA address of the given buffers in
561  * proper format as SGEs in the given MPI request.
562  *
563  * Return: Nothing
564  */
565 static void mpi3mr_bsg_build_sgl(u8 *mpi_req, uint32_t sgl_offset,
566 	struct mpi3mr_buf_map *drv_bufs, u8 bufcnt, u8 is_rmc,
567 	u8 is_rmr, u8 num_datasges)
568 {
569 	u8 *sgl = (mpi_req + sgl_offset), count = 0;
570 	struct mpi3_mgmt_passthrough_request *rmgmt_req =
571 	    (struct mpi3_mgmt_passthrough_request *)mpi_req;
572 	struct mpi3mr_buf_map *drv_buf_iter = drv_bufs;
573 	u8 sgl_flags, sgl_flags_last;
574 
575 	sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
576 		MPI3_SGE_FLAGS_DLAS_SYSTEM | MPI3_SGE_FLAGS_END_OF_BUFFER;
577 	sgl_flags_last = sgl_flags | MPI3_SGE_FLAGS_END_OF_LIST;
578 
579 	if (is_rmc) {
580 		mpi3mr_add_sg_single(&rmgmt_req->command_sgl,
581 		    sgl_flags_last, drv_buf_iter->kern_buf_len,
582 		    drv_buf_iter->kern_buf_dma);
583 		sgl = (u8 *)drv_buf_iter->kern_buf + drv_buf_iter->bsg_buf_len;
584 		drv_buf_iter++;
585 		count++;
586 		if (is_rmr) {
587 			mpi3mr_add_sg_single(&rmgmt_req->response_sgl,
588 			    sgl_flags_last, drv_buf_iter->kern_buf_len,
589 			    drv_buf_iter->kern_buf_dma);
590 			drv_buf_iter++;
591 			count++;
592 		} else
593 			mpi3mr_build_zero_len_sge(
594 			    &rmgmt_req->response_sgl);
595 	}
596 	if (!num_datasges) {
597 		mpi3mr_build_zero_len_sge(sgl);
598 		return;
599 	}
600 	for (; count < bufcnt; count++, drv_buf_iter++) {
601 		if (drv_buf_iter->data_dir == DMA_NONE)
602 			continue;
603 		if (num_datasges == 1 || !is_rmc)
604 			mpi3mr_add_sg_single(sgl, sgl_flags_last,
605 			    drv_buf_iter->kern_buf_len, drv_buf_iter->kern_buf_dma);
606 		else
607 			mpi3mr_add_sg_single(sgl, sgl_flags,
608 			    drv_buf_iter->kern_buf_len, drv_buf_iter->kern_buf_dma);
609 		sgl += sizeof(struct mpi3_sge_common);
610 		num_datasges--;
611 	}
612 }
613 
614 /**
615  * mpi3mr_get_nvme_data_fmt - returns the NVMe data format
616  * @nvme_encap_request: NVMe encapsulated MPI request
617  *
618  * This function returns the type of the data format specified
619  * in user provided NVMe command in NVMe encapsulated request.
620  *
621  * Return: Data format of the NVMe command (PRP/SGL etc)
622  */
623 static unsigned int mpi3mr_get_nvme_data_fmt(
624 	struct mpi3_nvme_encapsulated_request *nvme_encap_request)
625 {
626 	u8 format = 0;
627 
628 	format = ((nvme_encap_request->command[0] & 0xc000) >> 14);
629 	return format;
630 
631 }
632 
633 /**
634  * mpi3mr_build_nvme_sgl - SGL constructor for NVME
635  *				   encapsulated request
636  * @mrioc: Adapter instance reference
637  * @nvme_encap_request: NVMe encapsulated MPI request
638  * @drv_bufs: DMA address of the buffers to be placed in sgl
639  * @bufcnt: Number of DMA buffers
640  *
641  * This function places the DMA address of the given buffers in
642  * proper format as SGEs in the given NVMe encapsulated request.
643  *
644  * Return: 0 on success, -1 on failure
645  */
646 static int mpi3mr_build_nvme_sgl(struct mpi3mr_ioc *mrioc,
647 	struct mpi3_nvme_encapsulated_request *nvme_encap_request,
648 	struct mpi3mr_buf_map *drv_bufs, u8 bufcnt)
649 {
650 	struct mpi3mr_nvme_pt_sge *nvme_sgl;
651 	u64 sgl_ptr;
652 	u8 count;
653 	size_t length = 0;
654 	struct mpi3mr_buf_map *drv_buf_iter = drv_bufs;
655 	u64 sgemod_mask = ((u64)((mrioc->facts.sge_mod_mask) <<
656 			    mrioc->facts.sge_mod_shift) << 32);
657 	u64 sgemod_val = ((u64)(mrioc->facts.sge_mod_value) <<
658 			  mrioc->facts.sge_mod_shift) << 32;
659 
660 	/*
661 	 * Not all commands require a data transfer. If no data, just return
662 	 * without constructing any sgl.
663 	 */
664 	for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
665 		if (drv_buf_iter->data_dir == DMA_NONE)
666 			continue;
667 		sgl_ptr = (u64)drv_buf_iter->kern_buf_dma;
668 		length = drv_buf_iter->kern_buf_len;
669 		break;
670 	}
671 	if (!length)
672 		return 0;
673 
674 	if (sgl_ptr & sgemod_mask) {
675 		dprint_bsg_err(mrioc,
676 		    "%s: SGL address collides with SGE modifier\n",
677 		    __func__);
678 		return -1;
679 	}
680 
681 	sgl_ptr &= ~sgemod_mask;
682 	sgl_ptr |= sgemod_val;
683 	nvme_sgl = (struct mpi3mr_nvme_pt_sge *)
684 	    ((u8 *)(nvme_encap_request->command) + MPI3MR_NVME_CMD_SGL_OFFSET);
685 	memset(nvme_sgl, 0, sizeof(struct mpi3mr_nvme_pt_sge));
686 	nvme_sgl->base_addr = sgl_ptr;
687 	nvme_sgl->length = length;
688 	return 0;
689 }
690 
691 /**
692  * mpi3mr_build_nvme_prp - PRP constructor for NVME
693  *			       encapsulated request
694  * @mrioc: Adapter instance reference
695  * @nvme_encap_request: NVMe encapsulated MPI request
696  * @drv_bufs: DMA address of the buffers to be placed in SGL
697  * @bufcnt: Number of DMA buffers
698  *
699  * This function places the DMA address of the given buffers in
700  * proper format as PRP entries in the given NVMe encapsulated
701  * request.
702  *
703  * Return: 0 on success, -1 on failure
704  */
705 static int mpi3mr_build_nvme_prp(struct mpi3mr_ioc *mrioc,
706 	struct mpi3_nvme_encapsulated_request *nvme_encap_request,
707 	struct mpi3mr_buf_map *drv_bufs, u8 bufcnt)
708 {
709 	int prp_size = MPI3MR_NVME_PRP_SIZE;
710 	__le64 *prp_entry, *prp1_entry, *prp2_entry;
711 	__le64 *prp_page;
712 	dma_addr_t prp_entry_dma, prp_page_dma, dma_addr;
713 	u32 offset, entry_len, dev_pgsz;
714 	u32 page_mask_result, page_mask;
715 	size_t length = 0;
716 	u8 count;
717 	struct mpi3mr_buf_map *drv_buf_iter = drv_bufs;
718 	u64 sgemod_mask = ((u64)((mrioc->facts.sge_mod_mask) <<
719 			    mrioc->facts.sge_mod_shift) << 32);
720 	u64 sgemod_val = ((u64)(mrioc->facts.sge_mod_value) <<
721 			  mrioc->facts.sge_mod_shift) << 32;
722 	u16 dev_handle = nvme_encap_request->dev_handle;
723 	struct mpi3mr_tgt_dev *tgtdev;
724 
725 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
726 	if (!tgtdev) {
727 		dprint_bsg_err(mrioc, "%s: invalid device handle 0x%04x\n",
728 			__func__, dev_handle);
729 		return -1;
730 	}
731 
732 	if (tgtdev->dev_spec.pcie_inf.pgsz == 0) {
733 		dprint_bsg_err(mrioc,
734 		    "%s: NVMe device page size is zero for handle 0x%04x\n",
735 		    __func__, dev_handle);
736 		mpi3mr_tgtdev_put(tgtdev);
737 		return -1;
738 	}
739 
740 	dev_pgsz = 1 << (tgtdev->dev_spec.pcie_inf.pgsz);
741 	mpi3mr_tgtdev_put(tgtdev);
742 
743 	/*
744 	 * Not all commands require a data transfer. If no data, just return
745 	 * without constructing any PRP.
746 	 */
747 	for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
748 		if (drv_buf_iter->data_dir == DMA_NONE)
749 			continue;
750 		dma_addr = drv_buf_iter->kern_buf_dma;
751 		length = drv_buf_iter->kern_buf_len;
752 		break;
753 	}
754 
755 	if (!length)
756 		return 0;
757 
758 	mrioc->prp_sz = 0;
759 	mrioc->prp_list_virt = dma_alloc_coherent(&mrioc->pdev->dev,
760 	    dev_pgsz, &mrioc->prp_list_dma, GFP_KERNEL);
761 
762 	if (!mrioc->prp_list_virt)
763 		return -1;
764 	mrioc->prp_sz = dev_pgsz;
765 
766 	/*
767 	 * Set pointers to PRP1 and PRP2, which are in the NVMe command.
768 	 * PRP1 is located at a 24 byte offset from the start of the NVMe
769 	 * command.  Then set the current PRP entry pointer to PRP1.
770 	 */
771 	prp1_entry = (__le64 *)((u8 *)(nvme_encap_request->command) +
772 	    MPI3MR_NVME_CMD_PRP1_OFFSET);
773 	prp2_entry = (__le64 *)((u8 *)(nvme_encap_request->command) +
774 	    MPI3MR_NVME_CMD_PRP2_OFFSET);
775 	prp_entry = prp1_entry;
776 	/*
777 	 * For the PRP entries, use the specially allocated buffer of
778 	 * contiguous memory.
779 	 */
780 	prp_page = (__le64 *)mrioc->prp_list_virt;
781 	prp_page_dma = mrioc->prp_list_dma;
782 
783 	/*
784 	 * Check if we are within 1 entry of a page boundary we don't
785 	 * want our first entry to be a PRP List entry.
786 	 */
787 	page_mask = dev_pgsz - 1;
788 	page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
789 	if (!page_mask_result) {
790 		dprint_bsg_err(mrioc, "%s: PRP page is not page aligned\n",
791 		    __func__);
792 		goto err_out;
793 	}
794 
795 	/*
796 	 * Set PRP physical pointer, which initially points to the current PRP
797 	 * DMA memory page.
798 	 */
799 	prp_entry_dma = prp_page_dma;
800 
801 
802 	/* Loop while the length is not zero. */
803 	while (length) {
804 		page_mask_result = (prp_entry_dma + prp_size) & page_mask;
805 		if (!page_mask_result && (length >  dev_pgsz)) {
806 			dprint_bsg_err(mrioc,
807 			    "%s: single PRP page is not sufficient\n",
808 			    __func__);
809 			goto err_out;
810 		}
811 
812 		/* Need to handle if entry will be part of a page. */
813 		offset = dma_addr & page_mask;
814 		entry_len = dev_pgsz - offset;
815 
816 		if (prp_entry == prp1_entry) {
817 			/*
818 			 * Must fill in the first PRP pointer (PRP1) before
819 			 * moving on.
820 			 */
821 			*prp1_entry = cpu_to_le64(dma_addr);
822 			if (*prp1_entry & sgemod_mask) {
823 				dprint_bsg_err(mrioc,
824 				    "%s: PRP1 address collides with SGE modifier\n",
825 				    __func__);
826 				goto err_out;
827 			}
828 			*prp1_entry &= ~sgemod_mask;
829 			*prp1_entry |= sgemod_val;
830 
831 			/*
832 			 * Now point to the second PRP entry within the
833 			 * command (PRP2).
834 			 */
835 			prp_entry = prp2_entry;
836 		} else if (prp_entry == prp2_entry) {
837 			/*
838 			 * Should the PRP2 entry be a PRP List pointer or just
839 			 * a regular PRP pointer?  If there is more than one
840 			 * more page of data, must use a PRP List pointer.
841 			 */
842 			if (length > dev_pgsz) {
843 				/*
844 				 * PRP2 will contain a PRP List pointer because
845 				 * more PRP's are needed with this command. The
846 				 * list will start at the beginning of the
847 				 * contiguous buffer.
848 				 */
849 				*prp2_entry = cpu_to_le64(prp_entry_dma);
850 				if (*prp2_entry & sgemod_mask) {
851 					dprint_bsg_err(mrioc,
852 					    "%s: PRP list address collides with SGE modifier\n",
853 					    __func__);
854 					goto err_out;
855 				}
856 				*prp2_entry &= ~sgemod_mask;
857 				*prp2_entry |= sgemod_val;
858 
859 				/*
860 				 * The next PRP Entry will be the start of the
861 				 * first PRP List.
862 				 */
863 				prp_entry = prp_page;
864 				continue;
865 			} else {
866 				/*
867 				 * After this, the PRP Entries are complete.
868 				 * This command uses 2 PRP's and no PRP list.
869 				 */
870 				*prp2_entry = cpu_to_le64(dma_addr);
871 				if (*prp2_entry & sgemod_mask) {
872 					dprint_bsg_err(mrioc,
873 					    "%s: PRP2 collides with SGE modifier\n",
874 					    __func__);
875 					goto err_out;
876 				}
877 				*prp2_entry &= ~sgemod_mask;
878 				*prp2_entry |= sgemod_val;
879 			}
880 		} else {
881 			/*
882 			 * Put entry in list and bump the addresses.
883 			 *
884 			 * After PRP1 and PRP2 are filled in, this will fill in
885 			 * all remaining PRP entries in a PRP List, one per
886 			 * each time through the loop.
887 			 */
888 			*prp_entry = cpu_to_le64(dma_addr);
889 			if (*prp_entry & sgemod_mask) {
890 				dprint_bsg_err(mrioc,
891 				    "%s: PRP address collides with SGE modifier\n",
892 				    __func__);
893 				goto err_out;
894 			}
895 			*prp_entry &= ~sgemod_mask;
896 			*prp_entry |= sgemod_val;
897 			prp_entry++;
898 			prp_entry_dma += prp_size;
899 		}
900 
901 		/*
902 		 * Bump the phys address of the command's data buffer by the
903 		 * entry_len.
904 		 */
905 		dma_addr += entry_len;
906 
907 		/* decrement length accounting for last partial page. */
908 		if (entry_len > length)
909 			length = 0;
910 		else
911 			length -= entry_len;
912 	}
913 	return 0;
914 err_out:
915 	if (mrioc->prp_list_virt) {
916 		dma_free_coherent(&mrioc->pdev->dev, mrioc->prp_sz,
917 		    mrioc->prp_list_virt, mrioc->prp_list_dma);
918 		mrioc->prp_list_virt = NULL;
919 	}
920 	return -1;
921 }
922 /**
923  * mpi3mr_bsg_process_mpt_cmds - MPI Pass through BSG handler
924  * @job: BSG job reference
925  *
926  * This function is the top level handler for MPI Pass through
927  * command, this does basic validation of the input data buffers,
928  * identifies the given buffer types and MPI command, allocates
929  * DMAable memory for user given buffers, construstcs SGL
930  * properly and passes the command to the firmware.
931  *
932  * Once the MPI command is completed the driver copies the data
933  * if any and reply, sense information to user provided buffers.
934  * If the command is timed out then issues controller reset
935  * prior to returning.
936  *
937  * Return: 0 on success and proper error codes on failure
938  */
939 
940 static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job, unsigned int *reply_payload_rcv_len)
941 {
942 	long rval = -EINVAL;
943 
944 	struct mpi3mr_ioc *mrioc = NULL;
945 	u8 *mpi_req = NULL, *sense_buff_k = NULL;
946 	u8 mpi_msg_size = 0;
947 	struct mpi3mr_bsg_packet *bsg_req = NULL;
948 	struct mpi3mr_bsg_mptcmd *karg;
949 	struct mpi3mr_buf_entry *buf_entries = NULL;
950 	struct mpi3mr_buf_map *drv_bufs = NULL, *drv_buf_iter = NULL;
951 	u8 count, bufcnt = 0, is_rmcb = 0, is_rmrb = 0, din_cnt = 0, dout_cnt = 0;
952 	u8 invalid_be = 0, erb_offset = 0xFF, mpirep_offset = 0xFF, sg_entries = 0;
953 	u8 block_io = 0, resp_code = 0, nvme_fmt = 0;
954 	struct mpi3_request_header *mpi_header = NULL;
955 	struct mpi3_status_reply_descriptor *status_desc;
956 	struct mpi3_scsi_task_mgmt_request *tm_req;
957 	u32 erbsz = MPI3MR_SENSE_BUF_SZ, tmplen;
958 	u16 dev_handle;
959 	struct mpi3mr_tgt_dev *tgtdev;
960 	struct mpi3mr_stgt_priv_data *stgt_priv = NULL;
961 	struct mpi3mr_bsg_in_reply_buf *bsg_reply_buf = NULL;
962 	u32 din_size = 0, dout_size = 0;
963 	u8 *din_buf = NULL, *dout_buf = NULL;
964 	u8 *sgl_iter = NULL, *sgl_din_iter = NULL, *sgl_dout_iter = NULL;
965 
966 	bsg_req = job->request;
967 	karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd;
968 
969 	mrioc = mpi3mr_bsg_verify_adapter(karg->mrioc_id);
970 	if (!mrioc)
971 		return -ENODEV;
972 
973 	if (karg->timeout < MPI3MR_APP_DEFAULT_TIMEOUT)
974 		karg->timeout = MPI3MR_APP_DEFAULT_TIMEOUT;
975 
976 	mpi_req = kzalloc(MPI3MR_ADMIN_REQ_FRAME_SZ, GFP_KERNEL);
977 	if (!mpi_req)
978 		return -ENOMEM;
979 	mpi_header = (struct mpi3_request_header *)mpi_req;
980 
981 	bufcnt = karg->buf_entry_list.num_of_entries;
982 	drv_bufs = kzalloc((sizeof(*drv_bufs) * bufcnt), GFP_KERNEL);
983 	if (!drv_bufs) {
984 		rval = -ENOMEM;
985 		goto out;
986 	}
987 
988 	dout_buf = kzalloc(job->request_payload.payload_len,
989 				      GFP_KERNEL);
990 	if (!dout_buf) {
991 		rval = -ENOMEM;
992 		goto out;
993 	}
994 
995 	din_buf = kzalloc(job->reply_payload.payload_len,
996 				     GFP_KERNEL);
997 	if (!din_buf) {
998 		rval = -ENOMEM;
999 		goto out;
1000 	}
1001 
1002 	sg_copy_to_buffer(job->request_payload.sg_list,
1003 			  job->request_payload.sg_cnt,
1004 			  dout_buf, job->request_payload.payload_len);
1005 
1006 	buf_entries = karg->buf_entry_list.buf_entry;
1007 	sgl_din_iter = din_buf;
1008 	sgl_dout_iter = dout_buf;
1009 	drv_buf_iter = drv_bufs;
1010 
1011 	for (count = 0; count < bufcnt; count++, buf_entries++, drv_buf_iter++) {
1012 
1013 		if (sgl_dout_iter > (dout_buf + job->request_payload.payload_len)) {
1014 			dprint_bsg_err(mrioc, "%s: data_out buffer length mismatch\n",
1015 				__func__);
1016 			rval = -EINVAL;
1017 			goto out;
1018 		}
1019 		if (sgl_din_iter > (din_buf + job->reply_payload.payload_len)) {
1020 			dprint_bsg_err(mrioc, "%s: data_in buffer length mismatch\n",
1021 				__func__);
1022 			rval = -EINVAL;
1023 			goto out;
1024 		}
1025 
1026 		switch (buf_entries->buf_type) {
1027 		case MPI3MR_BSG_BUFTYPE_RAIDMGMT_CMD:
1028 			sgl_iter = sgl_dout_iter;
1029 			sgl_dout_iter += buf_entries->buf_len;
1030 			drv_buf_iter->data_dir = DMA_TO_DEVICE;
1031 			is_rmcb = 1;
1032 			if (count != 0)
1033 				invalid_be = 1;
1034 			break;
1035 		case MPI3MR_BSG_BUFTYPE_RAIDMGMT_RESP:
1036 			sgl_iter = sgl_din_iter;
1037 			sgl_din_iter += buf_entries->buf_len;
1038 			drv_buf_iter->data_dir = DMA_FROM_DEVICE;
1039 			is_rmrb = 1;
1040 			if (count != 1 || !is_rmcb)
1041 				invalid_be = 1;
1042 			break;
1043 		case MPI3MR_BSG_BUFTYPE_DATA_IN:
1044 			sgl_iter = sgl_din_iter;
1045 			sgl_din_iter += buf_entries->buf_len;
1046 			drv_buf_iter->data_dir = DMA_FROM_DEVICE;
1047 			din_cnt++;
1048 			din_size += drv_buf_iter->bsg_buf_len;
1049 			if ((din_cnt > 1) && !is_rmcb)
1050 				invalid_be = 1;
1051 			break;
1052 		case MPI3MR_BSG_BUFTYPE_DATA_OUT:
1053 			sgl_iter = sgl_dout_iter;
1054 			sgl_dout_iter += buf_entries->buf_len;
1055 			drv_buf_iter->data_dir = DMA_TO_DEVICE;
1056 			dout_cnt++;
1057 			dout_size += drv_buf_iter->bsg_buf_len;
1058 			if ((dout_cnt > 1) && !is_rmcb)
1059 				invalid_be = 1;
1060 			break;
1061 		case MPI3MR_BSG_BUFTYPE_MPI_REPLY:
1062 			sgl_iter = sgl_din_iter;
1063 			sgl_din_iter += buf_entries->buf_len;
1064 			drv_buf_iter->data_dir = DMA_NONE;
1065 			mpirep_offset = count;
1066 			break;
1067 		case MPI3MR_BSG_BUFTYPE_ERR_RESPONSE:
1068 			sgl_iter = sgl_din_iter;
1069 			sgl_din_iter += buf_entries->buf_len;
1070 			drv_buf_iter->data_dir = DMA_NONE;
1071 			erb_offset = count;
1072 			break;
1073 		case MPI3MR_BSG_BUFTYPE_MPI_REQUEST:
1074 			sgl_iter = sgl_dout_iter;
1075 			sgl_dout_iter += buf_entries->buf_len;
1076 			drv_buf_iter->data_dir = DMA_NONE;
1077 			mpi_msg_size = buf_entries->buf_len;
1078 			if ((!mpi_msg_size || (mpi_msg_size % 4)) ||
1079 					(mpi_msg_size > MPI3MR_ADMIN_REQ_FRAME_SZ)) {
1080 				dprint_bsg_err(mrioc, "%s: invalid MPI message size\n",
1081 					__func__);
1082 				rval = -EINVAL;
1083 				goto out;
1084 			}
1085 			memcpy(mpi_req, sgl_iter, buf_entries->buf_len);
1086 			break;
1087 		default:
1088 			invalid_be = 1;
1089 			break;
1090 		}
1091 		if (invalid_be) {
1092 			dprint_bsg_err(mrioc, "%s: invalid buffer entries passed\n",
1093 				__func__);
1094 			rval = -EINVAL;
1095 			goto out;
1096 		}
1097 
1098 		drv_buf_iter->bsg_buf = sgl_iter;
1099 		drv_buf_iter->bsg_buf_len = buf_entries->buf_len;
1100 
1101 	}
1102 	if (!is_rmcb && (dout_cnt || din_cnt)) {
1103 		sg_entries = dout_cnt + din_cnt;
1104 		if (((mpi_msg_size) + (sg_entries *
1105 		      sizeof(struct mpi3_sge_common))) > MPI3MR_ADMIN_REQ_FRAME_SZ) {
1106 			dprint_bsg_err(mrioc,
1107 			    "%s:%d: invalid message size passed\n",
1108 			    __func__, __LINE__);
1109 			rval = -EINVAL;
1110 			goto out;
1111 		}
1112 	}
1113 	if (din_size > MPI3MR_MAX_APP_XFER_SIZE) {
1114 		dprint_bsg_err(mrioc,
1115 		    "%s:%d: invalid data transfer size passed for function 0x%x din_size=%d\n",
1116 		    __func__, __LINE__, mpi_header->function, din_size);
1117 		rval = -EINVAL;
1118 		goto out;
1119 	}
1120 	if (dout_size > MPI3MR_MAX_APP_XFER_SIZE) {
1121 		dprint_bsg_err(mrioc,
1122 		    "%s:%d: invalid data transfer size passed for function 0x%x dout_size = %d\n",
1123 		    __func__, __LINE__, mpi_header->function, dout_size);
1124 		rval = -EINVAL;
1125 		goto out;
1126 	}
1127 
1128 	drv_buf_iter = drv_bufs;
1129 	for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
1130 		if (drv_buf_iter->data_dir == DMA_NONE)
1131 			continue;
1132 
1133 		drv_buf_iter->kern_buf_len = drv_buf_iter->bsg_buf_len;
1134 		if (is_rmcb && !count)
1135 			drv_buf_iter->kern_buf_len += ((dout_cnt + din_cnt) *
1136 			    sizeof(struct mpi3_sge_common));
1137 
1138 		if (!drv_buf_iter->kern_buf_len)
1139 			continue;
1140 
1141 		drv_buf_iter->kern_buf = dma_alloc_coherent(&mrioc->pdev->dev,
1142 		    drv_buf_iter->kern_buf_len, &drv_buf_iter->kern_buf_dma,
1143 		    GFP_KERNEL);
1144 		if (!drv_buf_iter->kern_buf) {
1145 			rval = -ENOMEM;
1146 			goto out;
1147 		}
1148 		if (drv_buf_iter->data_dir == DMA_TO_DEVICE) {
1149 			tmplen = min(drv_buf_iter->kern_buf_len,
1150 			    drv_buf_iter->bsg_buf_len);
1151 			memcpy(drv_buf_iter->kern_buf, drv_buf_iter->bsg_buf, tmplen);
1152 		}
1153 	}
1154 
1155 	if (erb_offset != 0xFF) {
1156 		sense_buff_k = kzalloc(erbsz, GFP_KERNEL);
1157 		if (!sense_buff_k) {
1158 			rval = -ENOMEM;
1159 			goto out;
1160 		}
1161 	}
1162 
1163 	if (mutex_lock_interruptible(&mrioc->bsg_cmds.mutex)) {
1164 		rval = -ERESTARTSYS;
1165 		goto out;
1166 	}
1167 	if (mrioc->bsg_cmds.state & MPI3MR_CMD_PENDING) {
1168 		rval = -EAGAIN;
1169 		dprint_bsg_err(mrioc, "%s: command is in use\n", __func__);
1170 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1171 		goto out;
1172 	}
1173 	if (mrioc->unrecoverable) {
1174 		dprint_bsg_err(mrioc, "%s: unrecoverable controller\n",
1175 		    __func__);
1176 		rval = -EFAULT;
1177 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1178 		goto out;
1179 	}
1180 	if (mrioc->reset_in_progress) {
1181 		dprint_bsg_err(mrioc, "%s: reset in progress\n", __func__);
1182 		rval = -EAGAIN;
1183 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1184 		goto out;
1185 	}
1186 	if (mrioc->stop_bsgs) {
1187 		dprint_bsg_err(mrioc, "%s: bsgs are blocked\n", __func__);
1188 		rval = -EAGAIN;
1189 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1190 		goto out;
1191 	}
1192 
1193 	if (mpi_header->function == MPI3_BSG_FUNCTION_NVME_ENCAPSULATED) {
1194 		nvme_fmt = mpi3mr_get_nvme_data_fmt(
1195 			(struct mpi3_nvme_encapsulated_request *)mpi_req);
1196 		if (nvme_fmt == MPI3MR_NVME_DATA_FORMAT_PRP) {
1197 			if (mpi3mr_build_nvme_prp(mrioc,
1198 			    (struct mpi3_nvme_encapsulated_request *)mpi_req,
1199 			    drv_bufs, bufcnt)) {
1200 				rval = -ENOMEM;
1201 				mutex_unlock(&mrioc->bsg_cmds.mutex);
1202 				goto out;
1203 			}
1204 		} else if (nvme_fmt == MPI3MR_NVME_DATA_FORMAT_SGL1 ||
1205 			nvme_fmt == MPI3MR_NVME_DATA_FORMAT_SGL2) {
1206 			if (mpi3mr_build_nvme_sgl(mrioc,
1207 			    (struct mpi3_nvme_encapsulated_request *)mpi_req,
1208 			    drv_bufs, bufcnt)) {
1209 				rval = -EINVAL;
1210 				mutex_unlock(&mrioc->bsg_cmds.mutex);
1211 				goto out;
1212 			}
1213 		} else {
1214 			dprint_bsg_err(mrioc,
1215 			    "%s:invalid NVMe command format\n", __func__);
1216 			rval = -EINVAL;
1217 			mutex_unlock(&mrioc->bsg_cmds.mutex);
1218 			goto out;
1219 		}
1220 	} else {
1221 		mpi3mr_bsg_build_sgl(mpi_req, (mpi_msg_size),
1222 		    drv_bufs, bufcnt, is_rmcb, is_rmrb,
1223 		    (dout_cnt + din_cnt));
1224 	}
1225 
1226 	if (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_TASK_MGMT) {
1227 		tm_req = (struct mpi3_scsi_task_mgmt_request *)mpi_req;
1228 		if (tm_req->task_type !=
1229 		    MPI3_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
1230 			dev_handle = tm_req->dev_handle;
1231 			block_io = 1;
1232 		}
1233 	}
1234 	if (block_io) {
1235 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1236 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata) {
1237 			stgt_priv = (struct mpi3mr_stgt_priv_data *)
1238 			    tgtdev->starget->hostdata;
1239 			atomic_inc(&stgt_priv->block_io);
1240 			mpi3mr_tgtdev_put(tgtdev);
1241 		}
1242 	}
1243 
1244 	mrioc->bsg_cmds.state = MPI3MR_CMD_PENDING;
1245 	mrioc->bsg_cmds.is_waiting = 1;
1246 	mrioc->bsg_cmds.callback = NULL;
1247 	mrioc->bsg_cmds.is_sense = 0;
1248 	mrioc->bsg_cmds.sensebuf = sense_buff_k;
1249 	memset(mrioc->bsg_cmds.reply, 0, mrioc->reply_sz);
1250 	mpi_header->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_BSG_CMDS);
1251 	if (mrioc->logging_level & MPI3_DEBUG_BSG_INFO) {
1252 		dprint_bsg_info(mrioc,
1253 		    "%s: posting bsg request to the controller\n", __func__);
1254 		dprint_dump(mpi_req, MPI3MR_ADMIN_REQ_FRAME_SZ,
1255 		    "bsg_mpi3_req");
1256 		if (mpi_header->function == MPI3_BSG_FUNCTION_MGMT_PASSTHROUGH) {
1257 			drv_buf_iter = &drv_bufs[0];
1258 			dprint_dump(drv_buf_iter->kern_buf,
1259 			    drv_buf_iter->kern_buf_len, "mpi3_mgmt_req");
1260 		}
1261 	}
1262 
1263 	init_completion(&mrioc->bsg_cmds.done);
1264 	rval = mpi3mr_admin_request_post(mrioc, mpi_req,
1265 	    MPI3MR_ADMIN_REQ_FRAME_SZ, 0);
1266 
1267 
1268 	if (rval) {
1269 		mrioc->bsg_cmds.is_waiting = 0;
1270 		dprint_bsg_err(mrioc,
1271 		    "%s: posting bsg request is failed\n", __func__);
1272 		rval = -EAGAIN;
1273 		goto out_unlock;
1274 	}
1275 	wait_for_completion_timeout(&mrioc->bsg_cmds.done,
1276 	    (karg->timeout * HZ));
1277 	if (block_io && stgt_priv)
1278 		atomic_dec(&stgt_priv->block_io);
1279 	if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE)) {
1280 		mrioc->bsg_cmds.is_waiting = 0;
1281 		rval = -EAGAIN;
1282 		if (mrioc->bsg_cmds.state & MPI3MR_CMD_RESET)
1283 			goto out_unlock;
1284 		dprint_bsg_err(mrioc,
1285 		    "%s: bsg request timedout after %d seconds\n", __func__,
1286 		    karg->timeout);
1287 		if (mrioc->logging_level & MPI3_DEBUG_BSG_ERROR) {
1288 			dprint_dump(mpi_req, MPI3MR_ADMIN_REQ_FRAME_SZ,
1289 			    "bsg_mpi3_req");
1290 			if (mpi_header->function ==
1291 			    MPI3_BSG_FUNCTION_MGMT_PASSTHROUGH) {
1292 				drv_buf_iter = &drv_bufs[0];
1293 				dprint_dump(drv_buf_iter->kern_buf,
1294 				    drv_buf_iter->kern_buf_len, "mpi3_mgmt_req");
1295 			}
1296 		}
1297 
1298 		if ((mpi_header->function == MPI3_BSG_FUNCTION_NVME_ENCAPSULATED) ||
1299 		    (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_IO))
1300 			mpi3mr_issue_tm(mrioc,
1301 			    MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
1302 			    mpi_header->function_dependent, 0,
1303 			    MPI3MR_HOSTTAG_BLK_TMS, MPI3MR_RESETTM_TIMEOUT,
1304 			    &mrioc->host_tm_cmds, &resp_code, NULL);
1305 		if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE) &&
1306 		    !(mrioc->bsg_cmds.state & MPI3MR_CMD_RESET))
1307 			mpi3mr_soft_reset_handler(mrioc,
1308 			    MPI3MR_RESET_FROM_APP_TIMEOUT, 1);
1309 		goto out_unlock;
1310 	}
1311 	dprint_bsg_info(mrioc, "%s: bsg request is completed\n", __func__);
1312 
1313 	if (mrioc->prp_list_virt) {
1314 		dma_free_coherent(&mrioc->pdev->dev, mrioc->prp_sz,
1315 		    mrioc->prp_list_virt, mrioc->prp_list_dma);
1316 		mrioc->prp_list_virt = NULL;
1317 	}
1318 
1319 	if ((mrioc->bsg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1320 	     != MPI3_IOCSTATUS_SUCCESS) {
1321 		dprint_bsg_info(mrioc,
1322 		    "%s: command failed, ioc_status(0x%04x) log_info(0x%08x)\n",
1323 		    __func__,
1324 		    (mrioc->bsg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1325 		    mrioc->bsg_cmds.ioc_loginfo);
1326 	}
1327 
1328 	if ((mpirep_offset != 0xFF) &&
1329 	    drv_bufs[mpirep_offset].bsg_buf_len) {
1330 		drv_buf_iter = &drv_bufs[mpirep_offset];
1331 		drv_buf_iter->kern_buf_len = (sizeof(*bsg_reply_buf) - 1 +
1332 					   mrioc->reply_sz);
1333 		bsg_reply_buf = kzalloc(drv_buf_iter->kern_buf_len, GFP_KERNEL);
1334 
1335 		if (!bsg_reply_buf) {
1336 			rval = -ENOMEM;
1337 			goto out_unlock;
1338 		}
1339 		if (mrioc->bsg_cmds.state & MPI3MR_CMD_REPLY_VALID) {
1340 			bsg_reply_buf->mpi_reply_type =
1341 				MPI3MR_BSG_MPI_REPLY_BUFTYPE_ADDRESS;
1342 			memcpy(bsg_reply_buf->reply_buf,
1343 			    mrioc->bsg_cmds.reply, mrioc->reply_sz);
1344 		} else {
1345 			bsg_reply_buf->mpi_reply_type =
1346 				MPI3MR_BSG_MPI_REPLY_BUFTYPE_STATUS;
1347 			status_desc = (struct mpi3_status_reply_descriptor *)
1348 			    bsg_reply_buf->reply_buf;
1349 			status_desc->ioc_status = mrioc->bsg_cmds.ioc_status;
1350 			status_desc->ioc_log_info = mrioc->bsg_cmds.ioc_loginfo;
1351 		}
1352 		tmplen = min(drv_buf_iter->kern_buf_len,
1353 			drv_buf_iter->bsg_buf_len);
1354 		memcpy(drv_buf_iter->bsg_buf, bsg_reply_buf, tmplen);
1355 	}
1356 
1357 	if (erb_offset != 0xFF && mrioc->bsg_cmds.sensebuf &&
1358 	    mrioc->bsg_cmds.is_sense) {
1359 		drv_buf_iter = &drv_bufs[erb_offset];
1360 		tmplen = min(erbsz, drv_buf_iter->bsg_buf_len);
1361 		memcpy(drv_buf_iter->bsg_buf, sense_buff_k, tmplen);
1362 	}
1363 
1364 	drv_buf_iter = drv_bufs;
1365 	for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
1366 		if (drv_buf_iter->data_dir == DMA_NONE)
1367 			continue;
1368 		if (drv_buf_iter->data_dir == DMA_FROM_DEVICE) {
1369 			tmplen = min(drv_buf_iter->kern_buf_len,
1370 				     drv_buf_iter->bsg_buf_len);
1371 			memcpy(drv_buf_iter->bsg_buf,
1372 			       drv_buf_iter->kern_buf, tmplen);
1373 		}
1374 	}
1375 
1376 out_unlock:
1377 	if (din_buf) {
1378 		*reply_payload_rcv_len =
1379 			sg_copy_from_buffer(job->reply_payload.sg_list,
1380 					    job->reply_payload.sg_cnt,
1381 					    din_buf, job->reply_payload.payload_len);
1382 	}
1383 	mrioc->bsg_cmds.is_sense = 0;
1384 	mrioc->bsg_cmds.sensebuf = NULL;
1385 	mrioc->bsg_cmds.state = MPI3MR_CMD_NOTUSED;
1386 	mutex_unlock(&mrioc->bsg_cmds.mutex);
1387 out:
1388 	kfree(sense_buff_k);
1389 	kfree(dout_buf);
1390 	kfree(din_buf);
1391 	kfree(mpi_req);
1392 	if (drv_bufs) {
1393 		drv_buf_iter = drv_bufs;
1394 		for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
1395 			if (drv_buf_iter->kern_buf && drv_buf_iter->kern_buf_dma)
1396 				dma_free_coherent(&mrioc->pdev->dev,
1397 				    drv_buf_iter->kern_buf_len,
1398 				    drv_buf_iter->kern_buf,
1399 				    drv_buf_iter->kern_buf_dma);
1400 		}
1401 		kfree(drv_bufs);
1402 	}
1403 	kfree(bsg_reply_buf);
1404 	return rval;
1405 }
1406 
1407 /**
1408  * mpi3mr_app_save_logdata - Save Log Data events
1409  * @mrioc: Adapter instance reference
1410  * @event_data: event data associated with log data event
1411  * @event_data_size: event data size to copy
1412  *
1413  * If log data event caching is enabled by the applicatiobns,
1414  * then this function saves the log data in the circular queue
1415  * and Sends async signal SIGIO to indicate there is an async
1416  * event from the firmware to the event monitoring applications.
1417  *
1418  * Return:Nothing
1419  */
1420 void mpi3mr_app_save_logdata(struct mpi3mr_ioc *mrioc, char *event_data,
1421 	u16 event_data_size)
1422 {
1423 	u32 index = mrioc->logdata_buf_idx, sz;
1424 	struct mpi3mr_logdata_entry *entry;
1425 
1426 	if (!(mrioc->logdata_buf))
1427 		return;
1428 
1429 	entry = (struct mpi3mr_logdata_entry *)
1430 		(mrioc->logdata_buf + (index * mrioc->logdata_entry_sz));
1431 	entry->valid_entry = 1;
1432 	sz = min(mrioc->logdata_entry_sz, event_data_size);
1433 	memcpy(entry->data, event_data, sz);
1434 	mrioc->logdata_buf_idx =
1435 		((++index) % MPI3MR_BSG_LOGDATA_MAX_ENTRIES);
1436 	atomic64_inc(&event_counter);
1437 }
1438 
1439 /**
1440  * mpi3mr_bsg_request - bsg request entry point
1441  * @job: BSG job reference
1442  *
1443  * This is driver's entry point for bsg requests
1444  *
1445  * Return: 0 on success and proper error codes on failure
1446  */
1447 static int mpi3mr_bsg_request(struct bsg_job *job)
1448 {
1449 	long rval = -EINVAL;
1450 	unsigned int reply_payload_rcv_len = 0;
1451 
1452 	struct mpi3mr_bsg_packet *bsg_req = job->request;
1453 
1454 	switch (bsg_req->cmd_type) {
1455 	case MPI3MR_DRV_CMD:
1456 		rval = mpi3mr_bsg_process_drv_cmds(job);
1457 		break;
1458 	case MPI3MR_MPT_CMD:
1459 		rval = mpi3mr_bsg_process_mpt_cmds(job, &reply_payload_rcv_len);
1460 		break;
1461 	default:
1462 		pr_err("%s: unsupported BSG command(0x%08x)\n",
1463 		    MPI3MR_DRIVER_NAME, bsg_req->cmd_type);
1464 		break;
1465 	}
1466 
1467 	bsg_job_done(job, rval, reply_payload_rcv_len);
1468 
1469 	return 0;
1470 }
1471 
1472 /**
1473  * mpi3mr_bsg_exit - de-registration from bsg layer
1474  *
1475  * This will be called during driver unload and all
1476  * bsg resources allocated during load will be freed.
1477  *
1478  * Return:Nothing
1479  */
1480 void mpi3mr_bsg_exit(struct mpi3mr_ioc *mrioc)
1481 {
1482 	struct device *bsg_dev = &mrioc->bsg_dev;
1483 	if (!mrioc->bsg_queue)
1484 		return;
1485 
1486 	bsg_remove_queue(mrioc->bsg_queue);
1487 	mrioc->bsg_queue = NULL;
1488 
1489 	device_del(bsg_dev);
1490 	put_device(bsg_dev);
1491 }
1492 
1493 /**
1494  * mpi3mr_bsg_node_release -release bsg device node
1495  * @dev: bsg device node
1496  *
1497  * decrements bsg dev parent reference count
1498  *
1499  * Return:Nothing
1500  */
1501 static void mpi3mr_bsg_node_release(struct device *dev)
1502 {
1503 	put_device(dev->parent);
1504 }
1505 
1506 /**
1507  * mpi3mr_bsg_init -  registration with bsg layer
1508  *
1509  * This will be called during driver load and it will
1510  * register driver with bsg layer
1511  *
1512  * Return:Nothing
1513  */
1514 void mpi3mr_bsg_init(struct mpi3mr_ioc *mrioc)
1515 {
1516 	struct device *bsg_dev = &mrioc->bsg_dev;
1517 	struct device *parent = &mrioc->shost->shost_gendev;
1518 
1519 	device_initialize(bsg_dev);
1520 
1521 	bsg_dev->parent = get_device(parent);
1522 	bsg_dev->release = mpi3mr_bsg_node_release;
1523 
1524 	dev_set_name(bsg_dev, "mpi3mrctl%u", mrioc->id);
1525 
1526 	if (device_add(bsg_dev)) {
1527 		ioc_err(mrioc, "%s: bsg device add failed\n",
1528 		    dev_name(bsg_dev));
1529 		put_device(bsg_dev);
1530 		return;
1531 	}
1532 
1533 	mrioc->bsg_queue = bsg_setup_queue(bsg_dev, dev_name(bsg_dev),
1534 			mpi3mr_bsg_request, NULL, 0);
1535 	if (IS_ERR(mrioc->bsg_queue)) {
1536 		ioc_err(mrioc, "%s: bsg registration failed\n",
1537 		    dev_name(bsg_dev));
1538 		device_del(bsg_dev);
1539 		put_device(bsg_dev);
1540 		return;
1541 	}
1542 
1543 	blk_queue_max_segments(mrioc->bsg_queue, MPI3MR_MAX_APP_XFER_SEGMENTS);
1544 	blk_queue_max_hw_sectors(mrioc->bsg_queue, MPI3MR_MAX_APP_XFER_SECTORS);
1545 
1546 	return;
1547 }
1548 
1549 /**
1550  * version_fw_show - SysFS callback for firmware version read
1551  * @dev: class device
1552  * @attr: Device attributes
1553  * @buf: Buffer to copy
1554  *
1555  * Return: sysfs_emit() return after copying firmware version
1556  */
1557 static ssize_t
1558 version_fw_show(struct device *dev, struct device_attribute *attr,
1559 	char *buf)
1560 {
1561 	struct Scsi_Host *shost = class_to_shost(dev);
1562 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1563 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
1564 
1565 	return sysfs_emit(buf, "%d.%d.%d.%d.%05d-%05d\n",
1566 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
1567 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
1568 }
1569 static DEVICE_ATTR_RO(version_fw);
1570 
1571 /**
1572  * fw_queue_depth_show - SysFS callback for firmware max cmds
1573  * @dev: class device
1574  * @attr: Device attributes
1575  * @buf: Buffer to copy
1576  *
1577  * Return: sysfs_emit() return after copying firmware max commands
1578  */
1579 static ssize_t
1580 fw_queue_depth_show(struct device *dev, struct device_attribute *attr,
1581 			char *buf)
1582 {
1583 	struct Scsi_Host *shost = class_to_shost(dev);
1584 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1585 
1586 	return sysfs_emit(buf, "%d\n", mrioc->facts.max_reqs);
1587 }
1588 static DEVICE_ATTR_RO(fw_queue_depth);
1589 
1590 /**
1591  * op_req_q_count_show - SysFS callback for request queue count
1592  * @dev: class device
1593  * @attr: Device attributes
1594  * @buf: Buffer to copy
1595  *
1596  * Return: sysfs_emit() return after copying request queue count
1597  */
1598 static ssize_t
1599 op_req_q_count_show(struct device *dev, struct device_attribute *attr,
1600 			char *buf)
1601 {
1602 	struct Scsi_Host *shost = class_to_shost(dev);
1603 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1604 
1605 	return sysfs_emit(buf, "%d\n", mrioc->num_op_req_q);
1606 }
1607 static DEVICE_ATTR_RO(op_req_q_count);
1608 
1609 /**
1610  * reply_queue_count_show - SysFS callback for reply queue count
1611  * @dev: class device
1612  * @attr: Device attributes
1613  * @buf: Buffer to copy
1614  *
1615  * Return: sysfs_emit() return after copying reply queue count
1616  */
1617 static ssize_t
1618 reply_queue_count_show(struct device *dev, struct device_attribute *attr,
1619 			char *buf)
1620 {
1621 	struct Scsi_Host *shost = class_to_shost(dev);
1622 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1623 
1624 	return sysfs_emit(buf, "%d\n", mrioc->num_op_reply_q);
1625 }
1626 
1627 static DEVICE_ATTR_RO(reply_queue_count);
1628 
1629 /**
1630  * logging_level_show - Show controller debug level
1631  * @dev: class device
1632  * @attr: Device attributes
1633  * @buf: Buffer to copy
1634  *
1635  * A sysfs 'read/write' shost attribute, to show the current
1636  * debug log level used by the driver for the specific
1637  * controller.
1638  *
1639  * Return: sysfs_emit() return
1640  */
1641 static ssize_t
1642 logging_level_show(struct device *dev,
1643 	struct device_attribute *attr, char *buf)
1644 
1645 {
1646 	struct Scsi_Host *shost = class_to_shost(dev);
1647 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1648 
1649 	return sysfs_emit(buf, "%08xh\n", mrioc->logging_level);
1650 }
1651 
1652 /**
1653  * logging_level_store- Change controller debug level
1654  * @dev: class device
1655  * @attr: Device attributes
1656  * @buf: Buffer to copy
1657  * @count: size of the buffer
1658  *
1659  * A sysfs 'read/write' shost attribute, to change the current
1660  * debug log level used by the driver for the specific
1661  * controller.
1662  *
1663  * Return: strlen() return
1664  */
1665 static ssize_t
1666 logging_level_store(struct device *dev,
1667 	struct device_attribute *attr,
1668 	const char *buf, size_t count)
1669 {
1670 	struct Scsi_Host *shost = class_to_shost(dev);
1671 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1672 	int val = 0;
1673 
1674 	if (kstrtoint(buf, 0, &val) != 0)
1675 		return -EINVAL;
1676 
1677 	mrioc->logging_level = val;
1678 	ioc_info(mrioc, "logging_level=%08xh\n", mrioc->logging_level);
1679 	return strlen(buf);
1680 }
1681 static DEVICE_ATTR_RW(logging_level);
1682 
1683 /**
1684  * adp_state_show() - SysFS callback for adapter state show
1685  * @dev: class device
1686  * @attr: Device attributes
1687  * @buf: Buffer to copy
1688  *
1689  * Return: sysfs_emit() return after copying adapter state
1690  */
1691 static ssize_t
1692 adp_state_show(struct device *dev, struct device_attribute *attr,
1693 	char *buf)
1694 {
1695 	struct Scsi_Host *shost = class_to_shost(dev);
1696 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1697 	enum mpi3mr_iocstate ioc_state;
1698 	uint8_t adp_state;
1699 
1700 	ioc_state = mpi3mr_get_iocstate(mrioc);
1701 	if (ioc_state == MRIOC_STATE_UNRECOVERABLE)
1702 		adp_state = MPI3MR_BSG_ADPSTATE_UNRECOVERABLE;
1703 	else if ((mrioc->reset_in_progress) || (mrioc->stop_bsgs))
1704 		adp_state = MPI3MR_BSG_ADPSTATE_IN_RESET;
1705 	else if (ioc_state == MRIOC_STATE_FAULT)
1706 		adp_state = MPI3MR_BSG_ADPSTATE_FAULT;
1707 	else
1708 		adp_state = MPI3MR_BSG_ADPSTATE_OPERATIONAL;
1709 
1710 	return sysfs_emit(buf, "%u\n", adp_state);
1711 }
1712 
1713 static DEVICE_ATTR_RO(adp_state);
1714 
1715 static struct attribute *mpi3mr_host_attrs[] = {
1716 	&dev_attr_version_fw.attr,
1717 	&dev_attr_fw_queue_depth.attr,
1718 	&dev_attr_op_req_q_count.attr,
1719 	&dev_attr_reply_queue_count.attr,
1720 	&dev_attr_logging_level.attr,
1721 	&dev_attr_adp_state.attr,
1722 	NULL,
1723 };
1724 
1725 static const struct attribute_group mpi3mr_host_attr_group = {
1726 	.attrs = mpi3mr_host_attrs
1727 };
1728 
1729 const struct attribute_group *mpi3mr_host_groups[] = {
1730 	&mpi3mr_host_attr_group,
1731 	NULL,
1732 };
1733 
1734 
1735 /*
1736  * SCSI Device attributes under sysfs
1737  */
1738 
1739 /**
1740  * sas_address_show - SysFS callback for dev SASaddress display
1741  * @dev: class device
1742  * @attr: Device attributes
1743  * @buf: Buffer to copy
1744  *
1745  * Return: sysfs_emit() return after copying SAS address of the
1746  * specific SAS/SATA end device.
1747  */
1748 static ssize_t
1749 sas_address_show(struct device *dev, struct device_attribute *attr,
1750 			char *buf)
1751 {
1752 	struct scsi_device *sdev = to_scsi_device(dev);
1753 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
1754 	struct mpi3mr_stgt_priv_data *tgt_priv_data;
1755 	struct mpi3mr_tgt_dev *tgtdev;
1756 
1757 	sdev_priv_data = sdev->hostdata;
1758 	if (!sdev_priv_data)
1759 		return 0;
1760 
1761 	tgt_priv_data = sdev_priv_data->tgt_priv_data;
1762 	if (!tgt_priv_data)
1763 		return 0;
1764 	tgtdev = tgt_priv_data->tgt_dev;
1765 	if (!tgtdev || tgtdev->dev_type != MPI3_DEVICE_DEVFORM_SAS_SATA)
1766 		return 0;
1767 	return sysfs_emit(buf, "0x%016llx\n",
1768 	    (unsigned long long)tgtdev->dev_spec.sas_sata_inf.sas_address);
1769 }
1770 
1771 static DEVICE_ATTR_RO(sas_address);
1772 
1773 /**
1774  * device_handle_show - SysFS callback for device handle display
1775  * @dev: class device
1776  * @attr: Device attributes
1777  * @buf: Buffer to copy
1778  *
1779  * Return: sysfs_emit() return after copying firmware internal
1780  * device handle of the specific device.
1781  */
1782 static ssize_t
1783 device_handle_show(struct device *dev, struct device_attribute *attr,
1784 			char *buf)
1785 {
1786 	struct scsi_device *sdev = to_scsi_device(dev);
1787 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
1788 	struct mpi3mr_stgt_priv_data *tgt_priv_data;
1789 	struct mpi3mr_tgt_dev *tgtdev;
1790 
1791 	sdev_priv_data = sdev->hostdata;
1792 	if (!sdev_priv_data)
1793 		return 0;
1794 
1795 	tgt_priv_data = sdev_priv_data->tgt_priv_data;
1796 	if (!tgt_priv_data)
1797 		return 0;
1798 	tgtdev = tgt_priv_data->tgt_dev;
1799 	if (!tgtdev)
1800 		return 0;
1801 	return sysfs_emit(buf, "0x%04x\n", tgtdev->dev_handle);
1802 }
1803 
1804 static DEVICE_ATTR_RO(device_handle);
1805 
1806 /**
1807  * persistent_id_show - SysFS callback for persisten ID display
1808  * @dev: class device
1809  * @attr: Device attributes
1810  * @buf: Buffer to copy
1811  *
1812  * Return: sysfs_emit() return after copying persistent ID of the
1813  * of the specific device.
1814  */
1815 static ssize_t
1816 persistent_id_show(struct device *dev, struct device_attribute *attr,
1817 			char *buf)
1818 {
1819 	struct scsi_device *sdev = to_scsi_device(dev);
1820 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
1821 	struct mpi3mr_stgt_priv_data *tgt_priv_data;
1822 	struct mpi3mr_tgt_dev *tgtdev;
1823 
1824 	sdev_priv_data = sdev->hostdata;
1825 	if (!sdev_priv_data)
1826 		return 0;
1827 
1828 	tgt_priv_data = sdev_priv_data->tgt_priv_data;
1829 	if (!tgt_priv_data)
1830 		return 0;
1831 	tgtdev = tgt_priv_data->tgt_dev;
1832 	if (!tgtdev)
1833 		return 0;
1834 	return sysfs_emit(buf, "%d\n", tgtdev->perst_id);
1835 }
1836 static DEVICE_ATTR_RO(persistent_id);
1837 
1838 static struct attribute *mpi3mr_dev_attrs[] = {
1839 	&dev_attr_sas_address.attr,
1840 	&dev_attr_device_handle.attr,
1841 	&dev_attr_persistent_id.attr,
1842 	NULL,
1843 };
1844 
1845 static const struct attribute_group mpi3mr_dev_attr_group = {
1846 	.attrs = mpi3mr_dev_attrs
1847 };
1848 
1849 const struct attribute_group *mpi3mr_dev_groups[] = {
1850 	&mpi3mr_dev_attr_group,
1851 	NULL,
1852 };
1853