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