xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_app.c (revision f4356947)
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 	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  * @reply_payload_rcv_len: length of payload recvd
926  *
927  * This function is the top level handler for MPI Pass through
928  * command, this does basic validation of the input data buffers,
929  * identifies the given buffer types and MPI command, allocates
930  * DMAable memory for user given buffers, construstcs SGL
931  * properly and passes the command to the firmware.
932  *
933  * Once the MPI command is completed the driver copies the data
934  * if any and reply, sense information to user provided buffers.
935  * If the command is timed out then issues controller reset
936  * prior to returning.
937  *
938  * Return: 0 on success and proper error codes on failure
939  */
940 
941 static long mpi3mr_bsg_process_mpt_cmds(struct bsg_job *job, unsigned int *reply_payload_rcv_len)
942 {
943 	long rval = -EINVAL;
944 
945 	struct mpi3mr_ioc *mrioc = NULL;
946 	u8 *mpi_req = NULL, *sense_buff_k = NULL;
947 	u8 mpi_msg_size = 0;
948 	struct mpi3mr_bsg_packet *bsg_req = NULL;
949 	struct mpi3mr_bsg_mptcmd *karg;
950 	struct mpi3mr_buf_entry *buf_entries = NULL;
951 	struct mpi3mr_buf_map *drv_bufs = NULL, *drv_buf_iter = NULL;
952 	u8 count, bufcnt = 0, is_rmcb = 0, is_rmrb = 0, din_cnt = 0, dout_cnt = 0;
953 	u8 invalid_be = 0, erb_offset = 0xFF, mpirep_offset = 0xFF, sg_entries = 0;
954 	u8 block_io = 0, resp_code = 0, nvme_fmt = 0;
955 	struct mpi3_request_header *mpi_header = NULL;
956 	struct mpi3_status_reply_descriptor *status_desc;
957 	struct mpi3_scsi_task_mgmt_request *tm_req;
958 	u32 erbsz = MPI3MR_SENSE_BUF_SZ, tmplen;
959 	u16 dev_handle;
960 	struct mpi3mr_tgt_dev *tgtdev;
961 	struct mpi3mr_stgt_priv_data *stgt_priv = NULL;
962 	struct mpi3mr_bsg_in_reply_buf *bsg_reply_buf = NULL;
963 	u32 din_size = 0, dout_size = 0;
964 	u8 *din_buf = NULL, *dout_buf = NULL;
965 	u8 *sgl_iter = NULL, *sgl_din_iter = NULL, *sgl_dout_iter = NULL;
966 
967 	bsg_req = job->request;
968 	karg = (struct mpi3mr_bsg_mptcmd *)&bsg_req->cmd.mptcmd;
969 
970 	mrioc = mpi3mr_bsg_verify_adapter(karg->mrioc_id);
971 	if (!mrioc)
972 		return -ENODEV;
973 
974 	if (karg->timeout < MPI3MR_APP_DEFAULT_TIMEOUT)
975 		karg->timeout = MPI3MR_APP_DEFAULT_TIMEOUT;
976 
977 	mpi_req = kzalloc(MPI3MR_ADMIN_REQ_FRAME_SZ, GFP_KERNEL);
978 	if (!mpi_req)
979 		return -ENOMEM;
980 	mpi_header = (struct mpi3_request_header *)mpi_req;
981 
982 	bufcnt = karg->buf_entry_list.num_of_entries;
983 	drv_bufs = kzalloc((sizeof(*drv_bufs) * bufcnt), GFP_KERNEL);
984 	if (!drv_bufs) {
985 		rval = -ENOMEM;
986 		goto out;
987 	}
988 
989 	dout_buf = kzalloc(job->request_payload.payload_len,
990 				      GFP_KERNEL);
991 	if (!dout_buf) {
992 		rval = -ENOMEM;
993 		goto out;
994 	}
995 
996 	din_buf = kzalloc(job->reply_payload.payload_len,
997 				     GFP_KERNEL);
998 	if (!din_buf) {
999 		rval = -ENOMEM;
1000 		goto out;
1001 	}
1002 
1003 	sg_copy_to_buffer(job->request_payload.sg_list,
1004 			  job->request_payload.sg_cnt,
1005 			  dout_buf, job->request_payload.payload_len);
1006 
1007 	buf_entries = karg->buf_entry_list.buf_entry;
1008 	sgl_din_iter = din_buf;
1009 	sgl_dout_iter = dout_buf;
1010 	drv_buf_iter = drv_bufs;
1011 
1012 	for (count = 0; count < bufcnt; count++, buf_entries++, drv_buf_iter++) {
1013 
1014 		if (sgl_dout_iter > (dout_buf + job->request_payload.payload_len)) {
1015 			dprint_bsg_err(mrioc, "%s: data_out buffer length mismatch\n",
1016 				__func__);
1017 			rval = -EINVAL;
1018 			goto out;
1019 		}
1020 		if (sgl_din_iter > (din_buf + job->reply_payload.payload_len)) {
1021 			dprint_bsg_err(mrioc, "%s: data_in buffer length mismatch\n",
1022 				__func__);
1023 			rval = -EINVAL;
1024 			goto out;
1025 		}
1026 
1027 		switch (buf_entries->buf_type) {
1028 		case MPI3MR_BSG_BUFTYPE_RAIDMGMT_CMD:
1029 			sgl_iter = sgl_dout_iter;
1030 			sgl_dout_iter += buf_entries->buf_len;
1031 			drv_buf_iter->data_dir = DMA_TO_DEVICE;
1032 			is_rmcb = 1;
1033 			if (count != 0)
1034 				invalid_be = 1;
1035 			break;
1036 		case MPI3MR_BSG_BUFTYPE_RAIDMGMT_RESP:
1037 			sgl_iter = sgl_din_iter;
1038 			sgl_din_iter += buf_entries->buf_len;
1039 			drv_buf_iter->data_dir = DMA_FROM_DEVICE;
1040 			is_rmrb = 1;
1041 			if (count != 1 || !is_rmcb)
1042 				invalid_be = 1;
1043 			break;
1044 		case MPI3MR_BSG_BUFTYPE_DATA_IN:
1045 			sgl_iter = sgl_din_iter;
1046 			sgl_din_iter += buf_entries->buf_len;
1047 			drv_buf_iter->data_dir = DMA_FROM_DEVICE;
1048 			din_cnt++;
1049 			din_size += drv_buf_iter->bsg_buf_len;
1050 			if ((din_cnt > 1) && !is_rmcb)
1051 				invalid_be = 1;
1052 			break;
1053 		case MPI3MR_BSG_BUFTYPE_DATA_OUT:
1054 			sgl_iter = sgl_dout_iter;
1055 			sgl_dout_iter += buf_entries->buf_len;
1056 			drv_buf_iter->data_dir = DMA_TO_DEVICE;
1057 			dout_cnt++;
1058 			dout_size += drv_buf_iter->bsg_buf_len;
1059 			if ((dout_cnt > 1) && !is_rmcb)
1060 				invalid_be = 1;
1061 			break;
1062 		case MPI3MR_BSG_BUFTYPE_MPI_REPLY:
1063 			sgl_iter = sgl_din_iter;
1064 			sgl_din_iter += buf_entries->buf_len;
1065 			drv_buf_iter->data_dir = DMA_NONE;
1066 			mpirep_offset = count;
1067 			break;
1068 		case MPI3MR_BSG_BUFTYPE_ERR_RESPONSE:
1069 			sgl_iter = sgl_din_iter;
1070 			sgl_din_iter += buf_entries->buf_len;
1071 			drv_buf_iter->data_dir = DMA_NONE;
1072 			erb_offset = count;
1073 			break;
1074 		case MPI3MR_BSG_BUFTYPE_MPI_REQUEST:
1075 			sgl_iter = sgl_dout_iter;
1076 			sgl_dout_iter += buf_entries->buf_len;
1077 			drv_buf_iter->data_dir = DMA_NONE;
1078 			mpi_msg_size = buf_entries->buf_len;
1079 			if ((!mpi_msg_size || (mpi_msg_size % 4)) ||
1080 					(mpi_msg_size > MPI3MR_ADMIN_REQ_FRAME_SZ)) {
1081 				dprint_bsg_err(mrioc, "%s: invalid MPI message size\n",
1082 					__func__);
1083 				rval = -EINVAL;
1084 				goto out;
1085 			}
1086 			memcpy(mpi_req, sgl_iter, buf_entries->buf_len);
1087 			break;
1088 		default:
1089 			invalid_be = 1;
1090 			break;
1091 		}
1092 		if (invalid_be) {
1093 			dprint_bsg_err(mrioc, "%s: invalid buffer entries passed\n",
1094 				__func__);
1095 			rval = -EINVAL;
1096 			goto out;
1097 		}
1098 
1099 		drv_buf_iter->bsg_buf = sgl_iter;
1100 		drv_buf_iter->bsg_buf_len = buf_entries->buf_len;
1101 
1102 	}
1103 	if (!is_rmcb && (dout_cnt || din_cnt)) {
1104 		sg_entries = dout_cnt + din_cnt;
1105 		if (((mpi_msg_size) + (sg_entries *
1106 		      sizeof(struct mpi3_sge_common))) > MPI3MR_ADMIN_REQ_FRAME_SZ) {
1107 			dprint_bsg_err(mrioc,
1108 			    "%s:%d: invalid message size passed\n",
1109 			    __func__, __LINE__);
1110 			rval = -EINVAL;
1111 			goto out;
1112 		}
1113 	}
1114 	if (din_size > MPI3MR_MAX_APP_XFER_SIZE) {
1115 		dprint_bsg_err(mrioc,
1116 		    "%s:%d: invalid data transfer size passed for function 0x%x din_size=%d\n",
1117 		    __func__, __LINE__, mpi_header->function, din_size);
1118 		rval = -EINVAL;
1119 		goto out;
1120 	}
1121 	if (dout_size > MPI3MR_MAX_APP_XFER_SIZE) {
1122 		dprint_bsg_err(mrioc,
1123 		    "%s:%d: invalid data transfer size passed for function 0x%x dout_size = %d\n",
1124 		    __func__, __LINE__, mpi_header->function, dout_size);
1125 		rval = -EINVAL;
1126 		goto out;
1127 	}
1128 
1129 	drv_buf_iter = drv_bufs;
1130 	for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
1131 		if (drv_buf_iter->data_dir == DMA_NONE)
1132 			continue;
1133 
1134 		drv_buf_iter->kern_buf_len = drv_buf_iter->bsg_buf_len;
1135 		if (is_rmcb && !count)
1136 			drv_buf_iter->kern_buf_len += ((dout_cnt + din_cnt) *
1137 			    sizeof(struct mpi3_sge_common));
1138 
1139 		if (!drv_buf_iter->kern_buf_len)
1140 			continue;
1141 
1142 		drv_buf_iter->kern_buf = dma_alloc_coherent(&mrioc->pdev->dev,
1143 		    drv_buf_iter->kern_buf_len, &drv_buf_iter->kern_buf_dma,
1144 		    GFP_KERNEL);
1145 		if (!drv_buf_iter->kern_buf) {
1146 			rval = -ENOMEM;
1147 			goto out;
1148 		}
1149 		if (drv_buf_iter->data_dir == DMA_TO_DEVICE) {
1150 			tmplen = min(drv_buf_iter->kern_buf_len,
1151 			    drv_buf_iter->bsg_buf_len);
1152 			memcpy(drv_buf_iter->kern_buf, drv_buf_iter->bsg_buf, tmplen);
1153 		}
1154 	}
1155 
1156 	if (erb_offset != 0xFF) {
1157 		sense_buff_k = kzalloc(erbsz, GFP_KERNEL);
1158 		if (!sense_buff_k) {
1159 			rval = -ENOMEM;
1160 			goto out;
1161 		}
1162 	}
1163 
1164 	if (mutex_lock_interruptible(&mrioc->bsg_cmds.mutex)) {
1165 		rval = -ERESTARTSYS;
1166 		goto out;
1167 	}
1168 	if (mrioc->bsg_cmds.state & MPI3MR_CMD_PENDING) {
1169 		rval = -EAGAIN;
1170 		dprint_bsg_err(mrioc, "%s: command is in use\n", __func__);
1171 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1172 		goto out;
1173 	}
1174 	if (mrioc->unrecoverable) {
1175 		dprint_bsg_err(mrioc, "%s: unrecoverable controller\n",
1176 		    __func__);
1177 		rval = -EFAULT;
1178 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1179 		goto out;
1180 	}
1181 	if (mrioc->reset_in_progress) {
1182 		dprint_bsg_err(mrioc, "%s: reset in progress\n", __func__);
1183 		rval = -EAGAIN;
1184 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1185 		goto out;
1186 	}
1187 	if (mrioc->stop_bsgs) {
1188 		dprint_bsg_err(mrioc, "%s: bsgs are blocked\n", __func__);
1189 		rval = -EAGAIN;
1190 		mutex_unlock(&mrioc->bsg_cmds.mutex);
1191 		goto out;
1192 	}
1193 
1194 	if (mpi_header->function == MPI3_BSG_FUNCTION_NVME_ENCAPSULATED) {
1195 		nvme_fmt = mpi3mr_get_nvme_data_fmt(
1196 			(struct mpi3_nvme_encapsulated_request *)mpi_req);
1197 		if (nvme_fmt == MPI3MR_NVME_DATA_FORMAT_PRP) {
1198 			if (mpi3mr_build_nvme_prp(mrioc,
1199 			    (struct mpi3_nvme_encapsulated_request *)mpi_req,
1200 			    drv_bufs, bufcnt)) {
1201 				rval = -ENOMEM;
1202 				mutex_unlock(&mrioc->bsg_cmds.mutex);
1203 				goto out;
1204 			}
1205 		} else if (nvme_fmt == MPI3MR_NVME_DATA_FORMAT_SGL1 ||
1206 			nvme_fmt == MPI3MR_NVME_DATA_FORMAT_SGL2) {
1207 			if (mpi3mr_build_nvme_sgl(mrioc,
1208 			    (struct mpi3_nvme_encapsulated_request *)mpi_req,
1209 			    drv_bufs, bufcnt)) {
1210 				rval = -EINVAL;
1211 				mutex_unlock(&mrioc->bsg_cmds.mutex);
1212 				goto out;
1213 			}
1214 		} else {
1215 			dprint_bsg_err(mrioc,
1216 			    "%s:invalid NVMe command format\n", __func__);
1217 			rval = -EINVAL;
1218 			mutex_unlock(&mrioc->bsg_cmds.mutex);
1219 			goto out;
1220 		}
1221 	} else {
1222 		mpi3mr_bsg_build_sgl(mpi_req, (mpi_msg_size),
1223 		    drv_bufs, bufcnt, is_rmcb, is_rmrb,
1224 		    (dout_cnt + din_cnt));
1225 	}
1226 
1227 	if (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_TASK_MGMT) {
1228 		tm_req = (struct mpi3_scsi_task_mgmt_request *)mpi_req;
1229 		if (tm_req->task_type !=
1230 		    MPI3_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
1231 			dev_handle = tm_req->dev_handle;
1232 			block_io = 1;
1233 		}
1234 	}
1235 	if (block_io) {
1236 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1237 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata) {
1238 			stgt_priv = (struct mpi3mr_stgt_priv_data *)
1239 			    tgtdev->starget->hostdata;
1240 			atomic_inc(&stgt_priv->block_io);
1241 			mpi3mr_tgtdev_put(tgtdev);
1242 		}
1243 	}
1244 
1245 	mrioc->bsg_cmds.state = MPI3MR_CMD_PENDING;
1246 	mrioc->bsg_cmds.is_waiting = 1;
1247 	mrioc->bsg_cmds.callback = NULL;
1248 	mrioc->bsg_cmds.is_sense = 0;
1249 	mrioc->bsg_cmds.sensebuf = sense_buff_k;
1250 	memset(mrioc->bsg_cmds.reply, 0, mrioc->reply_sz);
1251 	mpi_header->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_BSG_CMDS);
1252 	if (mrioc->logging_level & MPI3_DEBUG_BSG_INFO) {
1253 		dprint_bsg_info(mrioc,
1254 		    "%s: posting bsg request to the controller\n", __func__);
1255 		dprint_dump(mpi_req, MPI3MR_ADMIN_REQ_FRAME_SZ,
1256 		    "bsg_mpi3_req");
1257 		if (mpi_header->function == MPI3_BSG_FUNCTION_MGMT_PASSTHROUGH) {
1258 			drv_buf_iter = &drv_bufs[0];
1259 			dprint_dump(drv_buf_iter->kern_buf,
1260 			    drv_buf_iter->kern_buf_len, "mpi3_mgmt_req");
1261 		}
1262 	}
1263 
1264 	init_completion(&mrioc->bsg_cmds.done);
1265 	rval = mpi3mr_admin_request_post(mrioc, mpi_req,
1266 	    MPI3MR_ADMIN_REQ_FRAME_SZ, 0);
1267 
1268 
1269 	if (rval) {
1270 		mrioc->bsg_cmds.is_waiting = 0;
1271 		dprint_bsg_err(mrioc,
1272 		    "%s: posting bsg request is failed\n", __func__);
1273 		rval = -EAGAIN;
1274 		goto out_unlock;
1275 	}
1276 	wait_for_completion_timeout(&mrioc->bsg_cmds.done,
1277 	    (karg->timeout * HZ));
1278 	if (block_io && stgt_priv)
1279 		atomic_dec(&stgt_priv->block_io);
1280 	if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE)) {
1281 		mrioc->bsg_cmds.is_waiting = 0;
1282 		rval = -EAGAIN;
1283 		if (mrioc->bsg_cmds.state & MPI3MR_CMD_RESET)
1284 			goto out_unlock;
1285 		dprint_bsg_err(mrioc,
1286 		    "%s: bsg request timedout after %d seconds\n", __func__,
1287 		    karg->timeout);
1288 		if (mrioc->logging_level & MPI3_DEBUG_BSG_ERROR) {
1289 			dprint_dump(mpi_req, MPI3MR_ADMIN_REQ_FRAME_SZ,
1290 			    "bsg_mpi3_req");
1291 			if (mpi_header->function ==
1292 			    MPI3_BSG_FUNCTION_MGMT_PASSTHROUGH) {
1293 				drv_buf_iter = &drv_bufs[0];
1294 				dprint_dump(drv_buf_iter->kern_buf,
1295 				    drv_buf_iter->kern_buf_len, "mpi3_mgmt_req");
1296 			}
1297 		}
1298 
1299 		if ((mpi_header->function == MPI3_BSG_FUNCTION_NVME_ENCAPSULATED) ||
1300 		    (mpi_header->function == MPI3_BSG_FUNCTION_SCSI_IO))
1301 			mpi3mr_issue_tm(mrioc,
1302 			    MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
1303 			    mpi_header->function_dependent, 0,
1304 			    MPI3MR_HOSTTAG_BLK_TMS, MPI3MR_RESETTM_TIMEOUT,
1305 			    &mrioc->host_tm_cmds, &resp_code, NULL);
1306 		if (!(mrioc->bsg_cmds.state & MPI3MR_CMD_COMPLETE) &&
1307 		    !(mrioc->bsg_cmds.state & MPI3MR_CMD_RESET))
1308 			mpi3mr_soft_reset_handler(mrioc,
1309 			    MPI3MR_RESET_FROM_APP_TIMEOUT, 1);
1310 		goto out_unlock;
1311 	}
1312 	dprint_bsg_info(mrioc, "%s: bsg request is completed\n", __func__);
1313 
1314 	if (mrioc->prp_list_virt) {
1315 		dma_free_coherent(&mrioc->pdev->dev, mrioc->prp_sz,
1316 		    mrioc->prp_list_virt, mrioc->prp_list_dma);
1317 		mrioc->prp_list_virt = NULL;
1318 	}
1319 
1320 	if ((mrioc->bsg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1321 	     != MPI3_IOCSTATUS_SUCCESS) {
1322 		dprint_bsg_info(mrioc,
1323 		    "%s: command failed, ioc_status(0x%04x) log_info(0x%08x)\n",
1324 		    __func__,
1325 		    (mrioc->bsg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1326 		    mrioc->bsg_cmds.ioc_loginfo);
1327 	}
1328 
1329 	if ((mpirep_offset != 0xFF) &&
1330 	    drv_bufs[mpirep_offset].bsg_buf_len) {
1331 		drv_buf_iter = &drv_bufs[mpirep_offset];
1332 		drv_buf_iter->kern_buf_len = (sizeof(*bsg_reply_buf) - 1 +
1333 					   mrioc->reply_sz);
1334 		bsg_reply_buf = kzalloc(drv_buf_iter->kern_buf_len, GFP_KERNEL);
1335 
1336 		if (!bsg_reply_buf) {
1337 			rval = -ENOMEM;
1338 			goto out_unlock;
1339 		}
1340 		if (mrioc->bsg_cmds.state & MPI3MR_CMD_REPLY_VALID) {
1341 			bsg_reply_buf->mpi_reply_type =
1342 				MPI3MR_BSG_MPI_REPLY_BUFTYPE_ADDRESS;
1343 			memcpy(bsg_reply_buf->reply_buf,
1344 			    mrioc->bsg_cmds.reply, mrioc->reply_sz);
1345 		} else {
1346 			bsg_reply_buf->mpi_reply_type =
1347 				MPI3MR_BSG_MPI_REPLY_BUFTYPE_STATUS;
1348 			status_desc = (struct mpi3_status_reply_descriptor *)
1349 			    bsg_reply_buf->reply_buf;
1350 			status_desc->ioc_status = mrioc->bsg_cmds.ioc_status;
1351 			status_desc->ioc_log_info = mrioc->bsg_cmds.ioc_loginfo;
1352 		}
1353 		tmplen = min(drv_buf_iter->kern_buf_len,
1354 			drv_buf_iter->bsg_buf_len);
1355 		memcpy(drv_buf_iter->bsg_buf, bsg_reply_buf, tmplen);
1356 	}
1357 
1358 	if (erb_offset != 0xFF && mrioc->bsg_cmds.sensebuf &&
1359 	    mrioc->bsg_cmds.is_sense) {
1360 		drv_buf_iter = &drv_bufs[erb_offset];
1361 		tmplen = min(erbsz, drv_buf_iter->bsg_buf_len);
1362 		memcpy(drv_buf_iter->bsg_buf, sense_buff_k, tmplen);
1363 	}
1364 
1365 	drv_buf_iter = drv_bufs;
1366 	for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
1367 		if (drv_buf_iter->data_dir == DMA_NONE)
1368 			continue;
1369 		if (drv_buf_iter->data_dir == DMA_FROM_DEVICE) {
1370 			tmplen = min(drv_buf_iter->kern_buf_len,
1371 				     drv_buf_iter->bsg_buf_len);
1372 			memcpy(drv_buf_iter->bsg_buf,
1373 			       drv_buf_iter->kern_buf, tmplen);
1374 		}
1375 	}
1376 
1377 out_unlock:
1378 	if (din_buf) {
1379 		*reply_payload_rcv_len =
1380 			sg_copy_from_buffer(job->reply_payload.sg_list,
1381 					    job->reply_payload.sg_cnt,
1382 					    din_buf, job->reply_payload.payload_len);
1383 	}
1384 	mrioc->bsg_cmds.is_sense = 0;
1385 	mrioc->bsg_cmds.sensebuf = NULL;
1386 	mrioc->bsg_cmds.state = MPI3MR_CMD_NOTUSED;
1387 	mutex_unlock(&mrioc->bsg_cmds.mutex);
1388 out:
1389 	kfree(sense_buff_k);
1390 	kfree(dout_buf);
1391 	kfree(din_buf);
1392 	kfree(mpi_req);
1393 	if (drv_bufs) {
1394 		drv_buf_iter = drv_bufs;
1395 		for (count = 0; count < bufcnt; count++, drv_buf_iter++) {
1396 			if (drv_buf_iter->kern_buf && drv_buf_iter->kern_buf_dma)
1397 				dma_free_coherent(&mrioc->pdev->dev,
1398 				    drv_buf_iter->kern_buf_len,
1399 				    drv_buf_iter->kern_buf,
1400 				    drv_buf_iter->kern_buf_dma);
1401 		}
1402 		kfree(drv_bufs);
1403 	}
1404 	kfree(bsg_reply_buf);
1405 	return rval;
1406 }
1407 
1408 /**
1409  * mpi3mr_app_save_logdata - Save Log Data events
1410  * @mrioc: Adapter instance reference
1411  * @event_data: event data associated with log data event
1412  * @event_data_size: event data size to copy
1413  *
1414  * If log data event caching is enabled by the applicatiobns,
1415  * then this function saves the log data in the circular queue
1416  * and Sends async signal SIGIO to indicate there is an async
1417  * event from the firmware to the event monitoring applications.
1418  *
1419  * Return:Nothing
1420  */
1421 void mpi3mr_app_save_logdata(struct mpi3mr_ioc *mrioc, char *event_data,
1422 	u16 event_data_size)
1423 {
1424 	u32 index = mrioc->logdata_buf_idx, sz;
1425 	struct mpi3mr_logdata_entry *entry;
1426 
1427 	if (!(mrioc->logdata_buf))
1428 		return;
1429 
1430 	entry = (struct mpi3mr_logdata_entry *)
1431 		(mrioc->logdata_buf + (index * mrioc->logdata_entry_sz));
1432 	entry->valid_entry = 1;
1433 	sz = min(mrioc->logdata_entry_sz, event_data_size);
1434 	memcpy(entry->data, event_data, sz);
1435 	mrioc->logdata_buf_idx =
1436 		((++index) % MPI3MR_BSG_LOGDATA_MAX_ENTRIES);
1437 	atomic64_inc(&event_counter);
1438 }
1439 
1440 /**
1441  * mpi3mr_bsg_request - bsg request entry point
1442  * @job: BSG job reference
1443  *
1444  * This is driver's entry point for bsg requests
1445  *
1446  * Return: 0 on success and proper error codes on failure
1447  */
1448 static int mpi3mr_bsg_request(struct bsg_job *job)
1449 {
1450 	long rval = -EINVAL;
1451 	unsigned int reply_payload_rcv_len = 0;
1452 
1453 	struct mpi3mr_bsg_packet *bsg_req = job->request;
1454 
1455 	switch (bsg_req->cmd_type) {
1456 	case MPI3MR_DRV_CMD:
1457 		rval = mpi3mr_bsg_process_drv_cmds(job);
1458 		break;
1459 	case MPI3MR_MPT_CMD:
1460 		rval = mpi3mr_bsg_process_mpt_cmds(job, &reply_payload_rcv_len);
1461 		break;
1462 	default:
1463 		pr_err("%s: unsupported BSG command(0x%08x)\n",
1464 		    MPI3MR_DRIVER_NAME, bsg_req->cmd_type);
1465 		break;
1466 	}
1467 
1468 	bsg_job_done(job, rval, reply_payload_rcv_len);
1469 
1470 	return 0;
1471 }
1472 
1473 /**
1474  * mpi3mr_bsg_exit - de-registration from bsg layer
1475  * @mrioc: Adapter instance reference
1476  *
1477  * This will be called during driver unload and all
1478  * bsg resources allocated during load will be freed.
1479  *
1480  * Return:Nothing
1481  */
1482 void mpi3mr_bsg_exit(struct mpi3mr_ioc *mrioc)
1483 {
1484 	struct device *bsg_dev = &mrioc->bsg_dev;
1485 	if (!mrioc->bsg_queue)
1486 		return;
1487 
1488 	bsg_remove_queue(mrioc->bsg_queue);
1489 	mrioc->bsg_queue = NULL;
1490 
1491 	device_del(bsg_dev);
1492 	put_device(bsg_dev);
1493 }
1494 
1495 /**
1496  * mpi3mr_bsg_node_release -release bsg device node
1497  * @dev: bsg device node
1498  *
1499  * decrements bsg dev parent reference count
1500  *
1501  * Return:Nothing
1502  */
1503 static void mpi3mr_bsg_node_release(struct device *dev)
1504 {
1505 	put_device(dev->parent);
1506 }
1507 
1508 /**
1509  * mpi3mr_bsg_init -  registration with bsg layer
1510  * @mrioc: Adapter instance reference
1511  *
1512  * This will be called during driver load and it will
1513  * register driver with bsg layer
1514  *
1515  * Return:Nothing
1516  */
1517 void mpi3mr_bsg_init(struct mpi3mr_ioc *mrioc)
1518 {
1519 	struct device *bsg_dev = &mrioc->bsg_dev;
1520 	struct device *parent = &mrioc->shost->shost_gendev;
1521 
1522 	device_initialize(bsg_dev);
1523 
1524 	bsg_dev->parent = get_device(parent);
1525 	bsg_dev->release = mpi3mr_bsg_node_release;
1526 
1527 	dev_set_name(bsg_dev, "mpi3mrctl%u", mrioc->id);
1528 
1529 	if (device_add(bsg_dev)) {
1530 		ioc_err(mrioc, "%s: bsg device add failed\n",
1531 		    dev_name(bsg_dev));
1532 		put_device(bsg_dev);
1533 		return;
1534 	}
1535 
1536 	mrioc->bsg_queue = bsg_setup_queue(bsg_dev, dev_name(bsg_dev),
1537 			mpi3mr_bsg_request, NULL, 0);
1538 	if (IS_ERR(mrioc->bsg_queue)) {
1539 		ioc_err(mrioc, "%s: bsg registration failed\n",
1540 		    dev_name(bsg_dev));
1541 		device_del(bsg_dev);
1542 		put_device(bsg_dev);
1543 		return;
1544 	}
1545 
1546 	blk_queue_max_segments(mrioc->bsg_queue, MPI3MR_MAX_APP_XFER_SEGMENTS);
1547 	blk_queue_max_hw_sectors(mrioc->bsg_queue, MPI3MR_MAX_APP_XFER_SECTORS);
1548 
1549 	return;
1550 }
1551 
1552 /**
1553  * version_fw_show - SysFS callback for firmware version read
1554  * @dev: class device
1555  * @attr: Device attributes
1556  * @buf: Buffer to copy
1557  *
1558  * Return: sysfs_emit() return after copying firmware version
1559  */
1560 static ssize_t
1561 version_fw_show(struct device *dev, struct device_attribute *attr,
1562 	char *buf)
1563 {
1564 	struct Scsi_Host *shost = class_to_shost(dev);
1565 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1566 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
1567 
1568 	return sysfs_emit(buf, "%d.%d.%d.%d.%05d-%05d\n",
1569 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
1570 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
1571 }
1572 static DEVICE_ATTR_RO(version_fw);
1573 
1574 /**
1575  * fw_queue_depth_show - SysFS callback for firmware max cmds
1576  * @dev: class device
1577  * @attr: Device attributes
1578  * @buf: Buffer to copy
1579  *
1580  * Return: sysfs_emit() return after copying firmware max commands
1581  */
1582 static ssize_t
1583 fw_queue_depth_show(struct device *dev, struct device_attribute *attr,
1584 			char *buf)
1585 {
1586 	struct Scsi_Host *shost = class_to_shost(dev);
1587 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1588 
1589 	return sysfs_emit(buf, "%d\n", mrioc->facts.max_reqs);
1590 }
1591 static DEVICE_ATTR_RO(fw_queue_depth);
1592 
1593 /**
1594  * op_req_q_count_show - SysFS callback for request queue count
1595  * @dev: class device
1596  * @attr: Device attributes
1597  * @buf: Buffer to copy
1598  *
1599  * Return: sysfs_emit() return after copying request queue count
1600  */
1601 static ssize_t
1602 op_req_q_count_show(struct device *dev, struct device_attribute *attr,
1603 			char *buf)
1604 {
1605 	struct Scsi_Host *shost = class_to_shost(dev);
1606 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1607 
1608 	return sysfs_emit(buf, "%d\n", mrioc->num_op_req_q);
1609 }
1610 static DEVICE_ATTR_RO(op_req_q_count);
1611 
1612 /**
1613  * reply_queue_count_show - SysFS callback for reply queue count
1614  * @dev: class device
1615  * @attr: Device attributes
1616  * @buf: Buffer to copy
1617  *
1618  * Return: sysfs_emit() return after copying reply queue count
1619  */
1620 static ssize_t
1621 reply_queue_count_show(struct device *dev, struct device_attribute *attr,
1622 			char *buf)
1623 {
1624 	struct Scsi_Host *shost = class_to_shost(dev);
1625 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1626 
1627 	return sysfs_emit(buf, "%d\n", mrioc->num_op_reply_q);
1628 }
1629 
1630 static DEVICE_ATTR_RO(reply_queue_count);
1631 
1632 /**
1633  * logging_level_show - Show controller debug level
1634  * @dev: class device
1635  * @attr: Device attributes
1636  * @buf: Buffer to copy
1637  *
1638  * A sysfs 'read/write' shost attribute, to show the current
1639  * debug log level used by the driver for the specific
1640  * controller.
1641  *
1642  * Return: sysfs_emit() return
1643  */
1644 static ssize_t
1645 logging_level_show(struct device *dev,
1646 	struct device_attribute *attr, char *buf)
1647 
1648 {
1649 	struct Scsi_Host *shost = class_to_shost(dev);
1650 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1651 
1652 	return sysfs_emit(buf, "%08xh\n", mrioc->logging_level);
1653 }
1654 
1655 /**
1656  * logging_level_store- Change controller debug level
1657  * @dev: class device
1658  * @attr: Device attributes
1659  * @buf: Buffer to copy
1660  * @count: size of the buffer
1661  *
1662  * A sysfs 'read/write' shost attribute, to change the current
1663  * debug log level used by the driver for the specific
1664  * controller.
1665  *
1666  * Return: strlen() return
1667  */
1668 static ssize_t
1669 logging_level_store(struct device *dev,
1670 	struct device_attribute *attr,
1671 	const char *buf, size_t count)
1672 {
1673 	struct Scsi_Host *shost = class_to_shost(dev);
1674 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1675 	int val = 0;
1676 
1677 	if (kstrtoint(buf, 0, &val) != 0)
1678 		return -EINVAL;
1679 
1680 	mrioc->logging_level = val;
1681 	ioc_info(mrioc, "logging_level=%08xh\n", mrioc->logging_level);
1682 	return strlen(buf);
1683 }
1684 static DEVICE_ATTR_RW(logging_level);
1685 
1686 /**
1687  * adp_state_show() - SysFS callback for adapter state show
1688  * @dev: class device
1689  * @attr: Device attributes
1690  * @buf: Buffer to copy
1691  *
1692  * Return: sysfs_emit() return after copying adapter state
1693  */
1694 static ssize_t
1695 adp_state_show(struct device *dev, struct device_attribute *attr,
1696 	char *buf)
1697 {
1698 	struct Scsi_Host *shost = class_to_shost(dev);
1699 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
1700 	enum mpi3mr_iocstate ioc_state;
1701 	uint8_t adp_state;
1702 
1703 	ioc_state = mpi3mr_get_iocstate(mrioc);
1704 	if (ioc_state == MRIOC_STATE_UNRECOVERABLE)
1705 		adp_state = MPI3MR_BSG_ADPSTATE_UNRECOVERABLE;
1706 	else if ((mrioc->reset_in_progress) || (mrioc->stop_bsgs))
1707 		adp_state = MPI3MR_BSG_ADPSTATE_IN_RESET;
1708 	else if (ioc_state == MRIOC_STATE_FAULT)
1709 		adp_state = MPI3MR_BSG_ADPSTATE_FAULT;
1710 	else
1711 		adp_state = MPI3MR_BSG_ADPSTATE_OPERATIONAL;
1712 
1713 	return sysfs_emit(buf, "%u\n", adp_state);
1714 }
1715 
1716 static DEVICE_ATTR_RO(adp_state);
1717 
1718 static struct attribute *mpi3mr_host_attrs[] = {
1719 	&dev_attr_version_fw.attr,
1720 	&dev_attr_fw_queue_depth.attr,
1721 	&dev_attr_op_req_q_count.attr,
1722 	&dev_attr_reply_queue_count.attr,
1723 	&dev_attr_logging_level.attr,
1724 	&dev_attr_adp_state.attr,
1725 	NULL,
1726 };
1727 
1728 static const struct attribute_group mpi3mr_host_attr_group = {
1729 	.attrs = mpi3mr_host_attrs
1730 };
1731 
1732 const struct attribute_group *mpi3mr_host_groups[] = {
1733 	&mpi3mr_host_attr_group,
1734 	NULL,
1735 };
1736 
1737 
1738 /*
1739  * SCSI Device attributes under sysfs
1740  */
1741 
1742 /**
1743  * sas_address_show - SysFS callback for dev SASaddress display
1744  * @dev: class device
1745  * @attr: Device attributes
1746  * @buf: Buffer to copy
1747  *
1748  * Return: sysfs_emit() return after copying SAS address of the
1749  * specific SAS/SATA end device.
1750  */
1751 static ssize_t
1752 sas_address_show(struct device *dev, struct device_attribute *attr,
1753 			char *buf)
1754 {
1755 	struct scsi_device *sdev = to_scsi_device(dev);
1756 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
1757 	struct mpi3mr_stgt_priv_data *tgt_priv_data;
1758 	struct mpi3mr_tgt_dev *tgtdev;
1759 
1760 	sdev_priv_data = sdev->hostdata;
1761 	if (!sdev_priv_data)
1762 		return 0;
1763 
1764 	tgt_priv_data = sdev_priv_data->tgt_priv_data;
1765 	if (!tgt_priv_data)
1766 		return 0;
1767 	tgtdev = tgt_priv_data->tgt_dev;
1768 	if (!tgtdev || tgtdev->dev_type != MPI3_DEVICE_DEVFORM_SAS_SATA)
1769 		return 0;
1770 	return sysfs_emit(buf, "0x%016llx\n",
1771 	    (unsigned long long)tgtdev->dev_spec.sas_sata_inf.sas_address);
1772 }
1773 
1774 static DEVICE_ATTR_RO(sas_address);
1775 
1776 /**
1777  * device_handle_show - SysFS callback for device handle display
1778  * @dev: class device
1779  * @attr: Device attributes
1780  * @buf: Buffer to copy
1781  *
1782  * Return: sysfs_emit() return after copying firmware internal
1783  * device handle of the specific device.
1784  */
1785 static ssize_t
1786 device_handle_show(struct device *dev, struct device_attribute *attr,
1787 			char *buf)
1788 {
1789 	struct scsi_device *sdev = to_scsi_device(dev);
1790 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
1791 	struct mpi3mr_stgt_priv_data *tgt_priv_data;
1792 	struct mpi3mr_tgt_dev *tgtdev;
1793 
1794 	sdev_priv_data = sdev->hostdata;
1795 	if (!sdev_priv_data)
1796 		return 0;
1797 
1798 	tgt_priv_data = sdev_priv_data->tgt_priv_data;
1799 	if (!tgt_priv_data)
1800 		return 0;
1801 	tgtdev = tgt_priv_data->tgt_dev;
1802 	if (!tgtdev)
1803 		return 0;
1804 	return sysfs_emit(buf, "0x%04x\n", tgtdev->dev_handle);
1805 }
1806 
1807 static DEVICE_ATTR_RO(device_handle);
1808 
1809 /**
1810  * persistent_id_show - SysFS callback for persisten ID display
1811  * @dev: class device
1812  * @attr: Device attributes
1813  * @buf: Buffer to copy
1814  *
1815  * Return: sysfs_emit() return after copying persistent ID of the
1816  * of the specific device.
1817  */
1818 static ssize_t
1819 persistent_id_show(struct device *dev, struct device_attribute *attr,
1820 			char *buf)
1821 {
1822 	struct scsi_device *sdev = to_scsi_device(dev);
1823 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
1824 	struct mpi3mr_stgt_priv_data *tgt_priv_data;
1825 	struct mpi3mr_tgt_dev *tgtdev;
1826 
1827 	sdev_priv_data = sdev->hostdata;
1828 	if (!sdev_priv_data)
1829 		return 0;
1830 
1831 	tgt_priv_data = sdev_priv_data->tgt_priv_data;
1832 	if (!tgt_priv_data)
1833 		return 0;
1834 	tgtdev = tgt_priv_data->tgt_dev;
1835 	if (!tgtdev)
1836 		return 0;
1837 	return sysfs_emit(buf, "%d\n", tgtdev->perst_id);
1838 }
1839 static DEVICE_ATTR_RO(persistent_id);
1840 
1841 static struct attribute *mpi3mr_dev_attrs[] = {
1842 	&dev_attr_sas_address.attr,
1843 	&dev_attr_device_handle.attr,
1844 	&dev_attr_persistent_id.attr,
1845 	NULL,
1846 };
1847 
1848 static const struct attribute_group mpi3mr_dev_attr_group = {
1849 	.attrs = mpi3mr_dev_attrs
1850 };
1851 
1852 const struct attribute_group *mpi3mr_dev_groups[] = {
1853 	&mpi3mr_dev_attr_group,
1854 	NULL,
1855 };
1856