xref: /linux/drivers/scsi/mpi3mr/mpi3mr_os.c (revision 08dbfad3f5040f5bdb6c529da20d6d4e81fefd72)
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/idr.h>
12 
13 /* global driver scop variables */
14 LIST_HEAD(mrioc_list);
15 DEFINE_SPINLOCK(mrioc_list_lock);
16 static DEFINE_IDA(mrioc_ida);
17 static int warn_non_secure_ctlr;
18 atomic64_t event_counter;
19 
20 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
21 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
22 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
23 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
24 
25 /* Module parameters*/
26 int prot_mask = -1;
27 module_param(prot_mask, int, 0);
28 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
29 
30 static int prot_guard_mask = 3;
31 module_param(prot_guard_mask, int, 0);
32 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
33 static int logging_level;
34 module_param(logging_level, int, 0);
35 MODULE_PARM_DESC(logging_level,
36 	" bits for enabling additional logging info (default=0)");
37 static int max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
38 module_param(max_sgl_entries, int, 0444);
39 MODULE_PARM_DESC(max_sgl_entries,
40 	"Preferred max number of SG entries to be used for a single I/O\n"
41 	"The actual value will be determined by the driver\n"
42 	"(Minimum=256, Maximum=2048, default=256)");
43 
44 /* Forward declarations*/
45 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
46 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
47 
48 #define MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION	(0xFFFF)
49 
50 #define MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH	(0xFFFE)
51 
52 /*
53  * SAS Log info code for a NCQ collateral abort after an NCQ error:
54  * IOC_LOGINFO_PREFIX_PL | PL_LOGINFO_CODE_SATA_NCQ_FAIL_ALL_CMDS_AFTR_ERR
55  * See: drivers/message/fusion/lsi/mpi_log_sas.h
56  */
57 #define IOC_LOGINFO_SATA_NCQ_FAIL_AFTER_ERR	0x31080000
58 
59 /**
60  * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
61  * @mrioc: Adapter instance reference
62  * @scmd: SCSI command reference
63  *
64  * Calculate the host tag based on block tag for a given scmd.
65  *
66  * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
67  */
mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd)68 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
69 	struct scsi_cmnd *scmd)
70 {
71 	struct scmd_priv *priv = NULL;
72 	u32 unique_tag;
73 	u16 host_tag, hw_queue;
74 
75 	unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
76 
77 	hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
78 	if (hw_queue >= mrioc->num_op_reply_q)
79 		return MPI3MR_HOSTTAG_INVALID;
80 	host_tag = blk_mq_unique_tag_to_tag(unique_tag);
81 
82 	if (WARN_ON(host_tag >= mrioc->max_host_ios))
83 		return MPI3MR_HOSTTAG_INVALID;
84 
85 	priv = scsi_cmd_priv(scmd);
86 	/*host_tag 0 is invalid hence incrementing by 1*/
87 	priv->host_tag = host_tag + 1;
88 	priv->scmd = scmd;
89 	priv->in_lld_scope = 1;
90 	priv->req_q_idx = hw_queue;
91 	priv->meta_chain_idx = -1;
92 	priv->chain_idx = -1;
93 	priv->meta_sg_valid = 0;
94 	return priv->host_tag;
95 }
96 
97 /**
98  * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
99  * @mrioc: Adapter instance reference
100  * @host_tag: Host tag
101  * @qidx: Operational queue index
102  *
103  * Identify the block tag from the host tag and queue index and
104  * retrieve associated scsi command using scsi_host_find_tag().
105  *
106  * Return: SCSI command reference or NULL.
107  */
mpi3mr_scmd_from_host_tag(struct mpi3mr_ioc * mrioc,u16 host_tag,u16 qidx)108 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
109 	struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
110 {
111 	struct scsi_cmnd *scmd = NULL;
112 	struct scmd_priv *priv = NULL;
113 	u32 unique_tag = host_tag - 1;
114 
115 	if (WARN_ON(host_tag > mrioc->max_host_ios))
116 		goto out;
117 
118 	unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
119 
120 	scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
121 	if (scmd) {
122 		priv = scsi_cmd_priv(scmd);
123 		if (!priv->in_lld_scope)
124 			scmd = NULL;
125 	}
126 out:
127 	return scmd;
128 }
129 
130 /**
131  * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
132  * @mrioc: Adapter instance reference
133  * @scmd: SCSI command reference
134  *
135  * Invalidate the SCSI command private data to mark the command
136  * is not in LLD scope anymore.
137  *
138  * Return: Nothing.
139  */
mpi3mr_clear_scmd_priv(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd)140 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
141 	struct scsi_cmnd *scmd)
142 {
143 	struct scmd_priv *priv = NULL;
144 
145 	priv = scsi_cmd_priv(scmd);
146 
147 	if (WARN_ON(priv->in_lld_scope == 0))
148 		return;
149 	priv->host_tag = MPI3MR_HOSTTAG_INVALID;
150 	priv->req_q_idx = 0xFFFF;
151 	priv->scmd = NULL;
152 	priv->in_lld_scope = 0;
153 	priv->meta_sg_valid = 0;
154 	if (priv->chain_idx >= 0) {
155 		clear_bit(priv->chain_idx, mrioc->chain_bitmap);
156 		priv->chain_idx = -1;
157 	}
158 	if (priv->meta_chain_idx >= 0) {
159 		clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
160 		priv->meta_chain_idx = -1;
161 	}
162 }
163 
164 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
165 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
166 static void mpi3mr_fwevt_worker(struct work_struct *work);
167 
168 /**
169  * mpi3mr_fwevt_free - firmware event memory dealloctor
170  * @r: k reference pointer of the firmware event
171  *
172  * Free firmware event memory when no reference.
173  */
mpi3mr_fwevt_free(struct kref * r)174 static void mpi3mr_fwevt_free(struct kref *r)
175 {
176 	kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
177 }
178 
179 /**
180  * mpi3mr_fwevt_get - k reference incrementor
181  * @fwevt: Firmware event reference
182  *
183  * Increment firmware event reference count.
184  */
mpi3mr_fwevt_get(struct mpi3mr_fwevt * fwevt)185 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
186 {
187 	kref_get(&fwevt->ref_count);
188 }
189 
190 /**
191  * mpi3mr_fwevt_put - k reference decrementor
192  * @fwevt: Firmware event reference
193  *
194  * decrement firmware event reference count.
195  */
mpi3mr_fwevt_put(struct mpi3mr_fwevt * fwevt)196 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
197 {
198 	kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
199 }
200 
201 /**
202  * mpi3mr_alloc_fwevt - Allocate firmware event
203  * @len: length of firmware event data to allocate
204  *
205  * Allocate firmware event with required length and initialize
206  * the reference counter.
207  *
208  * Return: firmware event reference.
209  */
mpi3mr_alloc_fwevt(int len)210 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
211 {
212 	struct mpi3mr_fwevt *fwevt;
213 
214 	fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
215 	if (!fwevt)
216 		return NULL;
217 
218 	kref_init(&fwevt->ref_count);
219 	return fwevt;
220 }
221 
222 /**
223  * mpi3mr_fwevt_add_to_list - Add firmware event to the list
224  * @mrioc: Adapter instance reference
225  * @fwevt: Firmware event reference
226  *
227  * Add the given firmware event to the firmware event list.
228  *
229  * Return: Nothing.
230  */
mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)231 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
232 	struct mpi3mr_fwevt *fwevt)
233 {
234 	unsigned long flags;
235 
236 	if (!mrioc->fwevt_worker_thread)
237 		return;
238 
239 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
240 	/* get fwevt reference count while adding it to fwevt_list */
241 	mpi3mr_fwevt_get(fwevt);
242 	INIT_LIST_HEAD(&fwevt->list);
243 	list_add_tail(&fwevt->list, &mrioc->fwevt_list);
244 	INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
245 	/* get fwevt reference count while enqueueing it to worker queue */
246 	mpi3mr_fwevt_get(fwevt);
247 	queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
248 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
249 }
250 
251 /**
252  * mpi3mr_hdb_trigger_data_event - Add hdb trigger data event to
253  * the list
254  * @mrioc: Adapter instance reference
255  * @event_data: Event data
256  *
257  * Add the given hdb trigger data event to the firmware event
258  * list.
259  *
260  * Return: Nothing.
261  */
mpi3mr_hdb_trigger_data_event(struct mpi3mr_ioc * mrioc,struct trigger_event_data * event_data)262 void mpi3mr_hdb_trigger_data_event(struct mpi3mr_ioc *mrioc,
263 	struct trigger_event_data *event_data)
264 {
265 	struct mpi3mr_fwevt *fwevt;
266 	u16 sz = sizeof(*event_data);
267 
268 	fwevt = mpi3mr_alloc_fwevt(sz);
269 	if (!fwevt) {
270 		ioc_warn(mrioc, "failed to queue hdb trigger data event\n");
271 		return;
272 	}
273 
274 	fwevt->mrioc = mrioc;
275 	fwevt->event_id = MPI3MR_DRIVER_EVENT_PROCESS_TRIGGER;
276 	fwevt->send_ack = 0;
277 	fwevt->process_evt = 1;
278 	fwevt->evt_ctx = 0;
279 	fwevt->event_data_size = sz;
280 	memcpy(fwevt->event_data, event_data, sz);
281 
282 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
283 }
284 
285 /**
286  * mpi3mr_fwevt_del_from_list - Delete firmware event from list
287  * @mrioc: Adapter instance reference
288  * @fwevt: Firmware event reference
289  *
290  * Delete the given firmware event from the firmware event list.
291  *
292  * Return: Nothing.
293  */
mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)294 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
295 	struct mpi3mr_fwevt *fwevt)
296 {
297 	unsigned long flags;
298 
299 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
300 	if (!list_empty(&fwevt->list)) {
301 		list_del_init(&fwevt->list);
302 		/*
303 		 * Put fwevt reference count after
304 		 * removing it from fwevt_list
305 		 */
306 		mpi3mr_fwevt_put(fwevt);
307 	}
308 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
309 }
310 
311 /**
312  * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
313  * @mrioc: Adapter instance reference
314  *
315  * Dequeue a firmware event from the firmware event list.
316  *
317  * Return: firmware event.
318  */
mpi3mr_dequeue_fwevt(struct mpi3mr_ioc * mrioc)319 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
320 	struct mpi3mr_ioc *mrioc)
321 {
322 	unsigned long flags;
323 	struct mpi3mr_fwevt *fwevt = NULL;
324 
325 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
326 	if (!list_empty(&mrioc->fwevt_list)) {
327 		fwevt = list_first_entry(&mrioc->fwevt_list,
328 		    struct mpi3mr_fwevt, list);
329 		list_del_init(&fwevt->list);
330 		/*
331 		 * Put fwevt reference count after
332 		 * removing it from fwevt_list
333 		 */
334 		mpi3mr_fwevt_put(fwevt);
335 	}
336 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
337 
338 	return fwevt;
339 }
340 
341 /**
342  * mpi3mr_cancel_work - cancel firmware event
343  * @fwevt: fwevt object which needs to be canceled
344  *
345  * Return: Nothing.
346  */
mpi3mr_cancel_work(struct mpi3mr_fwevt * fwevt)347 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
348 {
349 	/*
350 	 * Wait on the fwevt to complete. If this returns 1, then
351 	 * the event was never executed.
352 	 *
353 	 * If it did execute, we wait for it to finish, and the put will
354 	 * happen from mpi3mr_process_fwevt()
355 	 */
356 	if (cancel_work_sync(&fwevt->work)) {
357 		/*
358 		 * Put fwevt reference count after
359 		 * dequeuing it from worker queue
360 		 */
361 		mpi3mr_fwevt_put(fwevt);
362 		/*
363 		 * Put fwevt reference count to neutralize
364 		 * kref_init increment
365 		 */
366 		mpi3mr_fwevt_put(fwevt);
367 	}
368 }
369 
370 /**
371  * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
372  * @mrioc: Adapter instance reference
373  *
374  * Flush all pending firmware events from the firmware event
375  * list.
376  *
377  * Return: Nothing.
378  */
mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc * mrioc)379 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
380 {
381 	struct mpi3mr_fwevt *fwevt = NULL;
382 
383 	if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
384 	    !mrioc->fwevt_worker_thread)
385 		return;
386 
387 	while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
388 		mpi3mr_cancel_work(fwevt);
389 
390 	if (mrioc->current_event) {
391 		fwevt = mrioc->current_event;
392 		/*
393 		 * Don't call cancel_work_sync() API for the
394 		 * fwevt work if the controller reset is
395 		 * get called as part of processing the
396 		 * same fwevt work (or) when worker thread is
397 		 * waiting for device add/remove APIs to complete.
398 		 * Otherwise we will see deadlock.
399 		 */
400 		if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
401 			fwevt->discard = 1;
402 			return;
403 		}
404 
405 		mpi3mr_cancel_work(fwevt);
406 	}
407 }
408 
409 /**
410  * mpi3mr_queue_qd_reduction_event - Queue TG QD reduction event
411  * @mrioc: Adapter instance reference
412  * @tg: Throttle group information pointer
413  *
414  * Accessor to queue on synthetically generated driver event to
415  * the event worker thread, the driver event will be used to
416  * reduce the QD of all VDs in the TG from the worker thread.
417  *
418  * Return: None.
419  */
mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc * mrioc,struct mpi3mr_throttle_group_info * tg)420 static void mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc *mrioc,
421 	struct mpi3mr_throttle_group_info *tg)
422 {
423 	struct mpi3mr_fwevt *fwevt;
424 	u16 sz = sizeof(struct mpi3mr_throttle_group_info *);
425 
426 	/*
427 	 * If the QD reduction event is already queued due to throttle and if
428 	 * the QD is not restored through device info change event
429 	 * then dont queue further reduction events
430 	 */
431 	if (tg->fw_qd != tg->modified_qd)
432 		return;
433 
434 	fwevt = mpi3mr_alloc_fwevt(sz);
435 	if (!fwevt) {
436 		ioc_warn(mrioc, "failed to queue TG QD reduction event\n");
437 		return;
438 	}
439 	*(struct mpi3mr_throttle_group_info **)fwevt->event_data = tg;
440 	fwevt->mrioc = mrioc;
441 	fwevt->event_id = MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION;
442 	fwevt->send_ack = 0;
443 	fwevt->process_evt = 1;
444 	fwevt->evt_ctx = 0;
445 	fwevt->event_data_size = sz;
446 	tg->modified_qd = max_t(u16, (tg->fw_qd * tg->qd_reduction) / 10, 8);
447 
448 	dprint_event_bh(mrioc, "qd reduction event queued for tg_id(%d)\n",
449 	    tg->id);
450 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
451 }
452 
453 /**
454  * mpi3mr_invalidate_devhandles -Invalidate device handles
455  * @mrioc: Adapter instance reference
456  *
457  * Invalidate the device handles in the target device structures
458  * . Called post reset prior to reinitializing the controller.
459  *
460  * Return: Nothing.
461  */
mpi3mr_invalidate_devhandles(struct mpi3mr_ioc * mrioc)462 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
463 {
464 	struct mpi3mr_tgt_dev *tgtdev;
465 	struct mpi3mr_stgt_priv_data *tgt_priv;
466 
467 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
468 		tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
469 		if (tgtdev->starget && tgtdev->starget->hostdata) {
470 			tgt_priv = tgtdev->starget->hostdata;
471 			tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
472 			tgt_priv->io_throttle_enabled = 0;
473 			tgt_priv->io_divert = 0;
474 			tgt_priv->throttle_group = NULL;
475 			tgt_priv->wslen = 0;
476 			if (tgtdev->host_exposed)
477 				atomic_set(&tgt_priv->block_io, 1);
478 		}
479 	}
480 }
481 
482 /**
483  * mpi3mr_print_scmd - print individual SCSI command
484  * @rq: Block request
485  * @data: Adapter instance reference
486  *
487  * Print the SCSI command details if it is in LLD scope.
488  *
489  * Return: true always.
490  */
mpi3mr_print_scmd(struct request * rq,void * data)491 static bool mpi3mr_print_scmd(struct request *rq, void *data)
492 {
493 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
494 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
495 	struct scmd_priv *priv = NULL;
496 
497 	if (scmd) {
498 		priv = scsi_cmd_priv(scmd);
499 		if (!priv->in_lld_scope)
500 			goto out;
501 
502 		ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
503 		    __func__, priv->host_tag, priv->req_q_idx + 1);
504 		scsi_print_command(scmd);
505 	}
506 
507 out:
508 	return(true);
509 }
510 
511 /**
512  * mpi3mr_flush_scmd - Flush individual SCSI command
513  * @rq: Block request
514  * @data: Adapter instance reference
515  *
516  * Return the SCSI command to the upper layers if it is in LLD
517  * scope.
518  *
519  * Return: true always.
520  */
521 
mpi3mr_flush_scmd(struct request * rq,void * data)522 static bool mpi3mr_flush_scmd(struct request *rq, void *data)
523 {
524 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
525 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
526 	struct scmd_priv *priv = NULL;
527 
528 	if (scmd) {
529 		priv = scsi_cmd_priv(scmd);
530 		if (!priv->in_lld_scope)
531 			goto out;
532 
533 		if (priv->meta_sg_valid)
534 			dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
535 			    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
536 		mpi3mr_clear_scmd_priv(mrioc, scmd);
537 		scsi_dma_unmap(scmd);
538 		scmd->result = DID_RESET << 16;
539 		scsi_print_command(scmd);
540 		scsi_done(scmd);
541 		mrioc->flush_io_count++;
542 	}
543 
544 out:
545 	return(true);
546 }
547 
548 /**
549  * mpi3mr_count_dev_pending - Count commands pending for a lun
550  * @rq: Block request
551  * @data: SCSI device reference
552  *
553  * This is an iterator function called for each SCSI command in
554  * a host and if the command is pending in the LLD for the
555  * specific device(lun) then device specific pending I/O counter
556  * is updated in the device structure.
557  *
558  * Return: true always.
559  */
560 
mpi3mr_count_dev_pending(struct request * rq,void * data)561 static bool mpi3mr_count_dev_pending(struct request *rq, void *data)
562 {
563 	struct scsi_device *sdev = (struct scsi_device *)data;
564 	struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
565 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
566 	struct scmd_priv *priv;
567 
568 	if (scmd) {
569 		priv = scsi_cmd_priv(scmd);
570 		if (!priv->in_lld_scope)
571 			goto out;
572 		if (scmd->device == sdev)
573 			sdev_priv_data->pend_count++;
574 	}
575 
576 out:
577 	return true;
578 }
579 
580 /**
581  * mpi3mr_count_tgt_pending - Count commands pending for target
582  * @rq: Block request
583  * @data: SCSI target reference
584  *
585  * This is an iterator function called for each SCSI command in
586  * a host and if the command is pending in the LLD for the
587  * specific target then target specific pending I/O counter is
588  * updated in the target structure.
589  *
590  * Return: true always.
591  */
592 
mpi3mr_count_tgt_pending(struct request * rq,void * data)593 static bool mpi3mr_count_tgt_pending(struct request *rq, void *data)
594 {
595 	struct scsi_target *starget = (struct scsi_target *)data;
596 	struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
597 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
598 	struct scmd_priv *priv;
599 
600 	if (scmd) {
601 		priv = scsi_cmd_priv(scmd);
602 		if (!priv->in_lld_scope)
603 			goto out;
604 		if (scmd->device && (scsi_target(scmd->device) == starget))
605 			stgt_priv_data->pend_count++;
606 	}
607 
608 out:
609 	return true;
610 }
611 
612 /**
613  * mpi3mr_flush_host_io -  Flush host I/Os
614  * @mrioc: Adapter instance reference
615  *
616  * Flush all of the pending I/Os by calling
617  * blk_mq_tagset_busy_iter() for each possible tag. This is
618  * executed post controller reset
619  *
620  * Return: Nothing.
621  */
mpi3mr_flush_host_io(struct mpi3mr_ioc * mrioc)622 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
623 {
624 	struct Scsi_Host *shost = mrioc->shost;
625 
626 	mrioc->flush_io_count = 0;
627 	ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
628 	blk_mq_tagset_busy_iter(&shost->tag_set,
629 	    mpi3mr_flush_scmd, (void *)mrioc);
630 	ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
631 	    mrioc->flush_io_count);
632 }
633 
634 /**
635  * mpi3mr_flush_cmds_for_unrecovered_controller - Flush all pending cmds
636  * @mrioc: Adapter instance reference
637  *
638  * This function waits for currently running IO poll threads to
639  * exit and then flushes all host I/Os and any internal pending
640  * cmds. This is executed after controller is marked as
641  * unrecoverable.
642  *
643  * Return: Nothing.
644  */
mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc * mrioc)645 void mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc *mrioc)
646 {
647 	struct Scsi_Host *shost = mrioc->shost;
648 	int i;
649 
650 	if (!mrioc->unrecoverable)
651 		return;
652 
653 	if (mrioc->op_reply_qinfo) {
654 		for (i = 0; i < mrioc->num_queues; i++) {
655 			while (atomic_read(&mrioc->op_reply_qinfo[i].in_use))
656 				udelay(500);
657 			atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
658 		}
659 	}
660 	mrioc->flush_io_count = 0;
661 	blk_mq_tagset_busy_iter(&shost->tag_set,
662 	    mpi3mr_flush_scmd, (void *)mrioc);
663 	mpi3mr_flush_delayed_cmd_lists(mrioc);
664 	mpi3mr_flush_drv_cmds(mrioc);
665 }
666 
667 /**
668  * mpi3mr_alloc_tgtdev - target device allocator
669  *
670  * Allocate target device instance and initialize the reference
671  * count
672  *
673  * Return: target device instance.
674  */
mpi3mr_alloc_tgtdev(void)675 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
676 {
677 	struct mpi3mr_tgt_dev *tgtdev;
678 
679 	tgtdev = kzalloc_obj(*tgtdev, GFP_ATOMIC);
680 	if (!tgtdev)
681 		return NULL;
682 	kref_init(&tgtdev->ref_count);
683 	return tgtdev;
684 }
685 
686 /**
687  * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
688  * @mrioc: Adapter instance reference
689  * @tgtdev: Target device
690  *
691  * Add the target device to the target device list
692  *
693  * Return: Nothing.
694  */
mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev)695 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
696 	struct mpi3mr_tgt_dev *tgtdev)
697 {
698 	unsigned long flags;
699 
700 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
701 	mpi3mr_tgtdev_get(tgtdev);
702 	INIT_LIST_HEAD(&tgtdev->list);
703 	list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
704 	tgtdev->state = MPI3MR_DEV_CREATED;
705 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
706 }
707 
708 /**
709  * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
710  * @mrioc: Adapter instance reference
711  * @tgtdev: Target device
712  * @must_delete: Must delete the target device from the list irrespective
713  * of the device state.
714  *
715  * Remove the target device from the target device list
716  *
717  * Return: Nothing.
718  */
mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev,bool must_delete)719 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
720 	struct mpi3mr_tgt_dev *tgtdev, bool must_delete)
721 {
722 	unsigned long flags;
723 
724 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
725 	if ((tgtdev->state == MPI3MR_DEV_REMOVE_HS_STARTED) || (must_delete == true)) {
726 		if (!list_empty(&tgtdev->list)) {
727 			list_del_init(&tgtdev->list);
728 			tgtdev->state = MPI3MR_DEV_DELETED;
729 			mpi3mr_tgtdev_put(tgtdev);
730 		}
731 	}
732 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
733 }
734 
735 /**
736  * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
737  * @mrioc: Adapter instance reference
738  * @handle: Device handle
739  *
740  * Accessor to retrieve target device from the device handle.
741  * Non Lock version
742  *
743  * Return: Target device reference.
744  */
__mpi3mr_get_tgtdev_by_handle(struct mpi3mr_ioc * mrioc,u16 handle)745 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_handle(
746 	struct mpi3mr_ioc *mrioc, u16 handle)
747 {
748 	struct mpi3mr_tgt_dev *tgtdev;
749 
750 	assert_spin_locked(&mrioc->tgtdev_lock);
751 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
752 		if (tgtdev->dev_handle == handle)
753 			goto found_tgtdev;
754 	return NULL;
755 
756 found_tgtdev:
757 	mpi3mr_tgtdev_get(tgtdev);
758 	return tgtdev;
759 }
760 
761 /**
762  * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
763  * @mrioc: Adapter instance reference
764  * @handle: Device handle
765  *
766  * Accessor to retrieve target device from the device handle.
767  * Lock version
768  *
769  * Return: Target device reference.
770  */
mpi3mr_get_tgtdev_by_handle(struct mpi3mr_ioc * mrioc,u16 handle)771 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
772 	struct mpi3mr_ioc *mrioc, u16 handle)
773 {
774 	struct mpi3mr_tgt_dev *tgtdev;
775 	unsigned long flags;
776 
777 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
778 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
779 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
780 	return tgtdev;
781 }
782 
783 /**
784  * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
785  * @mrioc: Adapter instance reference
786  * @persist_id: Persistent ID
787  *
788  * Accessor to retrieve target device from the Persistent ID.
789  * Non Lock version
790  *
791  * Return: Target device reference.
792  */
__mpi3mr_get_tgtdev_by_perst_id(struct mpi3mr_ioc * mrioc,u16 persist_id)793 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_perst_id(
794 	struct mpi3mr_ioc *mrioc, u16 persist_id)
795 {
796 	struct mpi3mr_tgt_dev *tgtdev;
797 
798 	assert_spin_locked(&mrioc->tgtdev_lock);
799 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
800 		if (tgtdev->perst_id == persist_id)
801 			goto found_tgtdev;
802 	return NULL;
803 
804 found_tgtdev:
805 	mpi3mr_tgtdev_get(tgtdev);
806 	return tgtdev;
807 }
808 
809 /**
810  * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
811  * @mrioc: Adapter instance reference
812  * @persist_id: Persistent ID
813  *
814  * Accessor to retrieve target device from the Persistent ID.
815  * Lock version
816  *
817  * Return: Target device reference.
818  */
mpi3mr_get_tgtdev_by_perst_id(struct mpi3mr_ioc * mrioc,u16 persist_id)819 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
820 	struct mpi3mr_ioc *mrioc, u16 persist_id)
821 {
822 	struct mpi3mr_tgt_dev *tgtdev;
823 	unsigned long flags;
824 
825 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
826 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
827 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
828 	return tgtdev;
829 }
830 
831 /**
832  * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
833  * @mrioc: Adapter instance reference
834  * @tgt_priv: Target private data
835  *
836  * Accessor to return target device from the target private
837  * data. Non Lock version
838  *
839  * Return: Target device reference.
840  */
__mpi3mr_get_tgtdev_from_tgtpriv(struct mpi3mr_ioc * mrioc,struct mpi3mr_stgt_priv_data * tgt_priv)841 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_from_tgtpriv(
842 	struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
843 {
844 	struct mpi3mr_tgt_dev *tgtdev;
845 
846 	assert_spin_locked(&mrioc->tgtdev_lock);
847 	tgtdev = tgt_priv->tgt_dev;
848 	if (tgtdev)
849 		mpi3mr_tgtdev_get(tgtdev);
850 	return tgtdev;
851 }
852 
853 /**
854  * mpi3mr_set_io_divert_for_all_vd_in_tg -set divert for TG VDs
855  * @mrioc: Adapter instance reference
856  * @tg: Throttle group information pointer
857  * @divert_value: 1 or 0
858  *
859  * Accessor to set io_divert flag for each device associated
860  * with the given throttle group with the given value.
861  *
862  * Return: None.
863  */
mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc * mrioc,struct mpi3mr_throttle_group_info * tg,u8 divert_value)864 static void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
865 	struct mpi3mr_throttle_group_info *tg, u8 divert_value)
866 {
867 	unsigned long flags;
868 	struct mpi3mr_tgt_dev *tgtdev;
869 	struct mpi3mr_stgt_priv_data *tgt_priv;
870 
871 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
872 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
873 		if (tgtdev->starget && tgtdev->starget->hostdata) {
874 			tgt_priv = tgtdev->starget->hostdata;
875 			if (tgt_priv->throttle_group == tg)
876 				tgt_priv->io_divert = divert_value;
877 		}
878 	}
879 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
880 }
881 
882 /**
883  * mpi3mr_print_device_event_notice - print notice related to post processing of
884  *					device event after controller reset.
885  *
886  * @mrioc: Adapter instance reference
887  * @device_add: true for device add event and false for device removal event
888  *
889  * Return: None.
890  */
mpi3mr_print_device_event_notice(struct mpi3mr_ioc * mrioc,bool device_add)891 void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
892 	bool device_add)
893 {
894 	ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
895 	    (device_add ? "addition" : "removal"));
896 	ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
897 	ioc_notice(mrioc, "are matched with attached devices for correctness\n");
898 }
899 
900 /**
901  * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
902  * @mrioc: Adapter instance reference
903  * @tgtdev: Target device structure
904  *
905  * Checks whether the device is exposed to upper layers and if it
906  * is then remove the device from upper layers by calling
907  * scsi_remove_target().
908  *
909  * Return: 0 on success, non zero on failure.
910  */
mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev)911 void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
912 	struct mpi3mr_tgt_dev *tgtdev)
913 {
914 	struct mpi3mr_stgt_priv_data *tgt_priv;
915 
916 	ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
917 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
918 	if (tgtdev->starget && tgtdev->starget->hostdata) {
919 		tgt_priv = tgtdev->starget->hostdata;
920 		atomic_set(&tgt_priv->block_io, 0);
921 		tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
922 	}
923 
924 	if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
925 	    MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl) {
926 		if (tgtdev->starget) {
927 			if (mrioc->current_event)
928 				mrioc->current_event->pending_at_sml = 1;
929 			scsi_remove_target(&tgtdev->starget->dev);
930 			tgtdev->host_exposed = 0;
931 			if (mrioc->current_event) {
932 				mrioc->current_event->pending_at_sml = 0;
933 				if (mrioc->current_event->discard) {
934 					mpi3mr_print_device_event_notice(mrioc,
935 					    false);
936 					return;
937 				}
938 			}
939 		}
940 	} else
941 		mpi3mr_remove_tgtdev_from_sas_transport(mrioc, tgtdev);
942 	mpi3mr_global_trigger(mrioc,
943 	    MPI3_DRIVER2_GLOBALTRIGGER_DEVICE_REMOVAL_ENABLED);
944 
945 	ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
946 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
947 }
948 
949 /**
950  * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
951  * @mrioc: Adapter instance reference
952  * @perst_id: Persistent ID of the device
953  *
954  * Checks whether the device can be exposed to upper layers and
955  * if it is not then expose the device to upper layers by
956  * calling scsi_scan_target().
957  *
958  * Return: 0 on success, non zero on failure.
959  */
mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc * mrioc,u16 perst_id)960 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
961 	u16 perst_id)
962 {
963 	int retval = 0;
964 	struct mpi3mr_tgt_dev *tgtdev;
965 
966 	if (mrioc->reset_in_progress || mrioc->pci_err_recovery)
967 		return -1;
968 
969 	tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
970 	if (!tgtdev) {
971 		retval = -1;
972 		goto out;
973 	}
974 	if (tgtdev->is_hidden || tgtdev->host_exposed) {
975 		retval = -1;
976 		goto out;
977 	}
978 	if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
979 	    MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl){
980 		tgtdev->host_exposed = 1;
981 		if (mrioc->current_event)
982 			mrioc->current_event->pending_at_sml = 1;
983 		scsi_scan_target(&mrioc->shost->shost_gendev,
984 		    mrioc->scsi_device_channel, tgtdev->perst_id,
985 		    SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
986 		if (!tgtdev->starget)
987 			tgtdev->host_exposed = 0;
988 		if (mrioc->current_event) {
989 			mrioc->current_event->pending_at_sml = 0;
990 			if (mrioc->current_event->discard) {
991 				mpi3mr_print_device_event_notice(mrioc, true);
992 				goto out;
993 			}
994 		}
995 		dprint_event_bh(mrioc,
996 		    "exposed target device with handle(0x%04x), perst_id(%d)\n",
997 		    tgtdev->dev_handle, perst_id);
998 		goto out;
999 	} else
1000 		mpi3mr_report_tgtdev_to_sas_transport(mrioc, tgtdev);
1001 out:
1002 	if (tgtdev)
1003 		mpi3mr_tgtdev_put(tgtdev);
1004 
1005 	return retval;
1006 }
1007 
1008 /**
1009  * mpi3mr_change_queue_depth- Change QD callback handler
1010  * @sdev: SCSI device reference
1011  * @q_depth: Queue depth
1012  *
1013  * Validate and limit QD and call scsi_change_queue_depth.
1014  *
1015  * Return: return value of scsi_change_queue_depth
1016  */
mpi3mr_change_queue_depth(struct scsi_device * sdev,int q_depth)1017 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
1018 	int q_depth)
1019 {
1020 	struct scsi_target *starget = scsi_target(sdev);
1021 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1022 	int retval = 0;
1023 
1024 	if (!sdev->tagged_supported)
1025 		q_depth = 1;
1026 	if (q_depth > shost->can_queue)
1027 		q_depth = shost->can_queue;
1028 	else if (!q_depth)
1029 		q_depth = MPI3MR_DEFAULT_SDEV_QD;
1030 	retval = scsi_change_queue_depth(sdev, q_depth);
1031 	sdev->max_queue_depth = sdev->queue_depth;
1032 
1033 	return retval;
1034 }
1035 
mpi3mr_configure_nvme_dev(struct mpi3mr_tgt_dev * tgt_dev,struct queue_limits * lim)1036 static void mpi3mr_configure_nvme_dev(struct mpi3mr_tgt_dev *tgt_dev,
1037 		struct queue_limits *lim)
1038 {
1039 	u8 pgsz = tgt_dev->dev_spec.pcie_inf.pgsz ? : MPI3MR_DEFAULT_PGSZEXP;
1040 
1041 	lim->max_hw_sectors = tgt_dev->dev_spec.pcie_inf.mdts / 512;
1042 	lim->virt_boundary_mask = (1 << pgsz) - 1;
1043 }
1044 
mpi3mr_configure_tgt_dev(struct mpi3mr_tgt_dev * tgt_dev,struct queue_limits * lim)1045 static void mpi3mr_configure_tgt_dev(struct mpi3mr_tgt_dev *tgt_dev,
1046 		struct queue_limits *lim)
1047 {
1048 	if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE &&
1049 	    (tgt_dev->dev_spec.pcie_inf.dev_info &
1050 	     MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
1051 			MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE)
1052 		mpi3mr_configure_nvme_dev(tgt_dev, lim);
1053 }
1054 
1055 /**
1056  * mpi3mr_update_sdev - Update SCSI device information
1057  * @sdev: SCSI device reference
1058  * @data: target device reference
1059  *
1060  * This is an iterator function called for each SCSI device in a
1061  * target to update the target specific information into each
1062  * SCSI device.
1063  *
1064  * Return: Nothing.
1065  */
1066 static void
mpi3mr_update_sdev(struct scsi_device * sdev,void * data)1067 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
1068 {
1069 	struct mpi3mr_tgt_dev *tgtdev;
1070 	struct queue_limits lim;
1071 
1072 	tgtdev = (struct mpi3mr_tgt_dev *)data;
1073 	if (!tgtdev)
1074 		return;
1075 
1076 	mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
1077 
1078 	lim = queue_limits_start_update(sdev->request_queue);
1079 	mpi3mr_configure_tgt_dev(tgtdev, &lim);
1080 	WARN_ON_ONCE(queue_limits_commit_update(sdev->request_queue, &lim));
1081 }
1082 
1083 /**
1084  * mpi3mr_refresh_tgtdevs - Refresh target device exposure
1085  * @mrioc: Adapter instance reference
1086  *
1087  * This is executed post controller reset to identify any
1088  * missing devices during reset and remove from the upper layers
1089  * or expose any newly detected device to the upper layers.
1090  *
1091  * Return: Nothing.
1092  */
mpi3mr_refresh_tgtdevs(struct mpi3mr_ioc * mrioc)1093 static void mpi3mr_refresh_tgtdevs(struct mpi3mr_ioc *mrioc)
1094 {
1095 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
1096 	struct mpi3mr_stgt_priv_data *tgt_priv;
1097 
1098 	dprint_reset(mrioc, "refresh target devices: check for removals\n");
1099 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1100 	    list) {
1101 		if (((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) ||
1102 		     tgtdev->is_hidden) &&
1103 		     tgtdev->host_exposed && tgtdev->starget &&
1104 		     tgtdev->starget->hostdata) {
1105 			tgt_priv = tgtdev->starget->hostdata;
1106 			tgt_priv->dev_removed = 1;
1107 			atomic_set(&tgt_priv->block_io, 0);
1108 		}
1109 	}
1110 
1111 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1112 	    list) {
1113 		if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
1114 			dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
1115 			    tgtdev->perst_id);
1116 			if (tgtdev->host_exposed)
1117 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1118 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
1119 			mpi3mr_tgtdev_put(tgtdev);
1120 		} else if (tgtdev->is_hidden & tgtdev->host_exposed) {
1121 			dprint_reset(mrioc, "hiding target device with perst_id(%d)\n",
1122 				     tgtdev->perst_id);
1123 			mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1124 		}
1125 	}
1126 
1127 	tgtdev = NULL;
1128 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1129 		if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
1130 		    !tgtdev->is_hidden) {
1131 			if (!tgtdev->host_exposed)
1132 				mpi3mr_report_tgtdev_to_host(mrioc,
1133 							     tgtdev->perst_id);
1134 			else if (tgtdev->starget)
1135 				starget_for_each_device(tgtdev->starget,
1136 							(void *)tgtdev, mpi3mr_update_sdev);
1137 	}
1138 	}
1139 }
1140 
1141 /**
1142  * mpi3mr_debug_dump_devpg0 - Dump device page0
1143  * @mrioc: Adapter instance reference
1144  * @dev_pg0: Device page 0.
1145  *
1146  * Prints pertinent details of the device page 0.
1147  *
1148  * Return: Nothing.
1149  */
1150 static void
mpi3mr_debug_dump_devpg0(struct mpi3mr_ioc * mrioc,struct mpi3_device_page0 * dev_pg0)1151 mpi3mr_debug_dump_devpg0(struct mpi3mr_ioc *mrioc, struct mpi3_device_page0 *dev_pg0)
1152 {
1153 	ioc_info(mrioc,
1154 	    "device_pg0: handle(0x%04x), perst_id(%d), wwid(0x%016llx), encl_handle(0x%04x), slot(%d)\n",
1155 	    le16_to_cpu(dev_pg0->dev_handle),
1156 	    le16_to_cpu(dev_pg0->persistent_id),
1157 	    le64_to_cpu(dev_pg0->wwid), le16_to_cpu(dev_pg0->enclosure_handle),
1158 	    le16_to_cpu(dev_pg0->slot));
1159 	ioc_info(mrioc, "device_pg0: access_status(0x%02x), flags(0x%04x), device_form(0x%02x), queue_depth(%d)\n",
1160 	    dev_pg0->access_status, le16_to_cpu(dev_pg0->flags),
1161 	    dev_pg0->device_form, le16_to_cpu(dev_pg0->queue_depth));
1162 	ioc_info(mrioc, "device_pg0: parent_handle(0x%04x), iounit_port(%d)\n",
1163 	    le16_to_cpu(dev_pg0->parent_dev_handle), dev_pg0->io_unit_port);
1164 
1165 	switch (dev_pg0->device_form) {
1166 	case MPI3_DEVICE_DEVFORM_SAS_SATA:
1167 	{
1168 
1169 		struct mpi3_device0_sas_sata_format *sasinf =
1170 		    &dev_pg0->device_specific.sas_sata_format;
1171 		ioc_info(mrioc,
1172 		    "device_pg0: sas_sata: sas_address(0x%016llx),flags(0x%04x),\n"
1173 		    "device_info(0x%04x), phy_num(%d), attached_phy_id(%d),negotiated_link_rate(0x%02x)\n",
1174 		    le64_to_cpu(sasinf->sas_address),
1175 		    le16_to_cpu(sasinf->flags),
1176 		    le16_to_cpu(sasinf->device_info), sasinf->phy_num,
1177 		    sasinf->attached_phy_identifier, sasinf->negotiated_link_rate);
1178 		break;
1179 	}
1180 	case MPI3_DEVICE_DEVFORM_PCIE:
1181 	{
1182 
1183 		struct mpi3_device0_pcie_format *pcieinf =
1184 		    &dev_pg0->device_specific.pcie_format;
1185 		ioc_info(mrioc,
1186 		    "device_pg0: pcie: port_num(%d), device_info(0x%04x), mdts(%d), page_sz(0x%02x)\n",
1187 		    pcieinf->port_num, le16_to_cpu(pcieinf->device_info),
1188 		    le32_to_cpu(pcieinf->maximum_data_transfer_size),
1189 		    pcieinf->page_size);
1190 		ioc_info(mrioc,
1191 		    "device_pg0: pcie: abort_timeout(%d), reset_timeout(%d) capabilities (0x%08x)\n",
1192 		    pcieinf->nvme_abort_to, pcieinf->controller_reset_to,
1193 		    le32_to_cpu(pcieinf->capabilities));
1194 		break;
1195 	}
1196 	case MPI3_DEVICE_DEVFORM_VD:
1197 	{
1198 
1199 		struct mpi3_device0_vd_format *vdinf =
1200 		    &dev_pg0->device_specific.vd_format;
1201 
1202 		ioc_info(mrioc,
1203 		    "device_pg0: vd: state(0x%02x), raid_level(%d), flags(0x%04x),\n"
1204 		    "device_info(0x%04x) abort_timeout(%d), reset_timeout(%d)\n",
1205 		    vdinf->vd_state, vdinf->raid_level,
1206 		    le16_to_cpu(vdinf->flags),
1207 		    le16_to_cpu(vdinf->device_info),
1208 		    vdinf->vd_abort_to, vdinf->vd_reset_to);
1209 		ioc_info(mrioc,
1210 		    "device_pg0: vd: tg_id(%d), high(%dMiB), low(%dMiB), qd_reduction_factor(%d)\n",
1211 		    vdinf->io_throttle_group,
1212 		    le16_to_cpu(vdinf->io_throttle_group_high),
1213 		    le16_to_cpu(vdinf->io_throttle_group_low),
1214 		    ((le16_to_cpu(vdinf->flags) &
1215 		       MPI3_DEVICE0_VD_FLAGS_IO_THROTTLE_GROUP_QD_MASK) >> 12));
1216 		break;
1217 
1218 	}
1219 	default:
1220 		break;
1221 	}
1222 }
1223 
1224 /**
1225  * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1226  * @mrioc: Adapter instance reference
1227  * @tgtdev: Target device internal structure
1228  * @dev_pg0: New device page0
1229  * @is_added: Flag to indicate the device is just added
1230  *
1231  * Update the information from the device page0 into the driver
1232  * cached target device structure.
1233  *
1234  * Return: Nothing.
1235  */
mpi3mr_update_tgtdev(struct mpi3mr_ioc * mrioc,struct mpi3mr_tgt_dev * tgtdev,struct mpi3_device_page0 * dev_pg0,bool is_added)1236 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1237 	struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1238 	bool is_added)
1239 {
1240 	u16 flags = 0;
1241 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1242 	struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1243 	u8 prot_mask = 0;
1244 
1245 	if (mrioc->logging_level &
1246 	    (MPI3_DEBUG_EVENT | MPI3_DEBUG_EVENT_WORK_TASK))
1247 		mpi3mr_debug_dump_devpg0(mrioc, dev_pg0);
1248 
1249 	tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1250 	tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1251 	tgtdev->dev_type = dev_pg0->device_form;
1252 	tgtdev->io_unit_port = dev_pg0->io_unit_port;
1253 	tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1254 	tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1255 	tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1256 	tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1257 	tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1258 	tgtdev->devpg0_flag = le16_to_cpu(dev_pg0->flags);
1259 
1260 	if (tgtdev->encl_handle)
1261 		enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1262 		    tgtdev->encl_handle);
1263 	if (enclosure_dev)
1264 		tgtdev->enclosure_logical_id = le64_to_cpu(
1265 		    enclosure_dev->pg0.enclosure_logical_id);
1266 
1267 	flags = tgtdev->devpg0_flag;
1268 
1269 	tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1270 
1271 	if (is_added == true)
1272 		tgtdev->io_throttle_enabled =
1273 		    (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1274 	if (!mrioc->sas_transport_enabled)
1275 		tgtdev->non_stl = 1;
1276 
1277 	switch (flags & MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_MASK) {
1278 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_256_LB:
1279 		tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_256_BLKS;
1280 		break;
1281 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_2048_LB:
1282 		tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_2048_BLKS;
1283 		break;
1284 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_NO_LIMIT:
1285 	default:
1286 		tgtdev->wslen = 0;
1287 		break;
1288 	}
1289 
1290 	if (tgtdev->starget && tgtdev->starget->hostdata) {
1291 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1292 		    tgtdev->starget->hostdata;
1293 		scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1294 		scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1295 		scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1296 		scsi_tgt_priv_data->io_throttle_enabled =
1297 		    tgtdev->io_throttle_enabled;
1298 		if (is_added == true)
1299 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
1300 		scsi_tgt_priv_data->wslen = tgtdev->wslen;
1301 	}
1302 
1303 	switch (dev_pg0->access_status) {
1304 	case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1305 	case MPI3_DEVICE0_ASTATUS_PREPARE:
1306 	case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1307 	case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1308 		break;
1309 	default:
1310 		tgtdev->is_hidden = 1;
1311 		break;
1312 	}
1313 
1314 	switch (tgtdev->dev_type) {
1315 	case MPI3_DEVICE_DEVFORM_SAS_SATA:
1316 	{
1317 		struct mpi3_device0_sas_sata_format *sasinf =
1318 		    &dev_pg0->device_specific.sas_sata_format;
1319 		u16 dev_info = le16_to_cpu(sasinf->device_info);
1320 
1321 		tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1322 		tgtdev->dev_spec.sas_sata_inf.sas_address =
1323 		    le64_to_cpu(sasinf->sas_address);
1324 		tgtdev->dev_spec.sas_sata_inf.phy_id = sasinf->phy_num;
1325 		tgtdev->dev_spec.sas_sata_inf.attached_phy_id =
1326 		    sasinf->attached_phy_identifier;
1327 		tgtdev->dev_spec.sas_sata_inf.negotiated_link_rate =
1328 			sasinf->negotiated_link_rate;
1329 		if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1330 		    MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1331 			tgtdev->is_hidden = 1;
1332 		else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1333 		    MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1334 			tgtdev->is_hidden = 1;
1335 
1336 		if (((tgtdev->devpg0_flag &
1337 		    MPI3_DEVICE0_FLAGS_ATT_METHOD_DIR_ATTACHED)
1338 		    && (tgtdev->devpg0_flag &
1339 		    MPI3_DEVICE0_FLAGS_ATT_METHOD_VIRTUAL)) ||
1340 		    (tgtdev->parent_handle == 0xFFFF))
1341 			tgtdev->non_stl = 1;
1342 		if (tgtdev->dev_spec.sas_sata_inf.hba_port)
1343 			tgtdev->dev_spec.sas_sata_inf.hba_port->port_id =
1344 			    dev_pg0->io_unit_port;
1345 		break;
1346 	}
1347 	case MPI3_DEVICE_DEVFORM_PCIE:
1348 	{
1349 		struct mpi3_device0_pcie_format *pcieinf =
1350 		    &dev_pg0->device_specific.pcie_format;
1351 		u16 dev_info = le16_to_cpu(pcieinf->device_info);
1352 
1353 		tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1354 		tgtdev->dev_spec.pcie_inf.capb =
1355 		    le32_to_cpu(pcieinf->capabilities);
1356 		tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1357 		/* 2^12 = 4096 */
1358 		tgtdev->dev_spec.pcie_inf.pgsz = 12;
1359 		if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1360 			tgtdev->dev_spec.pcie_inf.mdts =
1361 			    le32_to_cpu(pcieinf->maximum_data_transfer_size);
1362 			tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1363 			tgtdev->dev_spec.pcie_inf.reset_to =
1364 			    max_t(u8, pcieinf->controller_reset_to,
1365 			     MPI3MR_INTADMCMD_TIMEOUT);
1366 			tgtdev->dev_spec.pcie_inf.abort_to =
1367 			    max_t(u8, pcieinf->nvme_abort_to,
1368 			    MPI3MR_INTADMCMD_TIMEOUT);
1369 		}
1370 		if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1371 			tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1372 		if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1373 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1374 		    ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1375 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1376 			tgtdev->is_hidden = 1;
1377 		tgtdev->non_stl = 1;
1378 		if (!mrioc->shost)
1379 			break;
1380 		prot_mask = scsi_host_get_prot(mrioc->shost);
1381 		if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1382 			scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1383 			ioc_info(mrioc,
1384 			    "%s : Disabling DIX0 prot capability\n", __func__);
1385 			ioc_info(mrioc,
1386 			    "because HBA does not support DIX0 operation on NVME drives\n");
1387 		}
1388 		break;
1389 	}
1390 	case MPI3_DEVICE_DEVFORM_VD:
1391 	{
1392 		struct mpi3_device0_vd_format *vdinf =
1393 		    &dev_pg0->device_specific.vd_format;
1394 		struct mpi3mr_throttle_group_info *tg = NULL;
1395 		u16 vdinf_io_throttle_group =
1396 		    le16_to_cpu(vdinf->io_throttle_group);
1397 
1398 		tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1399 		if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1400 			tgtdev->is_hidden = 1;
1401 		tgtdev->non_stl = 1;
1402 		tgtdev->dev_spec.vd_inf.reset_to =
1403 			max_t(u8, vdinf->vd_reset_to,
1404 			      MPI3MR_INTADMCMD_TIMEOUT);
1405 		tgtdev->dev_spec.vd_inf.abort_to =
1406 			max_t(u8, vdinf->vd_abort_to,
1407 			      MPI3MR_INTADMCMD_TIMEOUT);
1408 		tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1409 		tgtdev->dev_spec.vd_inf.tg_high =
1410 		    le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1411 		tgtdev->dev_spec.vd_inf.tg_low =
1412 		    le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1413 		if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1414 			tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1415 			tg->id = vdinf_io_throttle_group;
1416 			tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1417 			tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1418 			tg->qd_reduction =
1419 			    tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1420 			if (is_added == true)
1421 				tg->fw_qd = tgtdev->q_depth;
1422 			tg->modified_qd = tgtdev->q_depth;
1423 		}
1424 		tgtdev->dev_spec.vd_inf.tg = tg;
1425 		if (scsi_tgt_priv_data)
1426 			scsi_tgt_priv_data->throttle_group = tg;
1427 		break;
1428 	}
1429 	default:
1430 		break;
1431 	}
1432 }
1433 
1434 /**
1435  * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1436  * @mrioc: Adapter instance reference
1437  * @fwevt: Firmware event information.
1438  *
1439  * Process Device status Change event and based on device's new
1440  * information, either expose the device to the upper layers, or
1441  * remove the device from upper layers.
1442  *
1443  * Return: Nothing.
1444  */
mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1445 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1446 	struct mpi3mr_fwevt *fwevt)
1447 {
1448 	u16 dev_handle = 0;
1449 	u8 uhide = 0, delete = 0, cleanup = 0;
1450 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1451 	struct mpi3_event_data_device_status_change *evtdata =
1452 	    (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1453 
1454 	dev_handle = le16_to_cpu(evtdata->dev_handle);
1455 	dprint_event_bh(mrioc,
1456 	    "processing device status change event bottom half for handle(0x%04x), rc(0x%02x)\n",
1457 	    dev_handle, evtdata->reason_code);
1458 	switch (evtdata->reason_code) {
1459 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1460 		delete = 1;
1461 		break;
1462 	case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1463 		uhide = 1;
1464 		break;
1465 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1466 		delete = 1;
1467 		cleanup = 1;
1468 		break;
1469 	default:
1470 		ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1471 		    evtdata->reason_code);
1472 		break;
1473 	}
1474 
1475 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1476 	if (!tgtdev) {
1477 		dprint_event_bh(mrioc,
1478 		    "processing device status change event bottom half,\n"
1479 		    "cannot identify target device for handle(0x%04x), rc(0x%02x)\n",
1480 		    dev_handle, evtdata->reason_code);
1481 		goto out;
1482 	}
1483 	if (uhide) {
1484 		tgtdev->is_hidden = 0;
1485 		if (!tgtdev->host_exposed)
1486 			mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1487 	}
1488 
1489 	if (delete)
1490 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1491 
1492 	if (cleanup) {
1493 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1494 		mpi3mr_tgtdev_put(tgtdev);
1495 	}
1496 
1497 out:
1498 	if (tgtdev)
1499 		mpi3mr_tgtdev_put(tgtdev);
1500 }
1501 
1502 /**
1503  * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1504  * @mrioc: Adapter instance reference
1505  * @dev_pg0: New device page0
1506  *
1507  * Process Device Info Change event and based on device's new
1508  * information, either expose the device to the upper layers, or
1509  * remove the device from upper layers or update the details of
1510  * the device.
1511  *
1512  * Return: Nothing.
1513  */
mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3_device_page0 * dev_pg0)1514 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1515 	struct mpi3_device_page0 *dev_pg0)
1516 {
1517 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1518 	u16 dev_handle = 0, perst_id = 0;
1519 
1520 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
1521 	dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1522 	dprint_event_bh(mrioc,
1523 	    "processing device info change event bottom half for handle(0x%04x), perst_id(%d)\n",
1524 	    dev_handle, perst_id);
1525 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1526 	if (!tgtdev) {
1527 		dprint_event_bh(mrioc,
1528 		    "cannot identify target device for  device info\n"
1529 		    "change event handle(0x%04x), perst_id(%d)\n",
1530 		    dev_handle, perst_id);
1531 		goto out;
1532 	}
1533 	mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1534 	if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1535 		mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1536 	if (tgtdev->is_hidden && tgtdev->host_exposed)
1537 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1538 	if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1539 		starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1540 		    mpi3mr_update_sdev);
1541 out:
1542 	if (tgtdev)
1543 		mpi3mr_tgtdev_put(tgtdev);
1544 }
1545 
1546 /**
1547  * mpi3mr_free_enclosure_list - release enclosures
1548  * @mrioc: Adapter instance reference
1549  *
1550  * Free memory allocated during encloure add.
1551  *
1552  * Return nothing.
1553  */
mpi3mr_free_enclosure_list(struct mpi3mr_ioc * mrioc)1554 void mpi3mr_free_enclosure_list(struct mpi3mr_ioc *mrioc)
1555 {
1556 	struct mpi3mr_enclosure_node *enclosure_dev, *enclosure_dev_next;
1557 
1558 	list_for_each_entry_safe(enclosure_dev,
1559 	    enclosure_dev_next, &mrioc->enclosure_list, list) {
1560 		list_del(&enclosure_dev->list);
1561 		kfree(enclosure_dev);
1562 	}
1563 }
1564 
1565 /**
1566  * mpi3mr_enclosure_find_by_handle - enclosure search by handle
1567  * @mrioc: Adapter instance reference
1568  * @handle: Firmware device handle of the enclosure
1569  *
1570  * This searches for enclosure device based on handle, then returns the
1571  * enclosure object.
1572  *
1573  * Return: Enclosure object reference or NULL
1574  */
mpi3mr_enclosure_find_by_handle(struct mpi3mr_ioc * mrioc,u16 handle)1575 struct mpi3mr_enclosure_node *mpi3mr_enclosure_find_by_handle(
1576 	struct mpi3mr_ioc *mrioc, u16 handle)
1577 {
1578 	struct mpi3mr_enclosure_node *enclosure_dev, *r = NULL;
1579 
1580 	list_for_each_entry(enclosure_dev, &mrioc->enclosure_list, list) {
1581 		if (le16_to_cpu(enclosure_dev->pg0.enclosure_handle) != handle)
1582 			continue;
1583 		r = enclosure_dev;
1584 		goto out;
1585 	}
1586 out:
1587 	return r;
1588 }
1589 
1590 /**
1591  * mpi3mr_process_trigger_data_event_bh - Process trigger event
1592  * data
1593  * @mrioc: Adapter instance reference
1594  * @event_data: Event data
1595  *
1596  * This function releases diage buffers or issues diag fault
1597  * based on trigger conditions
1598  *
1599  * Return: Nothing
1600  */
mpi3mr_process_trigger_data_event_bh(struct mpi3mr_ioc * mrioc,struct trigger_event_data * event_data)1601 static void mpi3mr_process_trigger_data_event_bh(struct mpi3mr_ioc *mrioc,
1602 	struct trigger_event_data *event_data)
1603 {
1604 	struct diag_buffer_desc *trace_hdb = event_data->trace_hdb;
1605 	struct diag_buffer_desc *fw_hdb = event_data->fw_hdb;
1606 	unsigned long flags;
1607 	int retval = 0;
1608 	u8 trigger_type = event_data->trigger_type;
1609 	union mpi3mr_trigger_data *trigger_data =
1610 		&event_data->trigger_specific_data;
1611 
1612 	if (event_data->snapdump)  {
1613 		if (trace_hdb)
1614 			mpi3mr_set_trigger_data_in_hdb(trace_hdb, trigger_type,
1615 			    trigger_data, 1);
1616 		if (fw_hdb)
1617 			mpi3mr_set_trigger_data_in_hdb(fw_hdb, trigger_type,
1618 			    trigger_data, 1);
1619 		mpi3mr_soft_reset_handler(mrioc,
1620 			    MPI3MR_RESET_FROM_TRIGGER, 1);
1621 		return;
1622 	}
1623 
1624 	if (trace_hdb) {
1625 		retval = mpi3mr_issue_diag_buf_release(mrioc, trace_hdb);
1626 		if (!retval) {
1627 			mpi3mr_set_trigger_data_in_hdb(trace_hdb, trigger_type,
1628 			    trigger_data, 1);
1629 		}
1630 		spin_lock_irqsave(&mrioc->trigger_lock, flags);
1631 		mrioc->trace_release_trigger_active = false;
1632 		spin_unlock_irqrestore(&mrioc->trigger_lock, flags);
1633 	}
1634 	if (fw_hdb) {
1635 		retval = mpi3mr_issue_diag_buf_release(mrioc, fw_hdb);
1636 		if (!retval) {
1637 			mpi3mr_set_trigger_data_in_hdb(fw_hdb, trigger_type,
1638 		    trigger_data, 1);
1639 		}
1640 		spin_lock_irqsave(&mrioc->trigger_lock, flags);
1641 		mrioc->fw_release_trigger_active = false;
1642 		spin_unlock_irqrestore(&mrioc->trigger_lock, flags);
1643 	}
1644 }
1645 
1646 /**
1647  * mpi3mr_encldev_add_chg_evt_debug - debug for enclosure event
1648  * @mrioc: Adapter instance reference
1649  * @encl_pg0: Enclosure page 0.
1650  * @is_added: Added event or not
1651  *
1652  * Return nothing.
1653  */
mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc * mrioc,struct mpi3_enclosure_page0 * encl_pg0,u8 is_added)1654 static void mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc *mrioc,
1655 	struct mpi3_enclosure_page0 *encl_pg0, u8 is_added)
1656 {
1657 	char *reason_str = NULL;
1658 
1659 	if (!(mrioc->logging_level & MPI3_DEBUG_EVENT_WORK_TASK))
1660 		return;
1661 
1662 	if (is_added)
1663 		reason_str = "enclosure added";
1664 	else
1665 		reason_str = "enclosure dev status changed";
1666 
1667 	ioc_info(mrioc,
1668 	    "%s: handle(0x%04x), enclosure logical id(0x%016llx)\n",
1669 	    reason_str, le16_to_cpu(encl_pg0->enclosure_handle),
1670 	    (unsigned long long)le64_to_cpu(encl_pg0->enclosure_logical_id));
1671 	ioc_info(mrioc,
1672 	    "number of slots(%d), port(%d), flags(0x%04x), present(%d)\n",
1673 	    le16_to_cpu(encl_pg0->num_slots), encl_pg0->io_unit_port,
1674 	    le16_to_cpu(encl_pg0->flags),
1675 	    ((le16_to_cpu(encl_pg0->flags) &
1676 	      MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4));
1677 }
1678 
1679 /**
1680  * mpi3mr_encldev_add_chg_evt_bh - Enclosure evt bottomhalf
1681  * @mrioc: Adapter instance reference
1682  * @fwevt: Firmware event reference
1683  *
1684  * Prints information about the Enclosure device status or
1685  * Enclosure add events if logging is enabled and add or remove
1686  * the enclosure from the controller's internal list of
1687  * enclosures.
1688  *
1689  * Return: Nothing.
1690  */
mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1691 static void mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc *mrioc,
1692 	struct mpi3mr_fwevt *fwevt)
1693 {
1694 	struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1695 	struct mpi3_enclosure_page0 *encl_pg0;
1696 	u16 encl_handle;
1697 	u8 added, present;
1698 
1699 	encl_pg0 = (struct mpi3_enclosure_page0 *) fwevt->event_data;
1700 	added = (fwevt->event_id == MPI3_EVENT_ENCL_DEVICE_ADDED) ? 1 : 0;
1701 	mpi3mr_encldev_add_chg_evt_debug(mrioc, encl_pg0, added);
1702 
1703 
1704 	encl_handle = le16_to_cpu(encl_pg0->enclosure_handle);
1705 	present = ((le16_to_cpu(encl_pg0->flags) &
1706 	      MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4);
1707 
1708 	if (encl_handle)
1709 		enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1710 		    encl_handle);
1711 	if (!enclosure_dev && present) {
1712 		enclosure_dev =
1713 			kzalloc_obj(struct mpi3mr_enclosure_node);
1714 		if (!enclosure_dev)
1715 			return;
1716 		list_add_tail(&enclosure_dev->list,
1717 		    &mrioc->enclosure_list);
1718 	}
1719 	if (enclosure_dev) {
1720 		if (!present) {
1721 			list_del(&enclosure_dev->list);
1722 			kfree(enclosure_dev);
1723 		} else
1724 			memcpy(&enclosure_dev->pg0, encl_pg0,
1725 			    sizeof(enclosure_dev->pg0));
1726 
1727 	}
1728 }
1729 
1730 /**
1731  * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1732  * @mrioc: Adapter instance reference
1733  * @event_data: SAS topology change list event data
1734  *
1735  * Prints information about the SAS topology change event.
1736  *
1737  * Return: Nothing.
1738  */
1739 static void
mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc * mrioc,struct mpi3_event_data_sas_topology_change_list * event_data)1740 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1741 	struct mpi3_event_data_sas_topology_change_list *event_data)
1742 {
1743 	int i;
1744 	u16 handle;
1745 	u8 reason_code, phy_number;
1746 	char *status_str = NULL;
1747 	u8 link_rate, prev_link_rate;
1748 
1749 	switch (event_data->exp_status) {
1750 	case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1751 		status_str = "remove";
1752 		break;
1753 	case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1754 		status_str =  "responding";
1755 		break;
1756 	case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1757 		status_str = "remove delay";
1758 		break;
1759 	case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1760 		status_str = "direct attached";
1761 		break;
1762 	default:
1763 		status_str = "unknown status";
1764 		break;
1765 	}
1766 	ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1767 	    __func__, status_str);
1768 	ioc_info(mrioc,
1769 	    "%s :\texpander_handle(0x%04x), port(%d), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1770 	    __func__, le16_to_cpu(event_data->expander_dev_handle),
1771 	    event_data->io_unit_port,
1772 	    le16_to_cpu(event_data->enclosure_handle),
1773 	    event_data->start_phy_num, event_data->num_entries);
1774 	for (i = 0; i < event_data->num_entries; i++) {
1775 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1776 		if (!handle)
1777 			continue;
1778 		phy_number = event_data->start_phy_num + i;
1779 		reason_code = event_data->phy_entry[i].status &
1780 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1781 		switch (reason_code) {
1782 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1783 			status_str = "target remove";
1784 			break;
1785 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1786 			status_str = "delay target remove";
1787 			break;
1788 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1789 			status_str = "link status change";
1790 			break;
1791 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1792 			status_str = "link status no change";
1793 			break;
1794 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1795 			status_str = "target responding";
1796 			break;
1797 		default:
1798 			status_str = "unknown";
1799 			break;
1800 		}
1801 		link_rate = event_data->phy_entry[i].link_rate >> 4;
1802 		prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1803 		ioc_info(mrioc,
1804 		    "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1805 		    __func__, phy_number, handle, status_str, link_rate,
1806 		    prev_link_rate);
1807 	}
1808 }
1809 
1810 /**
1811  * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1812  * @mrioc: Adapter instance reference
1813  * @fwevt: Firmware event reference
1814  *
1815  * Prints information about the SAS topology change event and
1816  * for "not responding" event code, removes the device from the
1817  * upper layers.
1818  *
1819  * Return: Nothing.
1820  */
mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)1821 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1822 	struct mpi3mr_fwevt *fwevt)
1823 {
1824 	struct mpi3_event_data_sas_topology_change_list *event_data =
1825 	    (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1826 	int i;
1827 	u16 handle;
1828 	u8 reason_code;
1829 	u64 exp_sas_address = 0, parent_sas_address = 0;
1830 	struct mpi3mr_hba_port *hba_port = NULL;
1831 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1832 	struct mpi3mr_sas_node *sas_expander = NULL;
1833 	unsigned long flags;
1834 	u8 link_rate, prev_link_rate, parent_phy_number;
1835 
1836 	mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1837 	if (mrioc->sas_transport_enabled) {
1838 		hba_port = mpi3mr_get_hba_port_by_id(mrioc,
1839 		    event_data->io_unit_port);
1840 		if (le16_to_cpu(event_data->expander_dev_handle)) {
1841 			spin_lock_irqsave(&mrioc->sas_node_lock, flags);
1842 			sas_expander = __mpi3mr_expander_find_by_handle(mrioc,
1843 			    le16_to_cpu(event_data->expander_dev_handle));
1844 			if (sas_expander) {
1845 				exp_sas_address = sas_expander->sas_address;
1846 				hba_port = sas_expander->hba_port;
1847 			}
1848 			spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
1849 			parent_sas_address = exp_sas_address;
1850 		} else
1851 			parent_sas_address = mrioc->sas_hba.sas_address;
1852 	}
1853 
1854 	for (i = 0; i < event_data->num_entries; i++) {
1855 		if (fwevt->discard)
1856 			return;
1857 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1858 		if (!handle)
1859 			continue;
1860 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1861 		if (!tgtdev)
1862 			continue;
1863 
1864 		reason_code = event_data->phy_entry[i].status &
1865 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1866 
1867 		switch (reason_code) {
1868 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1869 			if (tgtdev->host_exposed)
1870 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1871 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1872 			mpi3mr_tgtdev_put(tgtdev);
1873 			break;
1874 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1875 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1876 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1877 		{
1878 			if (!mrioc->sas_transport_enabled || tgtdev->non_stl
1879 			    || tgtdev->is_hidden)
1880 				break;
1881 			link_rate = event_data->phy_entry[i].link_rate >> 4;
1882 			prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1883 			if (link_rate == prev_link_rate)
1884 				break;
1885 			if (!parent_sas_address)
1886 				break;
1887 			parent_phy_number = event_data->start_phy_num + i;
1888 			mpi3mr_update_links(mrioc, parent_sas_address, handle,
1889 			    parent_phy_number, link_rate, hba_port);
1890 			break;
1891 		}
1892 		default:
1893 			break;
1894 		}
1895 		if (tgtdev)
1896 			mpi3mr_tgtdev_put(tgtdev);
1897 	}
1898 
1899 	if (mrioc->sas_transport_enabled && (event_data->exp_status ==
1900 	    MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING)) {
1901 		if (sas_expander)
1902 			mpi3mr_expander_remove(mrioc, exp_sas_address,
1903 			    hba_port);
1904 	}
1905 }
1906 
1907 /**
1908  * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1909  * @mrioc: Adapter instance reference
1910  * @event_data: PCIe topology change list event data
1911  *
1912  * Prints information about the PCIe topology change event.
1913  *
1914  * Return: Nothing.
1915  */
1916 static void
mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc * mrioc,struct mpi3_event_data_pcie_topology_change_list * event_data)1917 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1918 	struct mpi3_event_data_pcie_topology_change_list *event_data)
1919 {
1920 	int i;
1921 	u16 handle;
1922 	u16 reason_code;
1923 	u8 port_number;
1924 	char *status_str = NULL;
1925 	u8 link_rate, prev_link_rate;
1926 
1927 	switch (event_data->switch_status) {
1928 	case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1929 		status_str = "remove";
1930 		break;
1931 	case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1932 		status_str =  "responding";
1933 		break;
1934 	case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1935 		status_str = "remove delay";
1936 		break;
1937 	case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1938 		status_str = "direct attached";
1939 		break;
1940 	default:
1941 		status_str = "unknown status";
1942 		break;
1943 	}
1944 	ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1945 	    __func__, status_str);
1946 	ioc_info(mrioc,
1947 	    "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1948 	    __func__, le16_to_cpu(event_data->switch_dev_handle),
1949 	    le16_to_cpu(event_data->enclosure_handle),
1950 	    event_data->start_port_num, event_data->num_entries);
1951 	for (i = 0; i < event_data->num_entries; i++) {
1952 		handle =
1953 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1954 		if (!handle)
1955 			continue;
1956 		port_number = event_data->start_port_num + i;
1957 		reason_code = event_data->port_entry[i].port_status;
1958 		switch (reason_code) {
1959 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1960 			status_str = "target remove";
1961 			break;
1962 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1963 			status_str = "delay target remove";
1964 			break;
1965 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1966 			status_str = "link status change";
1967 			break;
1968 		case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1969 			status_str = "link status no change";
1970 			break;
1971 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1972 			status_str = "target responding";
1973 			break;
1974 		default:
1975 			status_str = "unknown";
1976 			break;
1977 		}
1978 		link_rate = event_data->port_entry[i].current_port_info &
1979 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1980 		prev_link_rate = event_data->port_entry[i].previous_port_info &
1981 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1982 		ioc_info(mrioc,
1983 		    "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1984 		    __func__, port_number, handle, status_str, link_rate,
1985 		    prev_link_rate);
1986 	}
1987 }
1988 
1989 /**
1990  * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1991  * @mrioc: Adapter instance reference
1992  * @fwevt: Firmware event reference
1993  *
1994  * Prints information about the PCIe topology change event and
1995  * for "not responding" event code, removes the device from the
1996  * upper layers.
1997  *
1998  * Return: Nothing.
1999  */
mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)2000 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
2001 	struct mpi3mr_fwevt *fwevt)
2002 {
2003 	struct mpi3_event_data_pcie_topology_change_list *event_data =
2004 	    (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
2005 	int i;
2006 	u16 handle;
2007 	u8 reason_code;
2008 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2009 
2010 	mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
2011 
2012 	for (i = 0; i < event_data->num_entries; i++) {
2013 		if (fwevt->discard)
2014 			return;
2015 		handle =
2016 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
2017 		if (!handle)
2018 			continue;
2019 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2020 		if (!tgtdev)
2021 			continue;
2022 
2023 		reason_code = event_data->port_entry[i].port_status;
2024 
2025 		switch (reason_code) {
2026 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2027 			if (tgtdev->host_exposed)
2028 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
2029 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
2030 			mpi3mr_tgtdev_put(tgtdev);
2031 			break;
2032 		default:
2033 			break;
2034 		}
2035 		if (tgtdev)
2036 			mpi3mr_tgtdev_put(tgtdev);
2037 	}
2038 }
2039 
2040 /**
2041  * mpi3mr_logdata_evt_bh -  Log data event bottomhalf
2042  * @mrioc: Adapter instance reference
2043  * @fwevt: Firmware event reference
2044  *
2045  * Extracts the event data and calls application interfacing
2046  * function to process the event further.
2047  *
2048  * Return: Nothing.
2049  */
mpi3mr_logdata_evt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)2050 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
2051 	struct mpi3mr_fwevt *fwevt)
2052 {
2053 	mpi3mr_app_save_logdata_th(mrioc, fwevt->event_data,
2054 	    fwevt->event_data_size);
2055 }
2056 
2057 /**
2058  * mpi3mr_update_sdev_qd - Update SCSI device queue depath
2059  * @sdev: SCSI device reference
2060  * @data: Queue depth reference
2061  *
2062  * This is an iterator function called for each SCSI device in a
2063  * target to update the QD of each SCSI device.
2064  *
2065  * Return: Nothing.
2066  */
mpi3mr_update_sdev_qd(struct scsi_device * sdev,void * data)2067 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
2068 {
2069 	u16 *q_depth = (u16 *)data;
2070 
2071 	scsi_change_queue_depth(sdev, (int)*q_depth);
2072 	sdev->max_queue_depth = sdev->queue_depth;
2073 }
2074 
2075 /**
2076  * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
2077  * @mrioc: Adapter instance reference
2078  * @tg: Throttle group information pointer
2079  *
2080  * Accessor to reduce QD for each device associated with the
2081  * given throttle group.
2082  *
2083  * Return: None.
2084  */
mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc * mrioc,struct mpi3mr_throttle_group_info * tg)2085 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
2086 	struct mpi3mr_throttle_group_info *tg)
2087 {
2088 	unsigned long flags;
2089 	struct mpi3mr_tgt_dev *tgtdev;
2090 	struct mpi3mr_stgt_priv_data *tgt_priv;
2091 
2092 
2093 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2094 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
2095 		if (tgtdev->starget && tgtdev->starget->hostdata) {
2096 			tgt_priv = tgtdev->starget->hostdata;
2097 			if (tgt_priv->throttle_group == tg) {
2098 				dprint_event_bh(mrioc,
2099 				    "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
2100 				    tgt_priv->perst_id, tgtdev->q_depth,
2101 				    tg->modified_qd);
2102 				starget_for_each_device(tgtdev->starget,
2103 				    (void *)&tg->modified_qd,
2104 				    mpi3mr_update_sdev_qd);
2105 			}
2106 		}
2107 	}
2108 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2109 }
2110 
2111 /**
2112  * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
2113  * @mrioc: Adapter instance reference
2114  * @fwevt: Firmware event reference
2115  *
2116  * Identifies the firmware event and calls corresponding bottomg
2117  * half handler and sends event acknowledgment if required.
2118  *
2119  * Return: Nothing.
2120  */
mpi3mr_fwevt_bh(struct mpi3mr_ioc * mrioc,struct mpi3mr_fwevt * fwevt)2121 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
2122 	struct mpi3mr_fwevt *fwevt)
2123 {
2124 	struct mpi3_device_page0 *dev_pg0 = NULL;
2125 	u16 perst_id, handle, dev_info;
2126 	struct mpi3_device0_sas_sata_format *sasinf = NULL;
2127 	unsigned int timeout;
2128 
2129 	mpi3mr_fwevt_del_from_list(mrioc, fwevt);
2130 	mrioc->current_event = fwevt;
2131 
2132 	if (mrioc->stop_drv_processing) {
2133 		dprint_event_bh(mrioc, "ignoring event(0x%02x) in the bottom half handler\n"
2134 				"due to stop_drv_processing\n", fwevt->event_id);
2135 		goto out;
2136 	}
2137 
2138 	if (mrioc->unrecoverable) {
2139 		dprint_event_bh(mrioc,
2140 		    "ignoring event(0x%02x) in bottom half handler due to unrecoverable controller\n",
2141 		    fwevt->event_id);
2142 		goto out;
2143 	}
2144 
2145 	if (!fwevt->process_evt)
2146 		goto evt_ack;
2147 
2148 	dprint_event_bh(mrioc, "processing event(0x%02x) -(0x%08x) in the bottom half handler\n",
2149 			fwevt->event_id, fwevt->evt_ctx);
2150 
2151 	switch (fwevt->event_id) {
2152 	case MPI3_EVENT_DEVICE_ADDED:
2153 	{
2154 		dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
2155 		perst_id = le16_to_cpu(dev_pg0->persistent_id);
2156 		handle = le16_to_cpu(dev_pg0->dev_handle);
2157 		if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
2158 			mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
2159 		else if (mrioc->sas_transport_enabled &&
2160 		    (dev_pg0->device_form == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
2161 			sasinf = &dev_pg0->device_specific.sas_sata_format;
2162 			dev_info = le16_to_cpu(sasinf->device_info);
2163 			if (!mrioc->sas_hba.num_phys)
2164 				mpi3mr_sas_host_add(mrioc);
2165 			else
2166 				mpi3mr_sas_host_refresh(mrioc);
2167 
2168 			if (mpi3mr_is_expander_device(dev_info))
2169 				mpi3mr_expander_add(mrioc, handle);
2170 		}
2171 		break;
2172 	}
2173 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
2174 	{
2175 		dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
2176 		perst_id = le16_to_cpu(dev_pg0->persistent_id);
2177 		if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
2178 			mpi3mr_devinfochg_evt_bh(mrioc, dev_pg0);
2179 		break;
2180 	}
2181 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2182 	{
2183 		mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
2184 		break;
2185 	}
2186 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
2187 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2188 	{
2189 		mpi3mr_encldev_add_chg_evt_bh(mrioc, fwevt);
2190 		break;
2191 	}
2192 
2193 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2194 	{
2195 		mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
2196 		break;
2197 	}
2198 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2199 	{
2200 		mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
2201 		break;
2202 	}
2203 	case MPI3_EVENT_LOG_DATA:
2204 	{
2205 		mpi3mr_logdata_evt_bh(mrioc, fwevt);
2206 		break;
2207 	}
2208 	case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
2209 	{
2210 		struct mpi3mr_throttle_group_info *tg;
2211 
2212 		tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
2213 		dprint_event_bh(mrioc,
2214 		    "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
2215 		    tg->id, tg->need_qd_reduction);
2216 		if (tg->need_qd_reduction) {
2217 			mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
2218 			tg->need_qd_reduction = 0;
2219 		}
2220 		break;
2221 	}
2222 	case MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH:
2223 	{
2224 		timeout = MPI3MR_RESET_TIMEOUT * 2;
2225 		while ((mrioc->device_refresh_on || mrioc->block_on_pci_err) &&
2226 		    !mrioc->unrecoverable && !mrioc->pci_err_recovery) {
2227 			msleep(500);
2228 			if (!timeout--) {
2229 				mrioc->unrecoverable = 1;
2230 				break;
2231 			}
2232 		}
2233 
2234 		if (mrioc->unrecoverable || mrioc->pci_err_recovery)
2235 			break;
2236 
2237 		dprint_event_bh(mrioc,
2238 		    "scan for non responding and newly added devices after soft reset started\n");
2239 		if (mrioc->sas_transport_enabled) {
2240 			mpi3mr_refresh_sas_ports(mrioc);
2241 			mpi3mr_refresh_expanders(mrioc);
2242 		}
2243 		mpi3mr_refresh_tgtdevs(mrioc);
2244 		ioc_info(mrioc,
2245 		    "scan for non responding and newly added devices after soft reset completed\n");
2246 		break;
2247 	}
2248 	case MPI3MR_DRIVER_EVENT_PROCESS_TRIGGER:
2249 	{
2250 		mpi3mr_process_trigger_data_event_bh(mrioc,
2251 		    (struct trigger_event_data *)fwevt->event_data);
2252 		break;
2253 	}
2254 	default:
2255 		break;
2256 	}
2257 
2258 evt_ack:
2259 	if (fwevt->send_ack)
2260 		mpi3mr_process_event_ack(mrioc, fwevt->event_id,
2261 		    fwevt->evt_ctx);
2262 out:
2263 	/* Put fwevt reference count to neutralize kref_init increment */
2264 	mpi3mr_fwevt_put(fwevt);
2265 	mrioc->current_event = NULL;
2266 }
2267 
2268 /**
2269  * mpi3mr_fwevt_worker - Firmware event worker
2270  * @work: Work struct containing firmware event
2271  *
2272  * Extracts the firmware event and calls mpi3mr_fwevt_bh.
2273  *
2274  * Return: Nothing.
2275  */
mpi3mr_fwevt_worker(struct work_struct * work)2276 static void mpi3mr_fwevt_worker(struct work_struct *work)
2277 {
2278 	struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
2279 	    work);
2280 	mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
2281 	/*
2282 	 * Put fwevt reference count after
2283 	 * dequeuing it from worker queue
2284 	 */
2285 	mpi3mr_fwevt_put(fwevt);
2286 }
2287 
2288 /**
2289  * mpi3mr_create_tgtdev - Create and add a target device
2290  * @mrioc: Adapter instance reference
2291  * @dev_pg0: Device Page 0 data
2292  *
2293  * If the device specified by the device page 0 data is not
2294  * present in the driver's internal list, allocate the memory
2295  * for the device, populate the data and add to the list, else
2296  * update the device data.  The key is persistent ID.
2297  *
2298  * Return: 0 on success, -ENOMEM on memory allocation failure
2299  */
mpi3mr_create_tgtdev(struct mpi3mr_ioc * mrioc,struct mpi3_device_page0 * dev_pg0)2300 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
2301 	struct mpi3_device_page0 *dev_pg0)
2302 {
2303 	int retval = 0;
2304 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2305 	u16 perst_id = 0;
2306 	unsigned long flags;
2307 
2308 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
2309 	if (perst_id == MPI3_DEVICE0_PERSISTENTID_INVALID)
2310 		return retval;
2311 
2312 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2313 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
2314 	if (tgtdev)
2315 		tgtdev->state = MPI3MR_DEV_CREATED;
2316 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2317 
2318 	if (tgtdev) {
2319 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2320 		mpi3mr_tgtdev_put(tgtdev);
2321 	} else {
2322 		tgtdev = mpi3mr_alloc_tgtdev();
2323 		if (!tgtdev)
2324 			return -ENOMEM;
2325 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2326 		mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
2327 	}
2328 
2329 	return retval;
2330 }
2331 
2332 /**
2333  * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
2334  * @mrioc: Adapter instance reference
2335  *
2336  * Flush pending commands in the delayed lists due to a
2337  * controller reset or driver removal as a cleanup.
2338  *
2339  * Return: Nothing
2340  */
mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc * mrioc)2341 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
2342 {
2343 	struct delayed_dev_rmhs_node *_rmhs_node;
2344 	struct delayed_evt_ack_node *_evtack_node;
2345 
2346 	dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
2347 	while (!list_empty(&mrioc->delayed_rmhs_list)) {
2348 		_rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
2349 		    struct delayed_dev_rmhs_node, list);
2350 		list_del(&_rmhs_node->list);
2351 		kfree(_rmhs_node);
2352 	}
2353 	dprint_reset(mrioc, "flushing delayed event ack commands\n");
2354 	while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2355 		_evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
2356 		    struct delayed_evt_ack_node, list);
2357 		list_del(&_evtack_node->list);
2358 		kfree(_evtack_node);
2359 	}
2360 }
2361 
2362 /**
2363  * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
2364  * @mrioc: Adapter instance reference
2365  * @drv_cmd: Internal command tracker
2366  *
2367  * Issues a target reset TM to the firmware from the device
2368  * removal TM pend list or retry the removal handshake sequence
2369  * based on the IOU control request IOC status.
2370  *
2371  * Return: Nothing
2372  */
mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc * mrioc,struct mpi3mr_drv_cmd * drv_cmd)2373 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
2374 	struct mpi3mr_drv_cmd *drv_cmd)
2375 {
2376 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2377 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2378 
2379 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2380 		goto clear_drv_cmd;
2381 
2382 	ioc_info(mrioc,
2383 	    "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
2384 	    __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
2385 	    drv_cmd->ioc_loginfo);
2386 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2387 		if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
2388 			drv_cmd->retry_count++;
2389 			ioc_info(mrioc,
2390 			    "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
2391 			    __func__, drv_cmd->dev_handle,
2392 			    drv_cmd->retry_count);
2393 			mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
2394 			    drv_cmd, drv_cmd->iou_rc);
2395 			return;
2396 		}
2397 		ioc_err(mrioc,
2398 		    "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
2399 		    __func__, drv_cmd->dev_handle);
2400 	} else {
2401 		ioc_info(mrioc,
2402 		    "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
2403 		    __func__, drv_cmd->dev_handle);
2404 		clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
2405 	}
2406 
2407 	if (!list_empty(&mrioc->delayed_rmhs_list)) {
2408 		delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
2409 		    struct delayed_dev_rmhs_node, list);
2410 		drv_cmd->dev_handle = delayed_dev_rmhs->handle;
2411 		drv_cmd->retry_count = 0;
2412 		drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
2413 		ioc_info(mrioc,
2414 		    "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
2415 		    __func__, drv_cmd->dev_handle);
2416 		mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
2417 		    drv_cmd->iou_rc);
2418 		list_del(&delayed_dev_rmhs->list);
2419 		kfree(delayed_dev_rmhs);
2420 		return;
2421 	}
2422 
2423 clear_drv_cmd:
2424 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2425 	drv_cmd->callback = NULL;
2426 	drv_cmd->retry_count = 0;
2427 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2428 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2429 }
2430 
2431 /**
2432  * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
2433  * @mrioc: Adapter instance reference
2434  * @drv_cmd: Internal command tracker
2435  *
2436  * Issues a target reset TM to the firmware from the device
2437  * removal TM pend list or issue IO unit control request as
2438  * part of device removal or hidden acknowledgment handshake.
2439  *
2440  * Return: Nothing
2441  */
mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc * mrioc,struct mpi3mr_drv_cmd * drv_cmd)2442 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
2443 	struct mpi3mr_drv_cmd *drv_cmd)
2444 {
2445 	struct mpi3_iounit_control_request iou_ctrl;
2446 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2447 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2448 	int retval;
2449 
2450 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2451 		goto clear_drv_cmd;
2452 
2453 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2454 		tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2455 
2456 	if (tm_reply)
2457 		pr_info(IOCNAME
2458 		    "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
2459 		    mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
2460 		    drv_cmd->ioc_loginfo,
2461 		    le32_to_cpu(tm_reply->termination_count));
2462 
2463 	pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
2464 	    mrioc->name, drv_cmd->dev_handle, cmd_idx);
2465 
2466 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
2467 
2468 	drv_cmd->state = MPI3MR_CMD_PENDING;
2469 	drv_cmd->is_waiting = 0;
2470 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
2471 	iou_ctrl.operation = drv_cmd->iou_rc;
2472 	iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
2473 	iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
2474 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
2475 
2476 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
2477 	    1);
2478 	if (retval) {
2479 		pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
2480 		    mrioc->name);
2481 		goto clear_drv_cmd;
2482 	}
2483 
2484 	return;
2485 clear_drv_cmd:
2486 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2487 	drv_cmd->callback = NULL;
2488 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2489 	drv_cmd->retry_count = 0;
2490 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2491 }
2492 
2493 /**
2494  * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
2495  * @mrioc: Adapter instance reference
2496  * @handle: Device handle
2497  * @cmdparam: Internal command tracker
2498  * @iou_rc: IO unit reason code
2499  *
2500  * Issues a target reset TM to the firmware or add it to a pend
2501  * list as part of device removal or hidden acknowledgment
2502  * handshake.
2503  *
2504  * Return: Nothing
2505  */
mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc * mrioc,u16 handle,struct mpi3mr_drv_cmd * cmdparam,u8 iou_rc)2506 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
2507 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
2508 {
2509 	struct mpi3_scsi_task_mgmt_request tm_req;
2510 	int retval = 0;
2511 	u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2512 	u8 retrycount = 5;
2513 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2514 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2515 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2516 	unsigned long flags;
2517 
2518 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2519 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2520 	if (tgtdev && (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE))
2521 		tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
2522 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2523 
2524 	if (drv_cmd)
2525 		goto issue_cmd;
2526 	do {
2527 		cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
2528 		    MPI3MR_NUM_DEVRMCMD);
2529 		if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
2530 			if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
2531 				break;
2532 			cmd_idx = MPI3MR_NUM_DEVRMCMD;
2533 		}
2534 	} while (retrycount--);
2535 
2536 	if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
2537 		delayed_dev_rmhs = kzalloc_obj(*delayed_dev_rmhs, GFP_ATOMIC);
2538 		if (!delayed_dev_rmhs)
2539 			return;
2540 		INIT_LIST_HEAD(&delayed_dev_rmhs->list);
2541 		delayed_dev_rmhs->handle = handle;
2542 		delayed_dev_rmhs->iou_rc = iou_rc;
2543 		list_add_tail(&delayed_dev_rmhs->list,
2544 		    &mrioc->delayed_rmhs_list);
2545 		ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
2546 		    __func__, handle);
2547 		return;
2548 	}
2549 	drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
2550 
2551 issue_cmd:
2552 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2553 	ioc_info(mrioc,
2554 	    "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
2555 	    __func__, handle, cmd_idx);
2556 
2557 	memset(&tm_req, 0, sizeof(tm_req));
2558 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2559 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2560 		goto out;
2561 	}
2562 	drv_cmd->state = MPI3MR_CMD_PENDING;
2563 	drv_cmd->is_waiting = 0;
2564 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
2565 	drv_cmd->dev_handle = handle;
2566 	drv_cmd->iou_rc = iou_rc;
2567 	tm_req.dev_handle = cpu_to_le16(handle);
2568 	tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2569 	tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2570 	tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
2571 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2572 
2573 	set_bit(handle, mrioc->removepend_bitmap);
2574 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2575 	if (retval) {
2576 		ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
2577 		    __func__);
2578 		goto out_failed;
2579 	}
2580 out:
2581 	return;
2582 out_failed:
2583 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2584 	drv_cmd->callback = NULL;
2585 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2586 	drv_cmd->retry_count = 0;
2587 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2588 }
2589 
2590 /**
2591  * mpi3mr_complete_evt_ack - event ack request completion
2592  * @mrioc: Adapter instance reference
2593  * @drv_cmd: Internal command tracker
2594  *
2595  * This is the completion handler for non blocking event
2596  * acknowledgment sent to the firmware and this will issue any
2597  * pending event acknowledgment request.
2598  *
2599  * Return: Nothing
2600  */
mpi3mr_complete_evt_ack(struct mpi3mr_ioc * mrioc,struct mpi3mr_drv_cmd * drv_cmd)2601 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
2602 	struct mpi3mr_drv_cmd *drv_cmd)
2603 {
2604 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2605 	struct delayed_evt_ack_node *delayed_evtack = NULL;
2606 
2607 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2608 		goto clear_drv_cmd;
2609 
2610 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2611 		dprint_event_th(mrioc,
2612 		    "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2613 		    (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2614 		    drv_cmd->ioc_loginfo);
2615 	}
2616 
2617 	if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2618 		delayed_evtack =
2619 			list_entry(mrioc->delayed_evtack_cmds_list.next,
2620 			    struct delayed_evt_ack_node, list);
2621 		mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2622 		    delayed_evtack->event_ctx);
2623 		list_del(&delayed_evtack->list);
2624 		kfree(delayed_evtack);
2625 		return;
2626 	}
2627 clear_drv_cmd:
2628 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2629 	drv_cmd->callback = NULL;
2630 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2631 }
2632 
2633 /**
2634  * mpi3mr_send_event_ack - Issue event acknwoledgment request
2635  * @mrioc: Adapter instance reference
2636  * @event: MPI3 event id
2637  * @cmdparam: Internal command tracker
2638  * @event_ctx: event context
2639  *
2640  * Issues event acknowledgment request to the firmware if there
2641  * is a free command to send the event ack else it to a pend
2642  * list so that it will be processed on a completion of a prior
2643  * event acknowledgment .
2644  *
2645  * Return: Nothing
2646  */
mpi3mr_send_event_ack(struct mpi3mr_ioc * mrioc,u8 event,struct mpi3mr_drv_cmd * cmdparam,u32 event_ctx)2647 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2648 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2649 {
2650 	struct mpi3_event_ack_request evtack_req;
2651 	int retval = 0;
2652 	u8 retrycount = 5;
2653 	u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2654 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2655 	struct delayed_evt_ack_node *delayed_evtack = NULL;
2656 
2657 	if (drv_cmd) {
2658 		dprint_event_th(mrioc,
2659 		    "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2660 		    event, event_ctx);
2661 		goto issue_cmd;
2662 	}
2663 	dprint_event_th(mrioc,
2664 	    "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2665 	    event, event_ctx);
2666 	do {
2667 		cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2668 		    MPI3MR_NUM_EVTACKCMD);
2669 		if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2670 			if (!test_and_set_bit(cmd_idx,
2671 			    mrioc->evtack_cmds_bitmap))
2672 				break;
2673 			cmd_idx = MPI3MR_NUM_EVTACKCMD;
2674 		}
2675 	} while (retrycount--);
2676 
2677 	if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2678 		delayed_evtack = kzalloc_obj(*delayed_evtack, GFP_ATOMIC);
2679 		if (!delayed_evtack)
2680 			return;
2681 		INIT_LIST_HEAD(&delayed_evtack->list);
2682 		delayed_evtack->event = event;
2683 		delayed_evtack->event_ctx = event_ctx;
2684 		list_add_tail(&delayed_evtack->list,
2685 		    &mrioc->delayed_evtack_cmds_list);
2686 		dprint_event_th(mrioc,
2687 		    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2688 		    event, event_ctx);
2689 		return;
2690 	}
2691 	drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2692 
2693 issue_cmd:
2694 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2695 
2696 	memset(&evtack_req, 0, sizeof(evtack_req));
2697 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2698 		dprint_event_th(mrioc,
2699 		    "sending event ack failed due to command in use\n");
2700 		goto out;
2701 	}
2702 	drv_cmd->state = MPI3MR_CMD_PENDING;
2703 	drv_cmd->is_waiting = 0;
2704 	drv_cmd->callback = mpi3mr_complete_evt_ack;
2705 	evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2706 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2707 	evtack_req.event = event;
2708 	evtack_req.event_context = cpu_to_le32(event_ctx);
2709 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2710 	    sizeof(evtack_req), 1);
2711 	if (retval) {
2712 		dprint_event_th(mrioc,
2713 		    "posting event ack request is failed\n");
2714 		goto out_failed;
2715 	}
2716 
2717 	dprint_event_th(mrioc,
2718 	    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2719 	    event, event_ctx);
2720 out:
2721 	return;
2722 out_failed:
2723 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2724 	drv_cmd->callback = NULL;
2725 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2726 }
2727 
2728 /**
2729  * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2730  * @mrioc: Adapter instance reference
2731  * @event_reply: event data
2732  *
2733  * Checks for the reason code and based on that either block I/O
2734  * to device, or unblock I/O to the device, or start the device
2735  * removal handshake with reason as remove with the firmware for
2736  * PCIe devices.
2737  *
2738  * Return: Nothing
2739  */
mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2740 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2741 	struct mpi3_event_notification_reply *event_reply)
2742 {
2743 	struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2744 	    (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2745 	int i;
2746 	u16 handle;
2747 	u8 reason_code;
2748 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2749 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2750 
2751 	for (i = 0; i < topo_evt->num_entries; i++) {
2752 		handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2753 		if (!handle)
2754 			continue;
2755 		reason_code = topo_evt->port_entry[i].port_status;
2756 		scsi_tgt_priv_data =  NULL;
2757 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2758 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2759 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2760 			    tgtdev->starget->hostdata;
2761 		switch (reason_code) {
2762 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2763 			if (scsi_tgt_priv_data) {
2764 				scsi_tgt_priv_data->dev_removed = 1;
2765 				scsi_tgt_priv_data->dev_removedelay = 0;
2766 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2767 			}
2768 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2769 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2770 			break;
2771 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2772 			if (scsi_tgt_priv_data) {
2773 				scsi_tgt_priv_data->dev_removedelay = 1;
2774 				atomic_inc(&scsi_tgt_priv_data->block_io);
2775 			}
2776 			break;
2777 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2778 			if (scsi_tgt_priv_data &&
2779 			    scsi_tgt_priv_data->dev_removedelay) {
2780 				scsi_tgt_priv_data->dev_removedelay = 0;
2781 				atomic_dec_if_positive
2782 				    (&scsi_tgt_priv_data->block_io);
2783 			}
2784 			break;
2785 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2786 		default:
2787 			break;
2788 		}
2789 		if (tgtdev)
2790 			mpi3mr_tgtdev_put(tgtdev);
2791 	}
2792 }
2793 
2794 /**
2795  * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2796  * @mrioc: Adapter instance reference
2797  * @event_reply: event data
2798  *
2799  * Checks for the reason code and based on that either block I/O
2800  * to device, or unblock I/O to the device, or start the device
2801  * removal handshake with reason as remove with the firmware for
2802  * SAS/SATA devices.
2803  *
2804  * Return: Nothing
2805  */
mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2806 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2807 	struct mpi3_event_notification_reply *event_reply)
2808 {
2809 	struct mpi3_event_data_sas_topology_change_list *topo_evt =
2810 	    (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2811 	int i;
2812 	u16 handle;
2813 	u8 reason_code;
2814 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2815 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2816 
2817 	for (i = 0; i < topo_evt->num_entries; i++) {
2818 		handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2819 		if (!handle)
2820 			continue;
2821 		reason_code = topo_evt->phy_entry[i].status &
2822 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2823 		scsi_tgt_priv_data =  NULL;
2824 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2825 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2826 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2827 			    tgtdev->starget->hostdata;
2828 		switch (reason_code) {
2829 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2830 			if (scsi_tgt_priv_data) {
2831 				scsi_tgt_priv_data->dev_removed = 1;
2832 				scsi_tgt_priv_data->dev_removedelay = 0;
2833 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2834 			}
2835 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2836 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2837 			break;
2838 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2839 			if (scsi_tgt_priv_data) {
2840 				scsi_tgt_priv_data->dev_removedelay = 1;
2841 				atomic_inc(&scsi_tgt_priv_data->block_io);
2842 			}
2843 			break;
2844 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2845 			if (scsi_tgt_priv_data &&
2846 			    scsi_tgt_priv_data->dev_removedelay) {
2847 				scsi_tgt_priv_data->dev_removedelay = 0;
2848 				atomic_dec_if_positive
2849 				    (&scsi_tgt_priv_data->block_io);
2850 			}
2851 			break;
2852 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2853 		default:
2854 			break;
2855 		}
2856 		if (tgtdev)
2857 			mpi3mr_tgtdev_put(tgtdev);
2858 	}
2859 }
2860 
2861 /**
2862  * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2863  * @mrioc: Adapter instance reference
2864  * @event_reply: event data
2865  *
2866  * Checks for the reason code and based on that either block I/O
2867  * to device, or unblock I/O to the device, or start the device
2868  * removal handshake with reason as remove/hide acknowledgment
2869  * with the firmware.
2870  *
2871  * Return: Nothing
2872  */
mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2873 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2874 	struct mpi3_event_notification_reply *event_reply)
2875 {
2876 	u16 dev_handle = 0;
2877 	u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2878 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2879 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2880 	struct mpi3_event_data_device_status_change *evtdata =
2881 	    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2882 
2883 	if (mrioc->stop_drv_processing)
2884 		goto out;
2885 
2886 	dev_handle = le16_to_cpu(evtdata->dev_handle);
2887 	dprint_event_th(mrioc,
2888 	    "device status change event top half with rc(0x%02x) for handle(0x%04x)\n",
2889 	    evtdata->reason_code, dev_handle);
2890 
2891 	switch (evtdata->reason_code) {
2892 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2893 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2894 		block = 1;
2895 		break;
2896 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2897 		delete = 1;
2898 		hide = 1;
2899 		break;
2900 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2901 		delete = 1;
2902 		remove = 1;
2903 		break;
2904 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2905 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2906 		ublock = 1;
2907 		break;
2908 	default:
2909 		break;
2910 	}
2911 
2912 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2913 	if (!tgtdev) {
2914 		dprint_event_th(mrioc,
2915 		    "processing device status change event could not identify device for handle(0x%04x)\n",
2916 		    dev_handle);
2917 		goto out;
2918 	}
2919 	if (hide)
2920 		tgtdev->is_hidden = hide;
2921 	if (tgtdev->starget && tgtdev->starget->hostdata) {
2922 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2923 		    tgtdev->starget->hostdata;
2924 		if (block)
2925 			atomic_inc(&scsi_tgt_priv_data->block_io);
2926 		if (delete)
2927 			scsi_tgt_priv_data->dev_removed = 1;
2928 		if (ublock)
2929 			atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2930 	}
2931 	if (remove)
2932 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2933 		    MPI3_CTRL_OP_REMOVE_DEVICE);
2934 	if (hide)
2935 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2936 		    MPI3_CTRL_OP_HIDDEN_ACK);
2937 
2938 out:
2939 	if (tgtdev)
2940 		mpi3mr_tgtdev_put(tgtdev);
2941 }
2942 
2943 /**
2944  * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2945  * @mrioc: Adapter instance reference
2946  * @event_reply: event data
2947  *
2948  * Blocks and unblocks host level I/O based on the reason code
2949  *
2950  * Return: Nothing
2951  */
mpi3mr_preparereset_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2952 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2953 	struct mpi3_event_notification_reply *event_reply)
2954 {
2955 	struct mpi3_event_data_prepare_for_reset *evtdata =
2956 	    (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2957 
2958 	if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2959 		dprint_event_th(mrioc,
2960 		    "prepare for reset event top half with rc=start\n");
2961 		if (mrioc->prepare_for_reset)
2962 			return;
2963 		scsi_block_requests(mrioc->shost);
2964 		mrioc->prepare_for_reset = 1;
2965 		mrioc->prepare_for_reset_timeout_counter = 0;
2966 	} else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2967 		dprint_event_th(mrioc,
2968 		    "prepare for reset top half with rc=abort\n");
2969 		mrioc->prepare_for_reset = 0;
2970 		scsi_unblock_requests(mrioc->shost);
2971 		mrioc->prepare_for_reset_timeout_counter = 0;
2972 	}
2973 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2974 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2975 		mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2976 		    le32_to_cpu(event_reply->event_context));
2977 }
2978 
2979 /**
2980  * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2981  * @mrioc: Adapter instance reference
2982  * @event_reply: event data
2983  *
2984  * Identifies the new shutdown timeout value and update.
2985  *
2986  * Return: Nothing
2987  */
mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)2988 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2989 	struct mpi3_event_notification_reply *event_reply)
2990 {
2991 	struct mpi3_event_data_energy_pack_change *evtdata =
2992 	    (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2993 	u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2994 
2995 	if (shutdown_timeout <= 0) {
2996 		dprint_event_th(mrioc,
2997 		    "%s :Invalid Shutdown Timeout received = %d\n",
2998 		    __func__, shutdown_timeout);
2999 		return;
3000 	}
3001 
3002 	dprint_event_th(mrioc,
3003 	    "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
3004 	    __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
3005 	mrioc->facts.shutdown_timeout = shutdown_timeout;
3006 }
3007 
3008 /**
3009  * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
3010  * @mrioc: Adapter instance reference
3011  * @event_reply: event data
3012  *
3013  * Displays Cable manegemt event details.
3014  *
3015  * Return: Nothing
3016  */
mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)3017 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
3018 	struct mpi3_event_notification_reply *event_reply)
3019 {
3020 	struct mpi3_event_data_cable_management *evtdata =
3021 	    (struct mpi3_event_data_cable_management *)event_reply->event_data;
3022 
3023 	switch (evtdata->status) {
3024 	case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
3025 	{
3026 		ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
3027 		    "Devices connected to this cable are not detected.\n"
3028 		    "This cable requires %d mW of power.\n",
3029 		    evtdata->receptacle_id,
3030 		    le32_to_cpu(evtdata->active_cable_power_requirement));
3031 		break;
3032 	}
3033 	case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
3034 	{
3035 		ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
3036 		    evtdata->receptacle_id);
3037 		break;
3038 	}
3039 	default:
3040 		break;
3041 	}
3042 }
3043 
3044 /**
3045  * mpi3mr_add_event_wait_for_device_refresh - Add Wait for Device Refresh Event
3046  * @mrioc: Adapter instance reference
3047  *
3048  * Add driver specific event to make sure that the driver won't process the
3049  * events until all the devices are refreshed during soft reset.
3050  *
3051  * Return: Nothing
3052  */
mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc * mrioc)3053 void mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc *mrioc)
3054 {
3055 	struct mpi3mr_fwevt *fwevt = NULL;
3056 
3057 	fwevt = mpi3mr_alloc_fwevt(0);
3058 	if (!fwevt) {
3059 		dprint_event_th(mrioc,
3060 		    "failed to schedule bottom half handler for event(0x%02x)\n",
3061 		    MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH);
3062 		return;
3063 	}
3064 	fwevt->mrioc = mrioc;
3065 	fwevt->event_id = MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH;
3066 	fwevt->send_ack = 0;
3067 	fwevt->process_evt = 1;
3068 	fwevt->evt_ctx = 0;
3069 	fwevt->event_data_size = 0;
3070 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
3071 }
3072 
3073 /**
3074  * mpi3mr_os_handle_events - Firmware event handler
3075  * @mrioc: Adapter instance reference
3076  * @event_reply: event data
3077  *
3078  * Identifies whether the event has to be handled and acknowledged,
3079  * and either processes the event in the top-half and/or schedule a
3080  * bottom-half through mpi3mr_fwevt_worker().
3081  *
3082  * Return: Nothing
3083  */
mpi3mr_os_handle_events(struct mpi3mr_ioc * mrioc,struct mpi3_event_notification_reply * event_reply)3084 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
3085 	struct mpi3_event_notification_reply *event_reply)
3086 {
3087 	u16 evt_type, sz;
3088 	struct mpi3mr_fwevt *fwevt = NULL;
3089 	bool ack_req = 0, process_evt_bh = 0;
3090 
3091 	if (mrioc->stop_drv_processing)
3092 		return;
3093 
3094 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
3095 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
3096 		ack_req = 1;
3097 
3098 	evt_type = event_reply->event;
3099 	mpi3mr_event_trigger(mrioc, event_reply->event);
3100 
3101 	switch (evt_type) {
3102 	case MPI3_EVENT_DEVICE_ADDED:
3103 	{
3104 		struct mpi3_device_page0 *dev_pg0 =
3105 		    (struct mpi3_device_page0 *)event_reply->event_data;
3106 		if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
3107 			dprint_event_th(mrioc,
3108 				"failed to process device added event for handle(0x%04x),\n"
3109 				"perst_id(%d) in the event top half handler\n",
3110 				le16_to_cpu(dev_pg0->dev_handle),
3111 				le16_to_cpu(dev_pg0->persistent_id));
3112 		else
3113 			process_evt_bh = 1;
3114 		break;
3115 	}
3116 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
3117 	{
3118 		process_evt_bh = 1;
3119 		mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
3120 		break;
3121 	}
3122 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
3123 	{
3124 		process_evt_bh = 1;
3125 		mpi3mr_sastopochg_evt_th(mrioc, event_reply);
3126 		break;
3127 	}
3128 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
3129 	{
3130 		process_evt_bh = 1;
3131 		mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
3132 		break;
3133 	}
3134 	case MPI3_EVENT_PREPARE_FOR_RESET:
3135 	{
3136 		mpi3mr_preparereset_evt_th(mrioc, event_reply);
3137 		ack_req = 0;
3138 		break;
3139 	}
3140 	case MPI3_EVENT_DIAGNOSTIC_BUFFER_STATUS_CHANGE:
3141 	{
3142 		mpi3mr_hdbstatuschg_evt_th(mrioc, event_reply);
3143 		break;
3144 	}
3145 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
3146 	case MPI3_EVENT_LOG_DATA:
3147 
3148 		sz = event_reply->event_data_length * 4;
3149 		mpi3mr_app_save_logdata_th(mrioc,
3150 			(char *)event_reply->event_data, sz);
3151 		break;
3152 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
3153 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
3154 	{
3155 		process_evt_bh = 1;
3156 		break;
3157 	}
3158 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
3159 	{
3160 		mpi3mr_energypackchg_evt_th(mrioc, event_reply);
3161 		break;
3162 	}
3163 	case MPI3_EVENT_CABLE_MGMT:
3164 	{
3165 		mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
3166 		break;
3167 	}
3168 	case MPI3_EVENT_SAS_DISCOVERY:
3169 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
3170 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
3171 	case MPI3_EVENT_PCIE_ENUMERATION:
3172 		break;
3173 	default:
3174 		ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
3175 		    __func__, evt_type);
3176 		break;
3177 	}
3178 	if (process_evt_bh || ack_req) {
3179 		dprint_event_th(mrioc,
3180 		    "scheduling bottom half handler for event(0x%02x) - (0x%08x), ack_required=%d\n",
3181 		    evt_type, le32_to_cpu(event_reply->event_context), ack_req);
3182 		sz = event_reply->event_data_length * 4;
3183 		fwevt = mpi3mr_alloc_fwevt(sz);
3184 		if (!fwevt) {
3185 			dprint_event_th(mrioc,
3186 				"failed to schedule bottom half handler for\n"
3187 				"event(0x%02x), ack_required=%d\n", evt_type, ack_req);
3188 			return;
3189 		}
3190 
3191 		memcpy(fwevt->event_data, event_reply->event_data, sz);
3192 		fwevt->mrioc = mrioc;
3193 		fwevt->event_id = evt_type;
3194 		fwevt->send_ack = ack_req;
3195 		fwevt->process_evt = process_evt_bh;
3196 		fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
3197 		mpi3mr_fwevt_add_to_list(mrioc, fwevt);
3198 	}
3199 }
3200 
3201 /**
3202  * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
3203  * @mrioc: Adapter instance reference
3204  * @scmd: SCSI command reference
3205  * @scsiio_req: MPI3 SCSI IO request
3206  *
3207  * Identifies the protection information flags from the SCSI
3208  * command and set appropriate flags in the MPI3 SCSI IO
3209  * request.
3210  *
3211  * Return: Nothing
3212  */
mpi3mr_setup_eedp(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req)3213 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
3214 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3215 {
3216 	u16 eedp_flags = 0;
3217 	unsigned char prot_op = scsi_get_prot_op(scmd);
3218 
3219 	switch (prot_op) {
3220 	case SCSI_PROT_NORMAL:
3221 		return;
3222 	case SCSI_PROT_READ_STRIP:
3223 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
3224 		break;
3225 	case SCSI_PROT_WRITE_INSERT:
3226 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
3227 		break;
3228 	case SCSI_PROT_READ_INSERT:
3229 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
3230 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3231 		break;
3232 	case SCSI_PROT_WRITE_STRIP:
3233 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
3234 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3235 		break;
3236 	case SCSI_PROT_READ_PASS:
3237 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
3238 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3239 		break;
3240 	case SCSI_PROT_WRITE_PASS:
3241 		if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
3242 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
3243 			scsiio_req->sgl[0].eedp.application_tag_translation_mask =
3244 			    0xffff;
3245 		} else
3246 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
3247 
3248 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3249 		break;
3250 	default:
3251 		return;
3252 	}
3253 
3254 	if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
3255 		eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
3256 
3257 	if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
3258 		eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
3259 
3260 	if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
3261 		eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
3262 			MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3263 		scsiio_req->cdb.eedp32.primary_reference_tag =
3264 			cpu_to_be32(scsi_prot_ref_tag(scmd));
3265 	}
3266 
3267 	if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
3268 		eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3269 
3270 	eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
3271 
3272 	switch (scsi_prot_interval(scmd)) {
3273 	case 512:
3274 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
3275 		break;
3276 	case 520:
3277 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
3278 		break;
3279 	case 4080:
3280 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
3281 		break;
3282 	case 4088:
3283 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
3284 		break;
3285 	case 4096:
3286 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
3287 		break;
3288 	case 4104:
3289 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
3290 		break;
3291 	case 4160:
3292 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
3293 		break;
3294 	default:
3295 		break;
3296 	}
3297 
3298 	scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
3299 	scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
3300 }
3301 
3302 /**
3303  * mpi3mr_build_sense_buffer - Map sense information
3304  * @desc: Sense type
3305  * @buf: Sense buffer to populate
3306  * @key: Sense key
3307  * @asc: Additional sense code
3308  * @ascq: Additional sense code qualifier
3309  *
3310  * Maps the given sense information into either descriptor or
3311  * fixed format sense data.
3312  *
3313  * Return: Nothing
3314  */
mpi3mr_build_sense_buffer(int desc,u8 * buf,u8 key,u8 asc,u8 ascq)3315 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
3316 	u8 asc, u8 ascq)
3317 {
3318 	if (desc) {
3319 		buf[0] = 0x72;	/* descriptor, current */
3320 		buf[1] = key;
3321 		buf[2] = asc;
3322 		buf[3] = ascq;
3323 		buf[7] = 0;
3324 	} else {
3325 		buf[0] = 0x70;	/* fixed, current */
3326 		buf[2] = key;
3327 		buf[7] = 0xa;
3328 		buf[12] = asc;
3329 		buf[13] = ascq;
3330 	}
3331 }
3332 
3333 /**
3334  * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
3335  * @scmd: SCSI command reference
3336  * @ioc_status: status of MPI3 request
3337  *
3338  * Maps the EEDP error status of the SCSI IO request to sense
3339  * data.
3340  *
3341  * Return: Nothing
3342  */
mpi3mr_map_eedp_error(struct scsi_cmnd * scmd,u16 ioc_status)3343 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
3344 	u16 ioc_status)
3345 {
3346 	u8 ascq = 0;
3347 
3348 	switch (ioc_status) {
3349 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3350 		ascq = 0x01;
3351 		break;
3352 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3353 		ascq = 0x02;
3354 		break;
3355 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3356 		ascq = 0x03;
3357 		break;
3358 	default:
3359 		ascq = 0x00;
3360 		break;
3361 	}
3362 
3363 	mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3364 	    0x10, ascq);
3365 	scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
3366 }
3367 
3368 /**
3369  * mpi3mr_process_op_reply_desc - reply descriptor handler
3370  * @mrioc: Adapter instance reference
3371  * @reply_desc: Operational reply descriptor
3372  * @reply_dma: place holder for reply DMA address
3373  * @qidx: Operational queue index
3374  *
3375  * Process the operational reply descriptor and identifies the
3376  * descriptor type. Based on the descriptor map the MPI3 request
3377  * status to a SCSI command status and calls scsi_done call
3378  * back.
3379  *
3380  * Return: Nothing
3381  */
mpi3mr_process_op_reply_desc(struct mpi3mr_ioc * mrioc,struct mpi3_default_reply_descriptor * reply_desc,u64 * reply_dma,u16 qidx)3382 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
3383 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
3384 {
3385 	u16 reply_desc_type, host_tag = 0;
3386 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3387 	u32 ioc_loginfo = 0;
3388 	struct mpi3_status_reply_descriptor *status_desc = NULL;
3389 	struct mpi3_address_reply_descriptor *addr_desc = NULL;
3390 	struct mpi3_success_reply_descriptor *success_desc = NULL;
3391 	struct mpi3_scsi_io_reply *scsi_reply = NULL;
3392 	struct scsi_cmnd *scmd = NULL;
3393 	struct scmd_priv *priv = NULL;
3394 	u8 *sense_buf = NULL;
3395 	u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
3396 	u32 xfer_count = 0, sense_count = 0, resp_data = 0;
3397 	u16 dev_handle = 0xFFFF;
3398 	struct scsi_sense_hdr sshdr;
3399 	struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
3400 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3401 	u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
3402 	struct mpi3mr_throttle_group_info *tg = NULL;
3403 	u8 throttle_enabled_dev = 0;
3404 
3405 	*reply_dma = 0;
3406 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
3407 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
3408 	switch (reply_desc_type) {
3409 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
3410 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
3411 		host_tag = le16_to_cpu(status_desc->host_tag);
3412 		ioc_status = le16_to_cpu(status_desc->ioc_status);
3413 		if (ioc_status &
3414 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3415 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
3416 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3417 		mpi3mr_reply_trigger(mrioc, ioc_status, ioc_loginfo);
3418 		break;
3419 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
3420 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
3421 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
3422 		scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
3423 		    *reply_dma);
3424 		if (!scsi_reply) {
3425 			panic("%s: scsi_reply is NULL, this shouldn't happen\n",
3426 			    mrioc->name);
3427 			goto out;
3428 		}
3429 		host_tag = le16_to_cpu(scsi_reply->host_tag);
3430 		ioc_status = le16_to_cpu(scsi_reply->ioc_status);
3431 		scsi_status = scsi_reply->scsi_status;
3432 		scsi_state = scsi_reply->scsi_state;
3433 		dev_handle = le16_to_cpu(scsi_reply->dev_handle);
3434 		sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
3435 		xfer_count = le32_to_cpu(scsi_reply->transfer_count);
3436 		sense_count = le32_to_cpu(scsi_reply->sense_count);
3437 		resp_data = le32_to_cpu(scsi_reply->response_data);
3438 		sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
3439 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
3440 		if (ioc_status &
3441 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3442 			ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
3443 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3444 		if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
3445 			panic("%s: Ran out of sense buffers\n", mrioc->name);
3446 		if (sense_buf) {
3447 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3448 			mpi3mr_scsisense_trigger(mrioc, sshdr.sense_key,
3449 			    sshdr.asc, sshdr.ascq);
3450 		}
3451 		mpi3mr_reply_trigger(mrioc, ioc_status, ioc_loginfo);
3452 		break;
3453 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
3454 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
3455 		host_tag = le16_to_cpu(success_desc->host_tag);
3456 		break;
3457 	default:
3458 		break;
3459 	}
3460 	scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
3461 	if (!scmd) {
3462 		ioc_err(mrioc, "Cannot Identify scmd for host_tag 0x%x", host_tag);
3463 		ioc_err(mrioc,
3464 		    "reply_desc_type(%d) host_tag(%d(0x%04x)): qid(%d): command issued to\n"
3465 		    "handle(0x%04x) returned with ioc_status(0x%04x), log_info(0x%08x),\n"
3466 		    "scsi_state(0x%02x), scsi_status(0x%02x), xfer_count(%d), resp_data(0x%08x)\n",
3467 		    reply_desc_type, host_tag, host_tag, qidx+1, dev_handle, ioc_status,
3468 		    ioc_loginfo, scsi_state, scsi_status,  xfer_count,
3469 		    resp_data);
3470 		mrioc->invalid_io_comp = 1;
3471 		goto out;
3472 	}
3473 	priv = scsi_cmd_priv(scmd);
3474 
3475 	data_len_blks = scsi_bufflen(scmd) >> 9;
3476 	sdev_priv_data = scmd->device->hostdata;
3477 	if (sdev_priv_data) {
3478 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
3479 		if (stgt_priv_data) {
3480 			tg = stgt_priv_data->throttle_group;
3481 			throttle_enabled_dev =
3482 			    stgt_priv_data->io_throttle_enabled;
3483 			dev_handle = stgt_priv_data->dev_handle;
3484 		}
3485 	}
3486 	if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
3487 	    throttle_enabled_dev)) {
3488 		ioc_pend_data_len = atomic_sub_return(data_len_blks,
3489 		    &mrioc->pend_large_data_sz);
3490 		if (tg) {
3491 			tg_pend_data_len = atomic_sub_return(data_len_blks,
3492 			    &tg->pend_large_data_sz);
3493 			if (tg->io_divert  && ((ioc_pend_data_len <=
3494 			    mrioc->io_throttle_low) &&
3495 			    (tg_pend_data_len <= tg->low))) {
3496 				tg->io_divert = 0;
3497 				mpi3mr_set_io_divert_for_all_vd_in_tg(
3498 				    mrioc, tg, 0);
3499 			}
3500 		} else {
3501 			if (ioc_pend_data_len <= mrioc->io_throttle_low)
3502 				stgt_priv_data->io_divert = 0;
3503 		}
3504 	} else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
3505 		ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
3506 		if (!tg) {
3507 			if (ioc_pend_data_len <= mrioc->io_throttle_low)
3508 				stgt_priv_data->io_divert = 0;
3509 
3510 		} else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
3511 			tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
3512 			if (tg->io_divert  && (tg_pend_data_len <= tg->low)) {
3513 				tg->io_divert = 0;
3514 				mpi3mr_set_io_divert_for_all_vd_in_tg(
3515 				    mrioc, tg, 0);
3516 			}
3517 		}
3518 	}
3519 
3520 	if (success_desc) {
3521 		scmd->result = DID_OK << 16;
3522 		goto out_success;
3523 	}
3524 
3525 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
3526 	if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
3527 	    xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
3528 	    scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
3529 	    scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
3530 		ioc_status = MPI3_IOCSTATUS_SUCCESS;
3531 
3532 	if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
3533 	    sense_buf) {
3534 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
3535 
3536 		memcpy(scmd->sense_buffer, sense_buf, sz);
3537 	}
3538 
3539 	switch (ioc_status) {
3540 	case MPI3_IOCSTATUS_BUSY:
3541 	case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
3542 		scmd->result = SAM_STAT_BUSY;
3543 		break;
3544 	case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3545 		scmd->result = DID_NO_CONNECT << 16;
3546 		break;
3547 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3548 		if (ioc_loginfo == IOC_LOGINFO_SATA_NCQ_FAIL_AFTER_ERR) {
3549 			/*
3550 			 * This is a ATA NCQ command aborted due to another NCQ
3551 			 * command failure. We must retry this command
3552 			 * immediately but without incrementing its retry
3553 			 * counter.
3554 			 */
3555 			WARN_ON_ONCE(xfer_count != 0);
3556 			scmd->result = DID_IMM_RETRY << 16;
3557 		} else {
3558 			scmd->result = DID_SOFT_ERROR << 16;
3559 		}
3560 		break;
3561 	case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
3562 	case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
3563 		scmd->result = DID_RESET << 16;
3564 		break;
3565 	case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3566 		if ((xfer_count == 0) || (scmd->underflow > xfer_count))
3567 			scmd->result = DID_SOFT_ERROR << 16;
3568 		else
3569 			scmd->result = (DID_OK << 16) | scsi_status;
3570 		break;
3571 	case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
3572 		scmd->result = (DID_OK << 16) | scsi_status;
3573 		if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
3574 			break;
3575 		if (xfer_count < scmd->underflow) {
3576 			if (scsi_status == SAM_STAT_BUSY)
3577 				scmd->result = SAM_STAT_BUSY;
3578 			else
3579 				scmd->result = DID_SOFT_ERROR << 16;
3580 		} else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3581 		    (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
3582 			scmd->result = DID_SOFT_ERROR << 16;
3583 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3584 			scmd->result = DID_RESET << 16;
3585 		break;
3586 	case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
3587 		scsi_set_resid(scmd, 0);
3588 		fallthrough;
3589 	case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
3590 	case MPI3_IOCSTATUS_SUCCESS:
3591 		scmd->result = (DID_OK << 16) | scsi_status;
3592 		if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3593 		    (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
3594 			(sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
3595 			scmd->result = DID_SOFT_ERROR << 16;
3596 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3597 			scmd->result = DID_RESET << 16;
3598 		break;
3599 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3600 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3601 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3602 		mpi3mr_map_eedp_error(scmd, ioc_status);
3603 		break;
3604 	case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3605 	case MPI3_IOCSTATUS_INVALID_FUNCTION:
3606 	case MPI3_IOCSTATUS_INVALID_SGL:
3607 	case MPI3_IOCSTATUS_INTERNAL_ERROR:
3608 	case MPI3_IOCSTATUS_INVALID_FIELD:
3609 	case MPI3_IOCSTATUS_INVALID_STATE:
3610 	case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
3611 	case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3612 	case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
3613 	default:
3614 		scmd->result = DID_SOFT_ERROR << 16;
3615 		break;
3616 	}
3617 
3618 	if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
3619 	    (scmd->cmnd[0] != ATA_16) &&
3620 	    mrioc->logging_level & MPI3_DEBUG_SCSI_ERROR) {
3621 		ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
3622 		    scmd->result);
3623 		scsi_print_command(scmd);
3624 		ioc_info(mrioc,
3625 		    "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
3626 		    __func__, dev_handle, ioc_status, ioc_loginfo,
3627 		    priv->req_q_idx + 1);
3628 		ioc_info(mrioc,
3629 		    " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
3630 		    host_tag, scsi_state, scsi_status, xfer_count, resp_data);
3631 		if (sense_buf) {
3632 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3633 			ioc_info(mrioc,
3634 			    "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
3635 			    __func__, sense_count, sshdr.sense_key,
3636 			    sshdr.asc, sshdr.ascq);
3637 		}
3638 	}
3639 out_success:
3640 	if (priv->meta_sg_valid) {
3641 		dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
3642 		    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
3643 	}
3644 	mpi3mr_clear_scmd_priv(mrioc, scmd);
3645 	scsi_dma_unmap(scmd);
3646 	scsi_done(scmd);
3647 out:
3648 	if (sense_buf)
3649 		mpi3mr_repost_sense_buf(mrioc,
3650 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
3651 }
3652 
3653 /**
3654  * mpi3mr_get_chain_idx - get free chain buffer index
3655  * @mrioc: Adapter instance reference
3656  *
3657  * Try to get a free chain buffer index from the free pool.
3658  *
3659  * Return: -1 on failure or the free chain buffer index
3660  */
mpi3mr_get_chain_idx(struct mpi3mr_ioc * mrioc)3661 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
3662 {
3663 	u8 retry_count = 5;
3664 	int cmd_idx = -1;
3665 	unsigned long flags;
3666 
3667 	spin_lock_irqsave(&mrioc->chain_buf_lock, flags);
3668 	do {
3669 		cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
3670 		    mrioc->chain_buf_count);
3671 		if (cmd_idx < mrioc->chain_buf_count) {
3672 			set_bit(cmd_idx, mrioc->chain_bitmap);
3673 			break;
3674 		}
3675 		cmd_idx = -1;
3676 	} while (retry_count--);
3677 	spin_unlock_irqrestore(&mrioc->chain_buf_lock, flags);
3678 	return cmd_idx;
3679 }
3680 
3681 /**
3682  * mpi3mr_prepare_sg_scmd - build scatter gather list
3683  * @mrioc: Adapter instance reference
3684  * @scmd: SCSI command reference
3685  * @scsiio_req: MPI3 SCSI IO request
3686  *
3687  * This function maps SCSI command's data and protection SGEs to
3688  * MPI request SGEs. If required additional 4K chain buffer is
3689  * used to send the SGEs.
3690  *
3691  * Return: 0 on success, -ENOMEM on dma_map_sg failure
3692  */
mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req)3693 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3694 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3695 {
3696 	dma_addr_t chain_dma;
3697 	struct scatterlist *sg_scmd;
3698 	void *sg_local, *chain;
3699 	u32 chain_length;
3700 	int sges_left, chain_idx;
3701 	u32 sges_in_segment;
3702 	u8 simple_sgl_flags;
3703 	u8 simple_sgl_flags_last;
3704 	u8 last_chain_sgl_flags;
3705 	struct chain_element *chain_req;
3706 	struct scmd_priv *priv = NULL;
3707 	u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3708 	    MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3709 
3710 	priv = scsi_cmd_priv(scmd);
3711 
3712 	simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3713 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
3714 	simple_sgl_flags_last = simple_sgl_flags |
3715 	    MPI3_SGE_FLAGS_END_OF_LIST;
3716 	last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3717 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
3718 
3719 	if (meta_sg)
3720 		sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3721 	else
3722 		sg_local = &scsiio_req->sgl;
3723 
3724 	if (!scsiio_req->data_length && !meta_sg) {
3725 		mpi3mr_build_zero_len_sge(sg_local);
3726 		return 0;
3727 	}
3728 
3729 	if (meta_sg) {
3730 		sg_scmd = scsi_prot_sglist(scmd);
3731 		sges_left = dma_map_sg(&mrioc->pdev->dev,
3732 		    scsi_prot_sglist(scmd),
3733 		    scsi_prot_sg_count(scmd),
3734 		    scmd->sc_data_direction);
3735 		priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3736 	} else {
3737 		/*
3738 		 * Some firmware versions byte-swap the REPORT ZONES command
3739 		 * reply from ATA-ZAC devices by directly accessing in the host
3740 		 * buffer. This does not respect the default command DMA
3741 		 * direction and causes IOMMU page faults on some architectures
3742 		 * with an IOMMU enforcing write mappings (e.g. AMD hosts).
3743 		 * Avoid such issue by making the REPORT ZONES buffer mapping
3744 		 * bi-directional.
3745 		 */
3746 		if (scmd->cmnd[0] == ZBC_IN && scmd->cmnd[1] == ZI_REPORT_ZONES)
3747 			scmd->sc_data_direction = DMA_BIDIRECTIONAL;
3748 		sg_scmd = scsi_sglist(scmd);
3749 		sges_left = scsi_dma_map(scmd);
3750 	}
3751 
3752 	if (sges_left < 0) {
3753 		sdev_printk(KERN_ERR, scmd->device,
3754 		    "scsi_dma_map failed: request for %d bytes!\n",
3755 		    scsi_bufflen(scmd));
3756 		return -ENOMEM;
3757 	}
3758 	if (sges_left > mrioc->max_sgl_entries) {
3759 		sdev_printk(KERN_ERR, scmd->device,
3760 		    "scsi_dma_map returned unsupported sge count %d!\n",
3761 		    sges_left);
3762 		return -ENOMEM;
3763 	}
3764 
3765 	sges_in_segment = (mrioc->facts.op_req_sz -
3766 	    offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3767 
3768 	if (scsiio_req->sgl[0].eedp.flags ==
3769 	    MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3770 		sg_local += sizeof(struct mpi3_sge_common);
3771 		sges_in_segment--;
3772 		/* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3773 	}
3774 
3775 	if (scsiio_req->msg_flags ==
3776 	    MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3777 		sges_in_segment--;
3778 		/* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3779 	}
3780 
3781 	if (meta_sg)
3782 		sges_in_segment = 1;
3783 
3784 	if (sges_left <= sges_in_segment)
3785 		goto fill_in_last_segment;
3786 
3787 	/* fill in main message segment when there is a chain following */
3788 	while (sges_in_segment > 1) {
3789 		mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3790 		    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3791 		sg_scmd = sg_next(sg_scmd);
3792 		sg_local += sizeof(struct mpi3_sge_common);
3793 		sges_left--;
3794 		sges_in_segment--;
3795 	}
3796 
3797 	chain_idx = mpi3mr_get_chain_idx(mrioc);
3798 	if (chain_idx < 0)
3799 		return -1;
3800 	chain_req = &mrioc->chain_sgl_list[chain_idx];
3801 	if (meta_sg)
3802 		priv->meta_chain_idx = chain_idx;
3803 	else
3804 		priv->chain_idx = chain_idx;
3805 
3806 	chain = chain_req->addr;
3807 	chain_dma = chain_req->dma_addr;
3808 	sges_in_segment = sges_left;
3809 	chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3810 
3811 	mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3812 	    chain_length, chain_dma);
3813 
3814 	sg_local = chain;
3815 
3816 fill_in_last_segment:
3817 	while (sges_left > 0) {
3818 		if (sges_left == 1)
3819 			mpi3mr_add_sg_single(sg_local,
3820 			    simple_sgl_flags_last, sg_dma_len(sg_scmd),
3821 			    sg_dma_address(sg_scmd));
3822 		else
3823 			mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3824 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3825 		sg_scmd = sg_next(sg_scmd);
3826 		sg_local += sizeof(struct mpi3_sge_common);
3827 		sges_left--;
3828 	}
3829 
3830 	return 0;
3831 }
3832 
3833 /**
3834  * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3835  * @mrioc: Adapter instance reference
3836  * @scmd: SCSI command reference
3837  * @scsiio_req: MPI3 SCSI IO request
3838  *
3839  * This function calls mpi3mr_prepare_sg_scmd for constructing
3840  * both data SGEs and protection information SGEs in the MPI
3841  * format from the SCSI Command as appropriate .
3842  *
3843  * Return: return value of mpi3mr_prepare_sg_scmd.
3844  */
mpi3mr_build_sg_scmd(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req)3845 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3846 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3847 {
3848 	int ret;
3849 
3850 	ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3851 	if (ret)
3852 		return ret;
3853 
3854 	if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3855 		/* There is a valid meta sg */
3856 		scsiio_req->flags |=
3857 		    cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3858 		ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3859 	}
3860 
3861 	return ret;
3862 }
3863 
3864 /**
3865  * mpi3mr_tm_response_name -  get TM response as a string
3866  * @resp_code: TM response code
3867  *
3868  * Convert known task management response code as a readable
3869  * string.
3870  *
3871  * Return: response code string.
3872  */
mpi3mr_tm_response_name(u8 resp_code)3873 static const char *mpi3mr_tm_response_name(u8 resp_code)
3874 {
3875 	char *desc;
3876 
3877 	switch (resp_code) {
3878 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3879 		desc = "task management request completed";
3880 		break;
3881 	case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3882 		desc = "invalid frame";
3883 		break;
3884 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3885 		desc = "task management request not supported";
3886 		break;
3887 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3888 		desc = "task management request failed";
3889 		break;
3890 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3891 		desc = "task management request succeeded";
3892 		break;
3893 	case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3894 		desc = "invalid LUN";
3895 		break;
3896 	case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3897 		desc = "overlapped tag attempted";
3898 		break;
3899 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3900 		desc = "task queued, however not sent to target";
3901 		break;
3902 	case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3903 		desc = "task management request denied by NVMe device";
3904 		break;
3905 	default:
3906 		desc = "unknown";
3907 		break;
3908 	}
3909 
3910 	return desc;
3911 }
3912 
mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc * mrioc)3913 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3914 {
3915 	int i;
3916 	int num_of_reply_queues =
3917 	    mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3918 
3919 	for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3920 		mpi3mr_process_op_reply_q(mrioc,
3921 		    mrioc->intr_info[i].op_reply_q);
3922 }
3923 
3924 /**
3925  * mpi3mr_issue_tm - Issue Task Management request
3926  * @mrioc: Adapter instance reference
3927  * @tm_type: Task Management type
3928  * @handle: Device handle
3929  * @lun: lun ID
3930  * @htag: Host tag of the TM request
3931  * @timeout: TM timeout value
3932  * @drv_cmd: Internal command tracker
3933  * @resp_code: Response code place holder
3934  * @scmd: SCSI command
3935  *
3936  * Issues a Task Management Request to the controller for a
3937  * specified target, lun and command and wait for its completion
3938  * and check TM response. Recover the TM if it timed out by
3939  * issuing controller reset.
3940  *
3941  * Return: 0 on success, non-zero on errors
3942  */
mpi3mr_issue_tm(struct mpi3mr_ioc * mrioc,u8 tm_type,u16 handle,uint lun,u16 htag,ulong timeout,struct mpi3mr_drv_cmd * drv_cmd,u8 * resp_code,struct scsi_cmnd * scmd)3943 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3944 	u16 handle, uint lun, u16 htag, ulong timeout,
3945 	struct mpi3mr_drv_cmd *drv_cmd,
3946 	u8 *resp_code, struct scsi_cmnd *scmd)
3947 {
3948 	struct mpi3_scsi_task_mgmt_request tm_req;
3949 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3950 	int retval = 0;
3951 	struct mpi3mr_tgt_dev *tgtdev = NULL;
3952 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3953 	struct scmd_priv *cmd_priv = NULL;
3954 	struct scsi_device *sdev = NULL;
3955 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3956 
3957 	ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3958 	     __func__, tm_type, handle);
3959 	if (mrioc->unrecoverable) {
3960 		retval = -1;
3961 		ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3962 		    __func__);
3963 		goto out;
3964 	}
3965 
3966 	memset(&tm_req, 0, sizeof(tm_req));
3967 	mutex_lock(&drv_cmd->mutex);
3968 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3969 		retval = -1;
3970 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3971 		mutex_unlock(&drv_cmd->mutex);
3972 		goto out;
3973 	}
3974 	if (mrioc->reset_in_progress) {
3975 		retval = -1;
3976 		ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3977 		mutex_unlock(&drv_cmd->mutex);
3978 		goto out;
3979 	}
3980 	if (mrioc->block_on_pci_err) {
3981 		retval = -1;
3982 		dprint_tm(mrioc, "sending task management failed due to\n"
3983 				"pci error recovery in progress\n");
3984 		mutex_unlock(&drv_cmd->mutex);
3985 		goto out;
3986 	}
3987 
3988 	drv_cmd->state = MPI3MR_CMD_PENDING;
3989 	drv_cmd->is_waiting = 1;
3990 	drv_cmd->callback = NULL;
3991 	tm_req.dev_handle = cpu_to_le16(handle);
3992 	tm_req.task_type = tm_type;
3993 	tm_req.host_tag = cpu_to_le16(htag);
3994 
3995 	int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3996 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3997 
3998 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3999 
4000 	if (scmd) {
4001 		if (tm_type == MPI3_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
4002 			cmd_priv = scsi_cmd_priv(scmd);
4003 			if (!cmd_priv)
4004 				goto out_unlock;
4005 
4006 			struct op_req_qinfo *op_req_q;
4007 
4008 			op_req_q = &mrioc->req_qinfo[cmd_priv->req_q_idx];
4009 			tm_req.task_host_tag = cpu_to_le16(cmd_priv->host_tag);
4010 			tm_req.task_request_queue_id =
4011 				cpu_to_le16(op_req_q->qid);
4012 		}
4013 		sdev = scmd->device;
4014 		sdev_priv_data = sdev->hostdata;
4015 		scsi_tgt_priv_data = ((sdev_priv_data) ?
4016 		    sdev_priv_data->tgt_priv_data : NULL);
4017 	} else {
4018 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
4019 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
4020 			    tgtdev->starget->hostdata;
4021 	}
4022 
4023 	if (scsi_tgt_priv_data)
4024 		atomic_inc(&scsi_tgt_priv_data->block_io);
4025 
4026 	if (tgtdev) {
4027 		if (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4028 			timeout = cmd_priv ? tgtdev->dev_spec.pcie_inf.abort_to
4029 					   : tgtdev->dev_spec.pcie_inf.reset_to;
4030 		else if (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4031 			timeout = cmd_priv ? tgtdev->dev_spec.vd_inf.abort_to
4032 					   : tgtdev->dev_spec.vd_inf.reset_to;
4033 	}
4034 
4035 	init_completion(&drv_cmd->done);
4036 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
4037 	if (retval) {
4038 		ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
4039 		goto out_unlock;
4040 	}
4041 	wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
4042 
4043 	if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
4044 		drv_cmd->is_waiting = 0;
4045 		retval = -1;
4046 		if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
4047 			dprint_tm(mrioc,
4048 			    "task management request timed out after %ld seconds\n",
4049 			    timeout);
4050 			if (mrioc->logging_level & MPI3_DEBUG_TM)
4051 				dprint_dump_req(&tm_req, sizeof(tm_req)/4);
4052 			mpi3mr_soft_reset_handler(mrioc,
4053 			    MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
4054 		}
4055 		goto out_unlock;
4056 	}
4057 
4058 	if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
4059 		dprint_tm(mrioc, "invalid task management reply message\n");
4060 		retval = -1;
4061 		goto out_unlock;
4062 	}
4063 
4064 	tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
4065 
4066 	switch (drv_cmd->ioc_status) {
4067 	case MPI3_IOCSTATUS_SUCCESS:
4068 		*resp_code = le32_to_cpu(tm_reply->response_data) &
4069 			MPI3MR_RI_MASK_RESPCODE;
4070 		break;
4071 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
4072 		*resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
4073 		break;
4074 	default:
4075 		dprint_tm(mrioc,
4076 		    "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
4077 		    handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
4078 		retval = -1;
4079 		goto out_unlock;
4080 	}
4081 
4082 	switch (*resp_code) {
4083 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
4084 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
4085 		break;
4086 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
4087 		if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
4088 			retval = -1;
4089 		break;
4090 	default:
4091 		retval = -1;
4092 		break;
4093 	}
4094 
4095 	dprint_tm(mrioc,
4096 	    "task management request type(%d) completed for handle(0x%04x) with ioc_status(0x%04x), log_info(0x%08x), termination_count(%d), response:%s(0x%x)\n",
4097 	    tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
4098 	    le32_to_cpu(tm_reply->termination_count),
4099 	    mpi3mr_tm_response_name(*resp_code), *resp_code);
4100 
4101 	if (!retval) {
4102 		mpi3mr_ioc_disable_intr(mrioc);
4103 		mpi3mr_poll_pend_io_completions(mrioc);
4104 		mpi3mr_ioc_enable_intr(mrioc);
4105 		mpi3mr_poll_pend_io_completions(mrioc);
4106 		mpi3mr_process_admin_reply_q(mrioc);
4107 	}
4108 	switch (tm_type) {
4109 	case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4110 		if (!scsi_tgt_priv_data)
4111 			break;
4112 		scsi_tgt_priv_data->pend_count = 0;
4113 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
4114 		    mpi3mr_count_tgt_pending,
4115 		    (void *)scsi_tgt_priv_data->starget);
4116 		break;
4117 	case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
4118 		if (!sdev_priv_data)
4119 			break;
4120 		sdev_priv_data->pend_count = 0;
4121 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
4122 		    mpi3mr_count_dev_pending, (void *)sdev);
4123 		break;
4124 	default:
4125 		break;
4126 	}
4127 	mpi3mr_global_trigger(mrioc,
4128 	    MPI3_DRIVER2_GLOBALTRIGGER_TASK_MANAGEMENT_ENABLED);
4129 
4130 out_unlock:
4131 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
4132 	mutex_unlock(&drv_cmd->mutex);
4133 	if (scsi_tgt_priv_data)
4134 		atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
4135 	if (tgtdev)
4136 		mpi3mr_tgtdev_put(tgtdev);
4137 out:
4138 	return retval;
4139 }
4140 
4141 /**
4142  * mpi3mr_bios_param - BIOS param callback
4143  * @sdev: SCSI device reference
4144  * @unused: gendisk reference
4145  * @capacity: Capacity in logical sectors
4146  * @params: Parameter array
4147  *
4148  * Just the parameters with heads/secots/cylinders.
4149  *
4150  * Return: 0 always
4151  */
mpi3mr_bios_param(struct scsi_device * sdev,struct gendisk * unused,sector_t capacity,int params[])4152 static int mpi3mr_bios_param(struct scsi_device *sdev,
4153 	struct gendisk *unused, sector_t capacity, int params[])
4154 {
4155 	int heads;
4156 	int sectors;
4157 	sector_t cylinders;
4158 	ulong dummy;
4159 
4160 	heads = 64;
4161 	sectors = 32;
4162 
4163 	dummy = heads * sectors;
4164 	cylinders = capacity;
4165 	sector_div(cylinders, dummy);
4166 
4167 	if ((ulong)capacity >= 0x200000) {
4168 		heads = 255;
4169 		sectors = 63;
4170 		dummy = heads * sectors;
4171 		cylinders = capacity;
4172 		sector_div(cylinders, dummy);
4173 	}
4174 
4175 	params[0] = heads;
4176 	params[1] = sectors;
4177 	params[2] = cylinders;
4178 	return 0;
4179 }
4180 
4181 /**
4182  * mpi3mr_map_queues - Map queues callback handler
4183  * @shost: SCSI host reference
4184  *
4185  * Maps default and poll queues.
4186  *
4187  * Return: return zero.
4188  */
mpi3mr_map_queues(struct Scsi_Host * shost)4189 static void mpi3mr_map_queues(struct Scsi_Host *shost)
4190 {
4191 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4192 	int i, qoff, offset;
4193 	struct blk_mq_queue_map *map = NULL;
4194 
4195 	offset = mrioc->op_reply_q_offset;
4196 
4197 	for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
4198 		map = &shost->tag_set.map[i];
4199 
4200 		map->nr_queues  = 0;
4201 
4202 		if (i == HCTX_TYPE_DEFAULT)
4203 			map->nr_queues = mrioc->default_qcount;
4204 		else if (i == HCTX_TYPE_POLL)
4205 			map->nr_queues = mrioc->active_poll_qcount;
4206 
4207 		if (!map->nr_queues) {
4208 			BUG_ON(i == HCTX_TYPE_DEFAULT);
4209 			continue;
4210 		}
4211 
4212 		/*
4213 		 * The poll queue(s) doesn't have an IRQ (and hence IRQ
4214 		 * affinity), so use the regular blk-mq cpu mapping
4215 		 */
4216 		map->queue_offset = qoff;
4217 		if (i != HCTX_TYPE_POLL)
4218 			blk_mq_map_hw_queues(map, &mrioc->pdev->dev, offset);
4219 		else
4220 			blk_mq_map_queues(map);
4221 
4222 		qoff += map->nr_queues;
4223 		offset += map->nr_queues;
4224 	}
4225 }
4226 
4227 /**
4228  * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
4229  * @mrioc: Adapter instance reference
4230  *
4231  * Calculate the pending I/Os for the controller and return.
4232  *
4233  * Return: Number of pending I/Os
4234  */
mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc * mrioc)4235 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
4236 {
4237 	u16 i;
4238 	uint pend_ios = 0;
4239 
4240 	for (i = 0; i < mrioc->num_op_reply_q; i++)
4241 		pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
4242 	return pend_ios;
4243 }
4244 
4245 /**
4246  * mpi3mr_print_pending_host_io - print pending I/Os
4247  * @mrioc: Adapter instance reference
4248  *
4249  * Print number of pending I/Os and each I/O details prior to
4250  * reset for debug purpose.
4251  *
4252  * Return: Nothing
4253  */
mpi3mr_print_pending_host_io(struct mpi3mr_ioc * mrioc)4254 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
4255 {
4256 	struct Scsi_Host *shost = mrioc->shost;
4257 
4258 	ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
4259 	    __func__, mpi3mr_get_fw_pending_ios(mrioc));
4260 	blk_mq_tagset_busy_iter(&shost->tag_set,
4261 	    mpi3mr_print_scmd, (void *)mrioc);
4262 }
4263 
4264 /**
4265  * mpi3mr_wait_for_host_io - block for I/Os to complete
4266  * @mrioc: Adapter instance reference
4267  * @timeout: time out in seconds
4268  * Waits for pending I/Os for the given adapter to complete or
4269  * to hit the timeout.
4270  *
4271  * Return: Nothing
4272  */
mpi3mr_wait_for_host_io(struct mpi3mr_ioc * mrioc,u32 timeout)4273 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
4274 {
4275 	enum mpi3mr_iocstate iocstate;
4276 	int i = 0;
4277 
4278 	iocstate = mpi3mr_get_iocstate(mrioc);
4279 	if (iocstate != MRIOC_STATE_READY)
4280 		return;
4281 
4282 	if (!mpi3mr_get_fw_pending_ios(mrioc))
4283 		return;
4284 	ioc_info(mrioc,
4285 	    "%s :Waiting for %d seconds prior to reset for %d I/O\n",
4286 	    __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
4287 
4288 	for (i = 0; i < timeout; i++) {
4289 		if (!mpi3mr_get_fw_pending_ios(mrioc))
4290 			break;
4291 		iocstate = mpi3mr_get_iocstate(mrioc);
4292 		if (iocstate != MRIOC_STATE_READY)
4293 			break;
4294 		msleep(1000);
4295 	}
4296 
4297 	ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
4298 	    mpi3mr_get_fw_pending_ios(mrioc));
4299 }
4300 
4301 /**
4302  * mpi3mr_setup_divert_ws - Setup Divert IO flag for write same
4303  * @mrioc: Adapter instance reference
4304  * @scmd: SCSI command reference
4305  * @scsiio_req: MPI3 SCSI IO request
4306  * @scsiio_flags: Pointer to MPI3 SCSI IO Flags
4307  * @wslen: write same max length
4308  *
4309  * Gets values of unmap, ndob and number of blocks from write
4310  * same scsi io and based on these values it sets divert IO flag
4311  * and reason for diverting IO to firmware.
4312  *
4313  * Return: Nothing
4314  */
mpi3mr_setup_divert_ws(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd,struct mpi3_scsi_io_request * scsiio_req,u32 * scsiio_flags,u16 wslen)4315 static inline void mpi3mr_setup_divert_ws(struct mpi3mr_ioc *mrioc,
4316 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req,
4317 	u32 *scsiio_flags, u16 wslen)
4318 {
4319 	u8 unmap = 0, ndob = 0;
4320 	u8 opcode = scmd->cmnd[0];
4321 	u32 num_blocks = 0;
4322 	u16 sa = (scmd->cmnd[8] << 8) | (scmd->cmnd[9]);
4323 
4324 	if (opcode == WRITE_SAME_16) {
4325 		unmap = scmd->cmnd[1] & 0x08;
4326 		ndob = scmd->cmnd[1] & 0x01;
4327 		num_blocks = get_unaligned_be32(scmd->cmnd + 10);
4328 	} else if ((opcode == VARIABLE_LENGTH_CMD) && (sa == WRITE_SAME_32)) {
4329 		unmap = scmd->cmnd[10] & 0x08;
4330 		ndob = scmd->cmnd[10] & 0x01;
4331 		num_blocks = get_unaligned_be32(scmd->cmnd + 28);
4332 	} else
4333 		return;
4334 
4335 	if ((unmap) && (ndob) && (num_blocks > wslen)) {
4336 		scsiio_req->msg_flags |=
4337 		    MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4338 		*scsiio_flags |=
4339 			MPI3_SCSIIO_FLAGS_DIVERT_REASON_WRITE_SAME_TOO_LARGE;
4340 	}
4341 }
4342 
4343 /**
4344  * mpi3mr_eh_host_reset - Host reset error handling callback
4345  * @scmd: SCSI command reference
4346  *
4347  * Issue controller reset
4348  *
4349  * Return: SUCCESS of successful reset else FAILED
4350  */
mpi3mr_eh_host_reset(struct scsi_cmnd * scmd)4351 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
4352 {
4353 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4354 	int retval = FAILED, ret;
4355 
4356 	ret = mpi3mr_soft_reset_handler(mrioc,
4357 	    MPI3MR_RESET_FROM_EH_HOS, 1);
4358 	if (ret)
4359 		goto out;
4360 
4361 	retval = SUCCESS;
4362 out:
4363 	sdev_printk(KERN_INFO, scmd->device,
4364 	    "Host reset is %s for scmd(%p)\n",
4365 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4366 
4367 	return retval;
4368 }
4369 
4370 /**
4371  * mpi3mr_eh_bus_reset - Bus reset error handling callback
4372  * @scmd: SCSI command reference
4373  *
4374  * Checks whether pending I/Os are present for the RAID volume;
4375  * if not there's no need to reset the adapter.
4376  *
4377  * Return: SUCCESS of successful reset else FAILED
4378  */
mpi3mr_eh_bus_reset(struct scsi_cmnd * scmd)4379 static int mpi3mr_eh_bus_reset(struct scsi_cmnd *scmd)
4380 {
4381 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4382 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4383 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4384 	u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
4385 	int retval = FAILED;
4386 	unsigned int timeout = MPI3MR_RESET_TIMEOUT;
4387 
4388 	sdev_priv_data = scmd->device->hostdata;
4389 	if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
4390 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
4391 		dev_type = stgt_priv_data->dev_type;
4392 	}
4393 
4394 	if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
4395 		mpi3mr_wait_for_host_io(mrioc,
4396 			MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
4397 		if (!mpi3mr_get_fw_pending_ios(mrioc)) {
4398 			while (mrioc->reset_in_progress ||
4399 			       mrioc->prepare_for_reset ||
4400 			       mrioc->block_on_pci_err) {
4401 				ssleep(1);
4402 				if (!timeout--) {
4403 					retval = FAILED;
4404 					goto out;
4405 				}
4406 			}
4407 			retval = SUCCESS;
4408 			goto out;
4409 		}
4410 	}
4411 	if (retval == FAILED)
4412 		mpi3mr_print_pending_host_io(mrioc);
4413 
4414 out:
4415 	sdev_printk(KERN_INFO, scmd->device,
4416 		"Bus reset is %s for scmd(%p)\n",
4417 		((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4418 	return retval;
4419 }
4420 
4421 /**
4422  * mpi3mr_eh_target_reset - Target reset error handling callback
4423  * @scmd: SCSI command reference
4424  *
4425  * Issue Target reset Task Management and verify the scmd is
4426  * terminated successfully and return status accordingly.
4427  *
4428  * Return: SUCCESS of successful termination of the scmd else
4429  *         FAILED
4430  */
mpi3mr_eh_target_reset(struct scsi_cmnd * scmd)4431 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
4432 {
4433 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4434 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4435 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4436 	u16 dev_handle;
4437 	u8 resp_code = 0;
4438 	int retval = FAILED, ret = 0;
4439 
4440 	sdev_printk(KERN_INFO, scmd->device,
4441 	    "Attempting Target Reset! scmd(%p)\n", scmd);
4442 	scsi_print_command(scmd);
4443 
4444 	sdev_priv_data = scmd->device->hostdata;
4445 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4446 		sdev_printk(KERN_INFO, scmd->device,
4447 		    "SCSI device is not available\n");
4448 		retval = SUCCESS;
4449 		goto out;
4450 	}
4451 
4452 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4453 	dev_handle = stgt_priv_data->dev_handle;
4454 	if (stgt_priv_data->dev_removed) {
4455 		struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4456 		sdev_printk(KERN_INFO, scmd->device,
4457 		    "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
4458 		    mrioc->name, dev_handle);
4459 		if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4460 			retval = SUCCESS;
4461 		else
4462 			retval = FAILED;
4463 		goto out;
4464 	}
4465 	sdev_printk(KERN_INFO, scmd->device,
4466 	    "Target Reset is issued to handle(0x%04x)\n",
4467 	    dev_handle);
4468 
4469 	ret = mpi3mr_issue_tm(mrioc,
4470 	    MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
4471 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4472 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4473 
4474 	if (ret)
4475 		goto out;
4476 
4477 	if (stgt_priv_data->pend_count) {
4478 		sdev_printk(KERN_INFO, scmd->device,
4479 		    "%s: target has %d pending commands, target reset is failed\n",
4480 		    mrioc->name, stgt_priv_data->pend_count);
4481 		goto out;
4482 	}
4483 
4484 	retval = SUCCESS;
4485 out:
4486 	sdev_printk(KERN_INFO, scmd->device,
4487 	    "%s: target reset is %s for scmd(%p)\n", mrioc->name,
4488 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4489 
4490 	return retval;
4491 }
4492 
4493 /**
4494  * mpi3mr_eh_dev_reset- Device reset error handling callback
4495  * @scmd: SCSI command reference
4496  *
4497  * Issue lun reset Task Management and verify the scmd is
4498  * terminated successfully and return status accordingly.
4499  *
4500  * Return: SUCCESS of successful termination of the scmd else
4501  *         FAILED
4502  */
mpi3mr_eh_dev_reset(struct scsi_cmnd * scmd)4503 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
4504 {
4505 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4506 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4507 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4508 	u16 dev_handle;
4509 	u8 resp_code = 0;
4510 	int retval = FAILED, ret = 0;
4511 
4512 	sdev_printk(KERN_INFO, scmd->device,
4513 	    "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
4514 	scsi_print_command(scmd);
4515 
4516 	sdev_priv_data = scmd->device->hostdata;
4517 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4518 		sdev_printk(KERN_INFO, scmd->device,
4519 		    "SCSI device is not available\n");
4520 		retval = SUCCESS;
4521 		goto out;
4522 	}
4523 
4524 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4525 	dev_handle = stgt_priv_data->dev_handle;
4526 	if (stgt_priv_data->dev_removed) {
4527 		struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4528 		sdev_printk(KERN_INFO, scmd->device,
4529 		    "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
4530 		    mrioc->name, dev_handle);
4531 		if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4532 			retval = SUCCESS;
4533 		else
4534 			retval = FAILED;
4535 		goto out;
4536 	}
4537 	sdev_printk(KERN_INFO, scmd->device,
4538 	    "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
4539 
4540 	ret = mpi3mr_issue_tm(mrioc,
4541 	    MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
4542 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4543 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4544 
4545 	if (ret)
4546 		goto out;
4547 
4548 	if (sdev_priv_data->pend_count) {
4549 		sdev_printk(KERN_INFO, scmd->device,
4550 		    "%s: device has %d pending commands, device(LUN) reset is failed\n",
4551 		    mrioc->name, sdev_priv_data->pend_count);
4552 		goto out;
4553 	}
4554 	retval = SUCCESS;
4555 out:
4556 	sdev_printk(KERN_INFO, scmd->device,
4557 	    "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
4558 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4559 
4560 	return retval;
4561 }
4562 
4563 /**
4564  * mpi3mr_eh_abort - Callback function for abort error handling
4565  * @scmd: SCSI command reference
4566  *
4567  * Issues Abort Task Management if the command is in LLD scope
4568  * and verifies if it is aborted successfully, and return status
4569  * accordingly.
4570  *
4571  * Return: SUCCESS if the abort was successful, otherwise FAILED
4572  */
mpi3mr_eh_abort(struct scsi_cmnd * scmd)4573 static int mpi3mr_eh_abort(struct scsi_cmnd *scmd)
4574 {
4575 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4576 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4577 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4578 	struct scmd_priv *cmd_priv;
4579 	u16 dev_handle, timeout = MPI3MR_ABORTTM_TIMEOUT;
4580 	u8 resp_code = 0;
4581 	int retval = FAILED, ret = 0;
4582 	struct request *rq = scsi_cmd_to_rq(scmd);
4583 	unsigned long scmd_age_ms = jiffies_to_msecs(jiffies - scmd->jiffies_at_alloc);
4584 	unsigned long scmd_age_sec = scmd_age_ms / HZ;
4585 
4586 	sdev_printk(KERN_INFO, scmd->device,
4587 		    "%s: attempting abort task for scmd(%p)\n", mrioc->name, scmd);
4588 
4589 	sdev_printk(KERN_INFO, scmd->device,
4590 		    "%s: scmd(0x%p) is outstanding for %lus %lums, timeout %us, retries %d, allowed %d\n",
4591 		    mrioc->name, scmd, scmd_age_sec, scmd_age_ms % HZ, rq->timeout / HZ,
4592 		    scmd->retries, scmd->allowed);
4593 
4594 	scsi_print_command(scmd);
4595 
4596 	sdev_priv_data = scmd->device->hostdata;
4597 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4598 		sdev_printk(KERN_INFO, scmd->device,
4599 			    "%s: Device not available, Skip issuing abort task\n",
4600 			    mrioc->name);
4601 		retval = SUCCESS;
4602 		goto out;
4603 	}
4604 
4605 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4606 	dev_handle = stgt_priv_data->dev_handle;
4607 
4608 	cmd_priv = scsi_cmd_priv(scmd);
4609 	if (!cmd_priv->in_lld_scope ||
4610 	    cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID) {
4611 		sdev_printk(KERN_INFO, scmd->device,
4612 			    "%s: scmd (0x%p) not in LLD scope, Skip issuing Abort Task\n",
4613 			    mrioc->name, scmd);
4614 		retval = SUCCESS;
4615 		goto out;
4616 	}
4617 
4618 	if (stgt_priv_data->dev_removed) {
4619 		sdev_printk(KERN_INFO, scmd->device,
4620 			    "%s: Device (handle = 0x%04x) removed, Skip issuing Abort Task\n",
4621 			    mrioc->name, dev_handle);
4622 		retval = FAILED;
4623 		goto out;
4624 	}
4625 
4626 	ret = mpi3mr_issue_tm(mrioc, MPI3_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4627 			      dev_handle, sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4628 			      timeout, &mrioc->host_tm_cmds, &resp_code, scmd);
4629 
4630 	if (ret)
4631 		goto out;
4632 
4633 	if (cmd_priv->in_lld_scope) {
4634 		sdev_printk(KERN_INFO, scmd->device,
4635 			    "%s: Abort task failed. scmd (0x%p) was not terminated\n",
4636 			    mrioc->name, scmd);
4637 		goto out;
4638 	}
4639 
4640 	retval = SUCCESS;
4641 out:
4642 	sdev_printk(KERN_INFO, scmd->device,
4643 		    "%s: Abort Task %s for scmd (0x%p)\n", mrioc->name,
4644 		    ((retval == SUCCESS) ? "SUCCEEDED" : "FAILED"), scmd);
4645 
4646 	return retval;
4647 }
4648 
4649 /**
4650  * mpi3mr_scan_start - Scan start callback handler
4651  * @shost: SCSI host reference
4652  *
4653  * Issue port enable request asynchronously.
4654  *
4655  * Return: Nothing
4656  */
mpi3mr_scan_start(struct Scsi_Host * shost)4657 static void mpi3mr_scan_start(struct Scsi_Host *shost)
4658 {
4659 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4660 
4661 	mrioc->scan_started = 1;
4662 	ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
4663 	if (mpi3mr_issue_port_enable(mrioc, 1)) {
4664 		ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
4665 		mrioc->scan_started = 0;
4666 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4667 	}
4668 }
4669 
4670 /**
4671  * mpi3mr_scan_finished - Scan finished callback handler
4672  * @shost: SCSI host reference
4673  * @time: Jiffies from the scan start
4674  *
4675  * Checks whether the port enable is completed or timedout or
4676  * failed and set the scan status accordingly after taking any
4677  * recovery if required.
4678  *
4679  * Return: 1 on scan finished or timed out, 0 for in progress
4680  */
mpi3mr_scan_finished(struct Scsi_Host * shost,unsigned long time)4681 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
4682 	unsigned long time)
4683 {
4684 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4685 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
4686 	u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4687 
4688 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4689 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4690 		ioc_err(mrioc, "port enable failed due to fault or reset\n");
4691 		mpi3mr_print_fault_info(mrioc);
4692 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4693 		mrioc->scan_started = 0;
4694 		mrioc->init_cmds.is_waiting = 0;
4695 		mrioc->init_cmds.callback = NULL;
4696 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4697 	}
4698 
4699 	if (time >= (pe_timeout * HZ)) {
4700 		ioc_err(mrioc, "port enable failed due to time out\n");
4701 		mpi3mr_check_rh_fault_ioc(mrioc,
4702 		    MPI3MR_RESET_FROM_PE_TIMEOUT);
4703 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4704 		mrioc->scan_started = 0;
4705 		mrioc->init_cmds.is_waiting = 0;
4706 		mrioc->init_cmds.callback = NULL;
4707 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4708 	}
4709 
4710 	if (mrioc->scan_started)
4711 		return 0;
4712 
4713 	if (mrioc->scan_failed) {
4714 		ioc_err(mrioc,
4715 		    "port enable failed with status=0x%04x\n",
4716 		    mrioc->scan_failed);
4717 	} else
4718 		ioc_info(mrioc, "port enable is successfully completed\n");
4719 
4720 	mpi3mr_start_watchdog(mrioc);
4721 	mrioc->is_driver_loading = 0;
4722 	mrioc->stop_bsgs = 0;
4723 	return 1;
4724 }
4725 
4726 /**
4727  * mpi3mr_sdev_destroy - Slave destroy callback handler
4728  * @sdev: SCSI device reference
4729  *
4730  * Cleanup and free per device(lun) private data.
4731  *
4732  * Return: Nothing.
4733  */
mpi3mr_sdev_destroy(struct scsi_device * sdev)4734 static void mpi3mr_sdev_destroy(struct scsi_device *sdev)
4735 {
4736 	struct Scsi_Host *shost;
4737 	struct mpi3mr_ioc *mrioc;
4738 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4739 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4740 	unsigned long flags;
4741 	struct scsi_target *starget;
4742 	struct sas_rphy *rphy = NULL;
4743 
4744 	if (!sdev->hostdata)
4745 		return;
4746 
4747 	starget = scsi_target(sdev);
4748 	shost = dev_to_shost(&starget->dev);
4749 	mrioc = shost_priv(shost);
4750 	scsi_tgt_priv_data = starget->hostdata;
4751 
4752 	scsi_tgt_priv_data->num_luns--;
4753 
4754 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4755 	if (starget->channel == mrioc->scsi_device_channel)
4756 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4757 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4758 		rphy = dev_to_rphy(starget->dev.parent);
4759 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4760 		    rphy->identify.sas_address, rphy);
4761 	}
4762 
4763 	if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
4764 		tgt_dev->starget = NULL;
4765 	if (tgt_dev)
4766 		mpi3mr_tgtdev_put(tgt_dev);
4767 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4768 
4769 	kfree(sdev->hostdata);
4770 	sdev->hostdata = NULL;
4771 }
4772 
4773 /**
4774  * mpi3mr_target_destroy - Target destroy callback handler
4775  * @starget: SCSI target reference
4776  *
4777  * Cleanup and free per target private data.
4778  *
4779  * Return: Nothing.
4780  */
mpi3mr_target_destroy(struct scsi_target * starget)4781 static void mpi3mr_target_destroy(struct scsi_target *starget)
4782 {
4783 	struct Scsi_Host *shost;
4784 	struct mpi3mr_ioc *mrioc;
4785 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4786 	struct mpi3mr_tgt_dev *tgt_dev;
4787 	unsigned long flags;
4788 
4789 	if (!starget->hostdata)
4790 		return;
4791 
4792 	shost = dev_to_shost(&starget->dev);
4793 	mrioc = shost_priv(shost);
4794 	scsi_tgt_priv_data = starget->hostdata;
4795 
4796 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4797 	tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
4798 	if (tgt_dev && (tgt_dev->starget == starget) &&
4799 	    (tgt_dev->perst_id == starget->id))
4800 		tgt_dev->starget = NULL;
4801 	if (tgt_dev) {
4802 		scsi_tgt_priv_data->tgt_dev = NULL;
4803 		scsi_tgt_priv_data->perst_id = 0;
4804 		mpi3mr_tgtdev_put(tgt_dev);
4805 		mpi3mr_tgtdev_put(tgt_dev);
4806 	}
4807 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4808 
4809 	kfree(starget->hostdata);
4810 	starget->hostdata = NULL;
4811 }
4812 
4813 /**
4814  * mpi3mr_sdev_configure - Slave configure callback handler
4815  * @sdev: SCSI device reference
4816  * @lim: queue limits
4817  *
4818  * Configure queue depth, max hardware sectors and virt boundary
4819  * as required
4820  *
4821  * Return: 0 always.
4822  */
mpi3mr_sdev_configure(struct scsi_device * sdev,struct queue_limits * lim)4823 static int mpi3mr_sdev_configure(struct scsi_device *sdev,
4824 				 struct queue_limits *lim)
4825 {
4826 	struct scsi_target *starget;
4827 	struct Scsi_Host *shost;
4828 	struct mpi3mr_ioc *mrioc;
4829 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4830 	unsigned long flags;
4831 	int retval = 0;
4832 	struct sas_rphy *rphy = NULL;
4833 
4834 	starget = scsi_target(sdev);
4835 	shost = dev_to_shost(&starget->dev);
4836 	mrioc = shost_priv(shost);
4837 
4838 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4839 	if (starget->channel == mrioc->scsi_device_channel)
4840 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4841 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4842 		rphy = dev_to_rphy(starget->dev.parent);
4843 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4844 		    rphy->identify.sas_address, rphy);
4845 	}
4846 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4847 	if (!tgt_dev)
4848 		return -ENXIO;
4849 
4850 	mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
4851 
4852 	sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
4853 	blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
4854 
4855 	mpi3mr_configure_tgt_dev(tgt_dev, lim);
4856 	mpi3mr_tgtdev_put(tgt_dev);
4857 	return retval;
4858 }
4859 
4860 /**
4861  * mpi3mr_sdev_init -Slave alloc callback handler
4862  * @sdev: SCSI device reference
4863  *
4864  * Allocate per device(lun) private data and initialize it.
4865  *
4866  * Return: 0 on success -ENOMEM on memory allocation failure.
4867  */
mpi3mr_sdev_init(struct scsi_device * sdev)4868 static int mpi3mr_sdev_init(struct scsi_device *sdev)
4869 {
4870 	struct Scsi_Host *shost;
4871 	struct mpi3mr_ioc *mrioc;
4872 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4873 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4874 	struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
4875 	unsigned long flags;
4876 	struct scsi_target *starget;
4877 	int retval = 0;
4878 	struct sas_rphy *rphy = NULL;
4879 
4880 	starget = scsi_target(sdev);
4881 	shost = dev_to_shost(&starget->dev);
4882 	mrioc = shost_priv(shost);
4883 	scsi_tgt_priv_data = starget->hostdata;
4884 
4885 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4886 
4887 	if (starget->channel == mrioc->scsi_device_channel)
4888 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4889 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4890 		rphy = dev_to_rphy(starget->dev.parent);
4891 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4892 		    rphy->identify.sas_address, rphy);
4893 	}
4894 
4895 	if (tgt_dev) {
4896 		if (tgt_dev->starget == NULL)
4897 			tgt_dev->starget = starget;
4898 		mpi3mr_tgtdev_put(tgt_dev);
4899 		retval = 0;
4900 	} else {
4901 		spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4902 		return -ENXIO;
4903 	}
4904 
4905 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4906 
4907 	scsi_dev_priv_data = kzalloc_obj(*scsi_dev_priv_data);
4908 	if (!scsi_dev_priv_data)
4909 		return -ENOMEM;
4910 
4911 	scsi_dev_priv_data->lun_id = sdev->lun;
4912 	scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4913 	sdev->hostdata = scsi_dev_priv_data;
4914 
4915 	scsi_tgt_priv_data->num_luns++;
4916 
4917 	return retval;
4918 }
4919 
4920 /**
4921  * mpi3mr_target_alloc - Target alloc callback handler
4922  * @starget: SCSI target reference
4923  *
4924  * Allocate per target private data and initialize it.
4925  *
4926  * Return: 0 on success -ENOMEM on memory allocation failure.
4927  */
mpi3mr_target_alloc(struct scsi_target * starget)4928 static int mpi3mr_target_alloc(struct scsi_target *starget)
4929 {
4930 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4931 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4932 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4933 	struct mpi3mr_tgt_dev *tgt_dev;
4934 	unsigned long flags;
4935 	int retval = 0;
4936 	struct sas_rphy *rphy = NULL;
4937 
4938 	scsi_tgt_priv_data = kzalloc_obj(*scsi_tgt_priv_data);
4939 	if (!scsi_tgt_priv_data)
4940 		return -ENOMEM;
4941 
4942 	starget->hostdata = scsi_tgt_priv_data;
4943 
4944 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4945 	if (starget->channel == mrioc->scsi_device_channel) {
4946 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4947 		if (tgt_dev && !tgt_dev->is_hidden && tgt_dev->non_stl) {
4948 			scsi_tgt_priv_data->starget = starget;
4949 			scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4950 			scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4951 			scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4952 			scsi_tgt_priv_data->tgt_dev = tgt_dev;
4953 			tgt_dev->starget = starget;
4954 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
4955 			retval = 0;
4956 			if ((tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
4957 			    ((tgt_dev->dev_spec.pcie_inf.dev_info &
4958 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4959 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
4960 			    ((tgt_dev->dev_spec.pcie_inf.dev_info &
4961 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_MASK) !=
4962 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_0))
4963 				scsi_tgt_priv_data->dev_nvme_dif = 1;
4964 			scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4965 			scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4966 			if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4967 				scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg;
4968 		} else
4969 			retval = -ENXIO;
4970 	} else if (mrioc->sas_transport_enabled && !starget->channel) {
4971 		rphy = dev_to_rphy(starget->dev.parent);
4972 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4973 		    rphy->identify.sas_address, rphy);
4974 		if (tgt_dev && !tgt_dev->is_hidden && !tgt_dev->non_stl &&
4975 		    (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
4976 			scsi_tgt_priv_data->starget = starget;
4977 			scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4978 			scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4979 			scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4980 			scsi_tgt_priv_data->tgt_dev = tgt_dev;
4981 			scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4982 			scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4983 			tgt_dev->starget = starget;
4984 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
4985 			retval = 0;
4986 		} else
4987 			retval = -ENXIO;
4988 	}
4989 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4990 
4991 	return retval;
4992 }
4993 
4994 /**
4995  * mpi3mr_check_return_unmap - Whether an unmap is allowed
4996  * @mrioc: Adapter instance reference
4997  * @scmd: SCSI Command reference
4998  *
4999  * The controller hardware cannot handle certain unmap commands
5000  * for NVMe drives, this routine checks those and return true
5001  * and completes the SCSI command with proper status and sense
5002  * data.
5003  *
5004  * Return: TRUE for not  allowed unmap, FALSE otherwise.
5005  */
mpi3mr_check_return_unmap(struct mpi3mr_ioc * mrioc,struct scsi_cmnd * scmd)5006 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
5007 	struct scsi_cmnd *scmd)
5008 {
5009 	unsigned char *buf;
5010 	u16 param_len, desc_len, trunc_param_len;
5011 
5012 	trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
5013 
5014 	if (mrioc->pdev->revision) {
5015 		if ((param_len > 24) && ((param_len - 8) & 0xF)) {
5016 			trunc_param_len -= (param_len - 8) & 0xF;
5017 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
5018 			dprint_scsi_err(mrioc,
5019 			    "truncating param_len from (%d) to (%d)\n",
5020 			    param_len, trunc_param_len);
5021 			put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
5022 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
5023 		}
5024 		return false;
5025 	}
5026 
5027 	if (!param_len) {
5028 		ioc_warn(mrioc,
5029 		    "%s: cdb received with zero parameter length\n",
5030 		    __func__);
5031 		scsi_print_command(scmd);
5032 		scmd->result = DID_OK << 16;
5033 		scsi_done(scmd);
5034 		return true;
5035 	}
5036 
5037 	if (param_len < 24) {
5038 		ioc_warn(mrioc,
5039 		    "%s: cdb received with invalid param_len: %d\n",
5040 		    __func__, param_len);
5041 		scsi_print_command(scmd);
5042 		scmd->result = SAM_STAT_CHECK_CONDITION;
5043 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
5044 		    0x1A, 0);
5045 		scsi_done(scmd);
5046 		return true;
5047 	}
5048 	if (param_len != scsi_bufflen(scmd)) {
5049 		ioc_warn(mrioc,
5050 		    "%s: cdb received with param_len: %d bufflen: %d\n",
5051 		    __func__, param_len, scsi_bufflen(scmd));
5052 		scsi_print_command(scmd);
5053 		scmd->result = SAM_STAT_CHECK_CONDITION;
5054 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
5055 		    0x1A, 0);
5056 		scsi_done(scmd);
5057 		return true;
5058 	}
5059 	buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
5060 	if (!buf) {
5061 		scsi_print_command(scmd);
5062 		scmd->result = SAM_STAT_CHECK_CONDITION;
5063 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
5064 		    0x55, 0x03);
5065 		scsi_done(scmd);
5066 		return true;
5067 	}
5068 	scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
5069 	desc_len = get_unaligned_be16(&buf[2]);
5070 
5071 	if (desc_len < 16) {
5072 		ioc_warn(mrioc,
5073 		    "%s: Invalid descriptor length in param list: %d\n",
5074 		    __func__, desc_len);
5075 		scsi_print_command(scmd);
5076 		scmd->result = SAM_STAT_CHECK_CONDITION;
5077 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
5078 		    0x26, 0);
5079 		scsi_done(scmd);
5080 		kfree(buf);
5081 		return true;
5082 	}
5083 
5084 	if (param_len > (desc_len + 8)) {
5085 		trunc_param_len = desc_len + 8;
5086 		scsi_print_command(scmd);
5087 		dprint_scsi_err(mrioc,
5088 		    "truncating param_len(%d) to desc_len+8(%d)\n",
5089 		    param_len, trunc_param_len);
5090 		put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
5091 		scsi_print_command(scmd);
5092 	}
5093 
5094 	kfree(buf);
5095 	return false;
5096 }
5097 
5098 /**
5099  * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
5100  * @scmd: SCSI Command reference
5101  *
5102  * Checks whether a cdb is allowed during shutdown or not.
5103  *
5104  * Return: TRUE for allowed commands, FALSE otherwise.
5105  */
5106 
mpi3mr_allow_scmd_to_fw(struct scsi_cmnd * scmd)5107 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
5108 {
5109 	switch (scmd->cmnd[0]) {
5110 	case SYNCHRONIZE_CACHE:
5111 	case START_STOP:
5112 		return true;
5113 	default:
5114 		return false;
5115 	}
5116 }
5117 
5118 /**
5119  * mpi3mr_qcmd - I/O request despatcher
5120  * @shost: SCSI Host reference
5121  * @scmd: SCSI Command reference
5122  *
5123  * Issues the SCSI Command as an MPI3 request.
5124  *
5125  * Return: 0 on successful queueing of the request or if the
5126  *         request is completed with failure.
5127  *         SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
5128  *         SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
5129  */
mpi3mr_qcmd(struct Scsi_Host * shost,struct scsi_cmnd * scmd)5130 static enum scsi_qc_status mpi3mr_qcmd(struct Scsi_Host *shost,
5131 				       struct scsi_cmnd *scmd)
5132 {
5133 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
5134 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
5135 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
5136 	struct scmd_priv *scmd_priv_data = NULL;
5137 	struct mpi3_scsi_io_request *scsiio_req = NULL;
5138 	struct op_req_qinfo *op_req_q = NULL;
5139 	int retval = 0;
5140 	u16 dev_handle;
5141 	u16 host_tag;
5142 	u32 scsiio_flags = 0, data_len_blks = 0;
5143 	struct request *rq = scsi_cmd_to_rq(scmd);
5144 	int iprio_class;
5145 	u8 is_pcie_dev = 0;
5146 	u32 tracked_io_sz = 0;
5147 	u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
5148 	struct mpi3mr_throttle_group_info *tg = NULL;
5149 
5150 	if (mrioc->unrecoverable) {
5151 		scmd->result = DID_ERROR << 16;
5152 		scsi_done(scmd);
5153 		goto out;
5154 	}
5155 
5156 	sdev_priv_data = scmd->device->hostdata;
5157 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
5158 		scmd->result = DID_NO_CONNECT << 16;
5159 		scsi_done(scmd);
5160 		goto out;
5161 	}
5162 
5163 	if (mrioc->stop_drv_processing &&
5164 	    !(mpi3mr_allow_scmd_to_fw(scmd))) {
5165 		scmd->result = DID_NO_CONNECT << 16;
5166 		scsi_done(scmd);
5167 		goto out;
5168 	}
5169 
5170 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
5171 	dev_handle = stgt_priv_data->dev_handle;
5172 
5173 	/* Avoid error handling escalation when device is removed or blocked */
5174 
5175 	if (scsi_get_host_state(scmd->device->host) == SHOST_RECOVERY &&
5176 		scmd->cmnd[0] == TEST_UNIT_READY &&
5177 		(stgt_priv_data->dev_removed || (dev_handle == MPI3MR_INVALID_DEV_HANDLE))) {
5178 		scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
5179 		scsi_done(scmd);
5180 		goto out;
5181 	}
5182 
5183 	if (mrioc->reset_in_progress || mrioc->prepare_for_reset
5184 	    || mrioc->block_on_pci_err) {
5185 		retval = SCSI_MLQUEUE_HOST_BUSY;
5186 		goto out;
5187 	}
5188 
5189 	if (atomic_read(&stgt_priv_data->block_io)) {
5190 		if (mrioc->stop_drv_processing) {
5191 			scmd->result = DID_NO_CONNECT << 16;
5192 			scsi_done(scmd);
5193 			goto out;
5194 		}
5195 		retval = SCSI_MLQUEUE_DEVICE_BUSY;
5196 		goto out;
5197 	}
5198 
5199 	if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
5200 		scmd->result = DID_NO_CONNECT << 16;
5201 		scsi_done(scmd);
5202 		goto out;
5203 	}
5204 	if (stgt_priv_data->dev_removed) {
5205 		scmd->result = DID_NO_CONNECT << 16;
5206 		scsi_done(scmd);
5207 		goto out;
5208 	}
5209 
5210 	if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
5211 		is_pcie_dev = 1;
5212 	if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
5213 	    (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
5214 	    mpi3mr_check_return_unmap(mrioc, scmd))
5215 		goto out;
5216 
5217 	host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
5218 	if (host_tag == MPI3MR_HOSTTAG_INVALID) {
5219 		scmd->result = DID_ERROR << 16;
5220 		scsi_done(scmd);
5221 		goto out;
5222 	}
5223 
5224 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
5225 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
5226 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
5227 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
5228 	else
5229 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
5230 
5231 	scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
5232 
5233 	if (sdev_priv_data->ncq_prio_enable) {
5234 		iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
5235 		if (iprio_class == IOPRIO_CLASS_RT)
5236 			scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
5237 	}
5238 
5239 	if (scmd->cmd_len > 16)
5240 		scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
5241 
5242 	scmd_priv_data = scsi_cmd_priv(scmd);
5243 	memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
5244 	scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
5245 	scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
5246 	scsiio_req->host_tag = cpu_to_le16(host_tag);
5247 
5248 	mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
5249 
5250 	if (stgt_priv_data->wslen)
5251 		mpi3mr_setup_divert_ws(mrioc, scmd, scsiio_req, &scsiio_flags,
5252 		    stgt_priv_data->wslen);
5253 
5254 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
5255 	scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
5256 	scsiio_req->dev_handle = cpu_to_le16(dev_handle);
5257 	scsiio_req->flags = cpu_to_le32(scsiio_flags);
5258 	int_to_scsilun(sdev_priv_data->lun_id,
5259 	    (struct scsi_lun *)scsiio_req->lun);
5260 
5261 	if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
5262 		mpi3mr_clear_scmd_priv(mrioc, scmd);
5263 		retval = SCSI_MLQUEUE_HOST_BUSY;
5264 		goto out;
5265 	}
5266 	op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
5267 	data_len_blks = scsi_bufflen(scmd) >> 9;
5268 	if ((data_len_blks >= mrioc->io_throttle_data_length) &&
5269 	    stgt_priv_data->io_throttle_enabled) {
5270 		tracked_io_sz = data_len_blks;
5271 		tg = stgt_priv_data->throttle_group;
5272 		if (tg) {
5273 			ioc_pend_data_len = atomic_add_return(data_len_blks,
5274 			    &mrioc->pend_large_data_sz);
5275 			tg_pend_data_len = atomic_add_return(data_len_blks,
5276 			    &tg->pend_large_data_sz);
5277 			if (!tg->io_divert  && ((ioc_pend_data_len >=
5278 			    mrioc->io_throttle_high) ||
5279 			    (tg_pend_data_len >= tg->high))) {
5280 				tg->io_divert = 1;
5281 				tg->need_qd_reduction = 1;
5282 				mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
5283 				    tg, 1);
5284 				mpi3mr_queue_qd_reduction_event(mrioc, tg);
5285 			}
5286 		} else {
5287 			ioc_pend_data_len = atomic_add_return(data_len_blks,
5288 			    &mrioc->pend_large_data_sz);
5289 			if (ioc_pend_data_len >= mrioc->io_throttle_high)
5290 				stgt_priv_data->io_divert = 1;
5291 		}
5292 	}
5293 
5294 	if (stgt_priv_data->io_divert) {
5295 		scsiio_req->msg_flags |=
5296 		    MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
5297 		scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
5298 	}
5299 	scsiio_req->flags |= cpu_to_le32(scsiio_flags);
5300 
5301 	if (mpi3mr_op_request_post(mrioc, op_req_q,
5302 	    scmd_priv_data->mpi3mr_scsiio_req)) {
5303 		mpi3mr_clear_scmd_priv(mrioc, scmd);
5304 		retval = SCSI_MLQUEUE_HOST_BUSY;
5305 		if (tracked_io_sz) {
5306 			atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
5307 			if (tg)
5308 				atomic_sub(tracked_io_sz,
5309 				    &tg->pend_large_data_sz);
5310 		}
5311 		goto out;
5312 	}
5313 
5314 out:
5315 	return retval;
5316 }
5317 
5318 static const struct scsi_host_template mpi3mr_driver_template = {
5319 	.module				= THIS_MODULE,
5320 	.name				= "MPI3 Storage Controller",
5321 	.proc_name			= MPI3MR_DRIVER_NAME,
5322 	.queuecommand			= mpi3mr_qcmd,
5323 	.target_alloc			= mpi3mr_target_alloc,
5324 	.sdev_init			= mpi3mr_sdev_init,
5325 	.sdev_configure			= mpi3mr_sdev_configure,
5326 	.target_destroy			= mpi3mr_target_destroy,
5327 	.sdev_destroy			= mpi3mr_sdev_destroy,
5328 	.scan_finished			= mpi3mr_scan_finished,
5329 	.scan_start			= mpi3mr_scan_start,
5330 	.change_queue_depth		= mpi3mr_change_queue_depth,
5331 	.eh_abort_handler		= mpi3mr_eh_abort,
5332 	.eh_device_reset_handler	= mpi3mr_eh_dev_reset,
5333 	.eh_target_reset_handler	= mpi3mr_eh_target_reset,
5334 	.eh_bus_reset_handler		= mpi3mr_eh_bus_reset,
5335 	.eh_host_reset_handler		= mpi3mr_eh_host_reset,
5336 	.bios_param			= mpi3mr_bios_param,
5337 	.map_queues			= mpi3mr_map_queues,
5338 	.mq_poll                        = mpi3mr_blk_mq_poll,
5339 	.no_write_same			= 1,
5340 	.can_queue			= 1,
5341 	.this_id			= -1,
5342 	.sg_tablesize			= MPI3MR_DEFAULT_SGL_ENTRIES,
5343 	/* max xfer supported is 1M (2K in 512 byte sized sectors)
5344 	 */
5345 	.max_sectors			= (MPI3MR_DEFAULT_MAX_IO_SIZE / 512),
5346 	.cmd_per_lun			= MPI3MR_MAX_CMDS_LUN,
5347 	.max_segment_size		= 0xffffffff,
5348 	.track_queue_depth		= 1,
5349 	.cmd_size			= sizeof(struct scmd_priv),
5350 	.shost_groups			= mpi3mr_host_groups,
5351 	.sdev_groups			= mpi3mr_dev_groups,
5352 };
5353 
5354 /**
5355  * mpi3mr_init_drv_cmd - Initialize internal command tracker
5356  * @cmdptr: Internal command tracker
5357  * @host_tag: Host tag used for the specific command
5358  *
5359  * Initialize the internal command tracker structure with
5360  * specified host tag.
5361  *
5362  * Return: Nothing.
5363  */
mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd * cmdptr,u16 host_tag)5364 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
5365 	u16 host_tag)
5366 {
5367 	mutex_init(&cmdptr->mutex);
5368 	cmdptr->reply = NULL;
5369 	cmdptr->state = MPI3MR_CMD_NOTUSED;
5370 	cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
5371 	cmdptr->host_tag = host_tag;
5372 }
5373 
5374 /**
5375  * osintfc_mrioc_security_status -Check controller secure status
5376  * @pdev: PCI device instance
5377  *
5378  * Read the Device Serial Number capability from PCI config
5379  * space and decide whether the controller is secure or not.
5380  *
5381  * Return: 0 on success, non-zero on failure.
5382  */
5383 static int
osintfc_mrioc_security_status(struct pci_dev * pdev)5384 osintfc_mrioc_security_status(struct pci_dev *pdev)
5385 {
5386 	u32 cap_data;
5387 	int base;
5388 	u32 ctlr_status;
5389 	u32 debug_status;
5390 	int retval = 0;
5391 
5392 	base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
5393 	if (!base) {
5394 		dev_err(&pdev->dev,
5395 		    "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
5396 		return -1;
5397 	}
5398 
5399 	pci_read_config_dword(pdev, base + 4, &cap_data);
5400 
5401 	debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
5402 	ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
5403 
5404 	switch (ctlr_status) {
5405 	case MPI3MR_INVALID_DEVICE:
5406 		dev_err(&pdev->dev,
5407 		    "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5408 		    __func__, pdev->device, pdev->subsystem_vendor,
5409 		    pdev->subsystem_device);
5410 		retval = -1;
5411 		break;
5412 	case MPI3MR_CONFIG_SECURE_DEVICE:
5413 		if (!debug_status)
5414 			dev_info(&pdev->dev,
5415 			    "%s: Config secure ctlr is detected\n",
5416 			    __func__);
5417 		break;
5418 	case MPI3MR_HARD_SECURE_DEVICE:
5419 		break;
5420 	case MPI3MR_TAMPERED_DEVICE:
5421 		dev_err(&pdev->dev,
5422 		    "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5423 		    __func__, pdev->device, pdev->subsystem_vendor,
5424 		    pdev->subsystem_device);
5425 		retval = -1;
5426 		break;
5427 	default:
5428 		retval = -1;
5429 			break;
5430 	}
5431 
5432 	if (!retval && debug_status) {
5433 		dev_err(&pdev->dev,
5434 		    "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5435 		    __func__, pdev->device, pdev->subsystem_vendor,
5436 		    pdev->subsystem_device);
5437 		retval = -1;
5438 	}
5439 
5440 	return retval;
5441 }
5442 
5443 /**
5444  * mpi3mr_probe - PCI probe callback
5445  * @pdev: PCI device instance
5446  * @id: PCI device ID details
5447  *
5448  * controller initialization routine. Checks the security status
5449  * of the controller and if it is invalid or tampered return the
5450  * probe without initializing the controller. Otherwise,
5451  * allocate per adapter instance through shost_priv and
5452  * initialize controller specific data structures, initializae
5453  * the controller hardware, add shost to the SCSI subsystem.
5454  *
5455  * Return: 0 on success, non-zero on failure.
5456  */
5457 
5458 static int
mpi3mr_probe(struct pci_dev * pdev,const struct pci_device_id * id)5459 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5460 {
5461 	struct mpi3mr_ioc *mrioc = NULL;
5462 	struct Scsi_Host *shost = NULL;
5463 	int retval = 0, i;
5464 
5465 	if (osintfc_mrioc_security_status(pdev)) {
5466 		warn_non_secure_ctlr = 1;
5467 		return 1; /* For Invalid and Tampered device */
5468 	}
5469 
5470 	shost = scsi_host_alloc(&mpi3mr_driver_template,
5471 	    sizeof(struct mpi3mr_ioc));
5472 	if (!shost) {
5473 		retval = -ENODEV;
5474 		goto shost_failed;
5475 	}
5476 
5477 	mrioc = shost_priv(shost);
5478 	retval = ida_alloc_range(&mrioc_ida, 0, U8_MAX, GFP_KERNEL);
5479 	if (retval < 0)
5480 		goto id_alloc_failed;
5481 	mrioc->id = (u8)retval;
5482 	strscpy(mrioc->driver_name, MPI3MR_DRIVER_NAME,
5483 	    sizeof(mrioc->driver_name));
5484 	scnprintf(mrioc->name, sizeof(mrioc->name),
5485 	    "%s%u", mrioc->driver_name, mrioc->id);
5486 	INIT_LIST_HEAD(&mrioc->list);
5487 	spin_lock(&mrioc_list_lock);
5488 	list_add_tail(&mrioc->list, &mrioc_list);
5489 	spin_unlock(&mrioc_list_lock);
5490 
5491 	spin_lock_init(&mrioc->admin_req_lock);
5492 	spin_lock_init(&mrioc->reply_free_queue_lock);
5493 	spin_lock_init(&mrioc->sbq_lock);
5494 	spin_lock_init(&mrioc->fwevt_lock);
5495 	spin_lock_init(&mrioc->tgtdev_lock);
5496 	spin_lock_init(&mrioc->watchdog_lock);
5497 	spin_lock_init(&mrioc->chain_buf_lock);
5498 	spin_lock_init(&mrioc->adm_req_q_bar_writeq_lock);
5499 	spin_lock_init(&mrioc->adm_reply_q_bar_writeq_lock);
5500 	spin_lock_init(&mrioc->sas_node_lock);
5501 	spin_lock_init(&mrioc->trigger_lock);
5502 
5503 	INIT_LIST_HEAD(&mrioc->fwevt_list);
5504 	INIT_LIST_HEAD(&mrioc->tgtdev_list);
5505 	INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
5506 	INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
5507 	INIT_LIST_HEAD(&mrioc->sas_expander_list);
5508 	INIT_LIST_HEAD(&mrioc->hba_port_table_list);
5509 	INIT_LIST_HEAD(&mrioc->enclosure_list);
5510 
5511 	mutex_init(&mrioc->reset_mutex);
5512 	mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
5513 	mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
5514 	mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
5515 	mpi3mr_init_drv_cmd(&mrioc->cfg_cmds, MPI3MR_HOSTTAG_CFG_CMDS);
5516 	mpi3mr_init_drv_cmd(&mrioc->transport_cmds,
5517 	    MPI3MR_HOSTTAG_TRANSPORT_CMDS);
5518 
5519 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5520 		mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
5521 		    MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
5522 
5523 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5524 		mpi3mr_init_drv_cmd(&mrioc->evtack_cmds[i],
5525 				    MPI3MR_HOSTTAG_EVTACKCMD_MIN + i);
5526 
5527 	if ((pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
5528 		!pdev->revision)
5529 		mrioc->enable_segqueue = false;
5530 	else
5531 		mrioc->enable_segqueue = true;
5532 
5533 	init_waitqueue_head(&mrioc->reset_waitq);
5534 	mrioc->logging_level = logging_level;
5535 	mrioc->shost = shost;
5536 	mrioc->pdev = pdev;
5537 	mrioc->stop_bsgs = 1;
5538 
5539 	mrioc->max_sgl_entries = max_sgl_entries;
5540 	if (max_sgl_entries > MPI3MR_MAX_SGL_ENTRIES)
5541 		mrioc->max_sgl_entries = MPI3MR_MAX_SGL_ENTRIES;
5542 	else if (max_sgl_entries < MPI3MR_DEFAULT_SGL_ENTRIES)
5543 		mrioc->max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
5544 	else {
5545 		mrioc->max_sgl_entries /= MPI3MR_DEFAULT_SGL_ENTRIES;
5546 		mrioc->max_sgl_entries *= MPI3MR_DEFAULT_SGL_ENTRIES;
5547 	}
5548 
5549 	/* init shost parameters */
5550 	shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
5551 	shost->max_lun = -1;
5552 	shost->unique_id = mrioc->id;
5553 
5554 	shost->max_channel = 0;
5555 	shost->max_id = 0xFFFFFFFF;
5556 
5557 	shost->host_tagset = 1;
5558 
5559 	if (prot_mask >= 0)
5560 		scsi_host_set_prot(shost, prot_mask);
5561 	else {
5562 		prot_mask = SHOST_DIF_TYPE1_PROTECTION
5563 		    | SHOST_DIF_TYPE2_PROTECTION
5564 		    | SHOST_DIF_TYPE3_PROTECTION;
5565 		scsi_host_set_prot(shost, prot_mask);
5566 	}
5567 
5568 	ioc_info(mrioc,
5569 	    "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
5570 	    __func__,
5571 	    (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5572 	    (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5573 	    (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5574 	    (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5575 	    (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5576 	    (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5577 	    (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5578 
5579 	if (prot_guard_mask)
5580 		scsi_host_set_guard(shost, (prot_guard_mask & 3));
5581 	else
5582 		scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
5583 
5584 	mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
5585 		"%s%d_fwevt_wrkr", 0, mrioc->driver_name, mrioc->id);
5586 	if (!mrioc->fwevt_worker_thread) {
5587 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5588 		    __FILE__, __LINE__, __func__);
5589 		retval = -ENODEV;
5590 		goto fwevtthread_failed;
5591 	}
5592 
5593 	mrioc->is_driver_loading = 1;
5594 	mrioc->cpu_count = num_online_cpus();
5595 	if (mpi3mr_setup_resources(mrioc)) {
5596 		ioc_err(mrioc, "setup resources failed\n");
5597 		retval = -ENODEV;
5598 		goto resource_alloc_failed;
5599 	}
5600 	if (mpi3mr_init_ioc(mrioc)) {
5601 		ioc_err(mrioc, "initializing IOC failed\n");
5602 		retval = -ENODEV;
5603 		goto init_ioc_failed;
5604 	}
5605 
5606 	shost->nr_hw_queues = mrioc->num_op_reply_q;
5607 	if (mrioc->active_poll_qcount)
5608 		shost->nr_maps = 3;
5609 
5610 	shost->can_queue = mrioc->max_host_ios;
5611 	shost->sg_tablesize = mrioc->max_sgl_entries;
5612 	shost->max_id = mrioc->facts.max_perids + 1;
5613 
5614 	retval = scsi_add_host(shost, &pdev->dev);
5615 	if (retval) {
5616 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5617 		    __FILE__, __LINE__, __func__);
5618 		goto addhost_failed;
5619 	}
5620 
5621 	scsi_scan_host(shost);
5622 	mpi3mr_bsg_init(mrioc);
5623 	return retval;
5624 
5625 addhost_failed:
5626 	mpi3mr_stop_watchdog(mrioc);
5627 	mpi3mr_cleanup_ioc(mrioc);
5628 init_ioc_failed:
5629 	mpi3mr_free_mem(mrioc);
5630 	mpi3mr_cleanup_resources(mrioc);
5631 resource_alloc_failed:
5632 	destroy_workqueue(mrioc->fwevt_worker_thread);
5633 fwevtthread_failed:
5634 	ida_free(&mrioc_ida, mrioc->id);
5635 	spin_lock(&mrioc_list_lock);
5636 	list_del(&mrioc->list);
5637 	spin_unlock(&mrioc_list_lock);
5638 id_alloc_failed:
5639 	scsi_host_put(shost);
5640 shost_failed:
5641 	return retval;
5642 }
5643 
5644 /**
5645  * mpi3mr_remove - PCI remove callback
5646  * @pdev: PCI device instance
5647  *
5648  * Cleanup the IOC by issuing MUR and shutdown notification.
5649  * Free up all memory and resources associated with the
5650  * controllerand target devices, unregister the shost.
5651  *
5652  * Return: Nothing.
5653  */
mpi3mr_remove(struct pci_dev * pdev)5654 static void mpi3mr_remove(struct pci_dev *pdev)
5655 {
5656 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5657 	struct mpi3mr_ioc *mrioc;
5658 	struct workqueue_struct	*wq;
5659 	unsigned long flags;
5660 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
5661 	struct mpi3mr_hba_port *port, *hba_port_next;
5662 	struct mpi3mr_sas_node *sas_expander, *sas_expander_next;
5663 
5664 	if (!shost)
5665 		return;
5666 
5667 	mrioc = shost_priv(shost);
5668 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5669 		ssleep(1);
5670 
5671 	if (mrioc->block_on_pci_err) {
5672 		mrioc->block_on_pci_err = false;
5673 		scsi_unblock_requests(shost);
5674 		mrioc->unrecoverable = 1;
5675 	}
5676 
5677 	if (!pci_device_is_present(mrioc->pdev) ||
5678 	    mrioc->pci_err_recovery) {
5679 		mrioc->unrecoverable = 1;
5680 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5681 	}
5682 
5683 	mpi3mr_bsg_exit(mrioc);
5684 	mrioc->stop_drv_processing = 1;
5685 	mpi3mr_cleanup_fwevt_list(mrioc);
5686 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5687 	wq = mrioc->fwevt_worker_thread;
5688 	mrioc->fwevt_worker_thread = NULL;
5689 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5690 	if (wq)
5691 		destroy_workqueue(wq);
5692 
5693 	if (mrioc->sas_transport_enabled)
5694 		sas_remove_host(shost);
5695 	else
5696 		scsi_remove_host(shost);
5697 
5698 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
5699 	    list) {
5700 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
5701 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
5702 		mpi3mr_tgtdev_put(tgtdev);
5703 	}
5704 	mpi3mr_stop_watchdog(mrioc);
5705 	mpi3mr_cleanup_ioc(mrioc);
5706 	mpi3mr_free_mem(mrioc);
5707 	mpi3mr_cleanup_resources(mrioc);
5708 
5709 	spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5710 	list_for_each_entry_safe_reverse(sas_expander, sas_expander_next,
5711 	    &mrioc->sas_expander_list, list) {
5712 		spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5713 		mpi3mr_expander_node_remove(mrioc, sas_expander);
5714 		spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5715 	}
5716 	list_for_each_entry_safe(port, hba_port_next, &mrioc->hba_port_table_list, list) {
5717 		ioc_info(mrioc,
5718 		    "removing hba_port entry: %p port: %d from hba_port list\n",
5719 		    port, port->port_id);
5720 		list_del(&port->list);
5721 		kfree(port);
5722 	}
5723 	spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5724 
5725 	if (mrioc->sas_hba.num_phys) {
5726 		kfree(mrioc->sas_hba.phy);
5727 		mrioc->sas_hba.phy = NULL;
5728 		mrioc->sas_hba.num_phys = 0;
5729 	}
5730 
5731 	ida_free(&mrioc_ida, mrioc->id);
5732 	spin_lock(&mrioc_list_lock);
5733 	list_del(&mrioc->list);
5734 	spin_unlock(&mrioc_list_lock);
5735 
5736 	scsi_host_put(shost);
5737 }
5738 
5739 /**
5740  * mpi3mr_shutdown - PCI shutdown callback
5741  * @pdev: PCI device instance
5742  *
5743  * Free up all memory and resources associated with the
5744  * controller
5745  *
5746  * Return: Nothing.
5747  */
mpi3mr_shutdown(struct pci_dev * pdev)5748 static void mpi3mr_shutdown(struct pci_dev *pdev)
5749 {
5750 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5751 	struct mpi3mr_ioc *mrioc;
5752 	struct workqueue_struct	*wq;
5753 	unsigned long flags;
5754 
5755 	if (!shost)
5756 		return;
5757 
5758 	mrioc = shost_priv(shost);
5759 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5760 		ssleep(1);
5761 
5762 	mrioc->stop_drv_processing = 1;
5763 	mpi3mr_cleanup_fwevt_list(mrioc);
5764 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5765 	wq = mrioc->fwevt_worker_thread;
5766 	mrioc->fwevt_worker_thread = NULL;
5767 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5768 	if (wq)
5769 		destroy_workqueue(wq);
5770 
5771 	mpi3mr_stop_watchdog(mrioc);
5772 	mpi3mr_cleanup_ioc(mrioc);
5773 	mpi3mr_cleanup_resources(mrioc);
5774 }
5775 
5776 /**
5777  * mpi3mr_suspend - PCI power management suspend callback
5778  * @dev: Device struct
5779  *
5780  * Change the power state to the given value and cleanup the IOC
5781  * by issuing MUR and shutdown notification
5782  *
5783  * Return: 0 always.
5784  */
5785 static int __maybe_unused
mpi3mr_suspend(struct device * dev)5786 mpi3mr_suspend(struct device *dev)
5787 {
5788 	struct pci_dev *pdev = to_pci_dev(dev);
5789 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5790 	struct mpi3mr_ioc *mrioc;
5791 
5792 	if (!shost)
5793 		return 0;
5794 
5795 	mrioc = shost_priv(shost);
5796 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5797 		ssleep(1);
5798 	mrioc->stop_drv_processing = 1;
5799 	mpi3mr_cleanup_fwevt_list(mrioc);
5800 	scsi_block_requests(shost);
5801 	mpi3mr_stop_watchdog(mrioc);
5802 	mpi3mr_cleanup_ioc(mrioc);
5803 
5804 	ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state\n",
5805 	    pdev, pci_name(pdev));
5806 	mpi3mr_cleanup_resources(mrioc);
5807 
5808 	return 0;
5809 }
5810 
5811 /**
5812  * mpi3mr_resume - PCI power management resume callback
5813  * @dev: Device struct
5814  *
5815  * Restore the power state to D0 and reinitialize the controller
5816  * and resume I/O operations to the target devices
5817  *
5818  * Return: 0 on success, non-zero on failure
5819  */
5820 static int __maybe_unused
mpi3mr_resume(struct device * dev)5821 mpi3mr_resume(struct device *dev)
5822 {
5823 	struct pci_dev *pdev = to_pci_dev(dev);
5824 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5825 	struct mpi3mr_ioc *mrioc;
5826 	pci_power_t device_state = pdev->current_state;
5827 	int r;
5828 
5829 	if (!shost)
5830 		return 0;
5831 
5832 	mrioc = shost_priv(shost);
5833 
5834 	ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
5835 	    pdev, pci_name(pdev), device_state);
5836 	mrioc->pdev = pdev;
5837 	mrioc->cpu_count = num_online_cpus();
5838 	r = mpi3mr_setup_resources(mrioc);
5839 	if (r) {
5840 		ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
5841 		    __func__, r);
5842 		return r;
5843 	}
5844 
5845 	mrioc->stop_drv_processing = 0;
5846 	mpi3mr_invalidate_devhandles(mrioc);
5847 	mpi3mr_free_enclosure_list(mrioc);
5848 	mpi3mr_memset_buffers(mrioc);
5849 	r = mpi3mr_reinit_ioc(mrioc, 1);
5850 	if (r) {
5851 		ioc_err(mrioc, "resuming controller failed[%d]\n", r);
5852 		return r;
5853 	}
5854 	ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
5855 	scsi_unblock_requests(shost);
5856 	mrioc->device_refresh_on = 0;
5857 	mpi3mr_start_watchdog(mrioc);
5858 
5859 	return 0;
5860 }
5861 
5862 /**
5863  * mpi3mr_pcierr_error_detected - PCI error detected callback
5864  * @pdev: PCI device instance
5865  * @state: channel state
5866  *
5867  * This function is called by the PCI error recovery driver and
5868  * based on the state passed the driver decides what actions to
5869  * be recommended back to PCI driver.
5870  *
5871  * For all of the states if there is no valid mrioc or scsi host
5872  * references in the PCI device then this function will return
5873  * the result as disconnect.
5874  *
5875  * For normal state, this function will return the result as can
5876  * recover.
5877  *
5878  * For frozen state, this function will block for any pending
5879  * controller initialization or re-initialization to complete,
5880  * stop any new interactions with the controller and return
5881  * status as reset required.
5882  *
5883  * For permanent failure state, this function will mark the
5884  * controller as unrecoverable and return status as disconnect.
5885  *
5886  * Returns: PCI_ERS_RESULT_NEED_RESET or CAN_RECOVER or
5887  * DISCONNECT based on the controller state.
5888  */
5889 static pci_ers_result_t
mpi3mr_pcierr_error_detected(struct pci_dev * pdev,pci_channel_state_t state)5890 mpi3mr_pcierr_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5891 {
5892 	struct Scsi_Host *shost;
5893 	struct mpi3mr_ioc *mrioc;
5894 	unsigned int timeout = MPI3MR_RESET_TIMEOUT;
5895 
5896 	dev_info(&pdev->dev, "%s: callback invoked state(%d)\n", __func__,
5897 	    state);
5898 
5899 	shost = pci_get_drvdata(pdev);
5900 	mrioc = shost_priv(shost);
5901 
5902 	switch (state) {
5903 	case pci_channel_io_normal:
5904 		return PCI_ERS_RESULT_CAN_RECOVER;
5905 	case pci_channel_io_frozen:
5906 		mrioc->pci_err_recovery = true;
5907 		mrioc->block_on_pci_err = true;
5908 		do {
5909 			if (mrioc->reset_in_progress || mrioc->is_driver_loading)
5910 				ssleep(1);
5911 			else
5912 				break;
5913 		} while (--timeout);
5914 
5915 		if (!timeout) {
5916 			mrioc->pci_err_recovery = true;
5917 			mrioc->block_on_pci_err = true;
5918 			mrioc->unrecoverable = 1;
5919 			mpi3mr_stop_watchdog(mrioc);
5920 			mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5921 			return PCI_ERS_RESULT_DISCONNECT;
5922 		}
5923 
5924 		scsi_block_requests(mrioc->shost);
5925 		mpi3mr_stop_watchdog(mrioc);
5926 		mpi3mr_cleanup_resources(mrioc);
5927 		return PCI_ERS_RESULT_NEED_RESET;
5928 	case pci_channel_io_perm_failure:
5929 		mrioc->pci_err_recovery = true;
5930 		mrioc->block_on_pci_err = true;
5931 		mrioc->unrecoverable = 1;
5932 		mpi3mr_stop_watchdog(mrioc);
5933 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5934 		return PCI_ERS_RESULT_DISCONNECT;
5935 	default:
5936 		return PCI_ERS_RESULT_DISCONNECT;
5937 	}
5938 }
5939 
5940 /**
5941  * mpi3mr_pcierr_slot_reset - Post slot reset callback
5942  * @pdev: PCI device instance
5943  *
5944  * This function is called by the PCI error recovery driver
5945  * after a slot or link reset issued by it for the recovery, the
5946  * driver is expected to bring back the controller and
5947  * initialize it.
5948  *
5949  * This function restores PCI state and reinitializes controller
5950  * resources and the controller, this blocks for any pending
5951  * reset to complete.
5952  *
5953  * Returns: PCI_ERS_RESULT_DISCONNECT on failure or
5954  * PCI_ERS_RESULT_RECOVERED
5955  */
mpi3mr_pcierr_slot_reset(struct pci_dev * pdev)5956 static pci_ers_result_t mpi3mr_pcierr_slot_reset(struct pci_dev *pdev)
5957 {
5958 	struct Scsi_Host *shost;
5959 	struct mpi3mr_ioc *mrioc;
5960 	unsigned int timeout = MPI3MR_RESET_TIMEOUT;
5961 
5962 	dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5963 
5964 	shost = pci_get_drvdata(pdev);
5965 	mrioc = shost_priv(shost);
5966 
5967 	do {
5968 		if (mrioc->reset_in_progress)
5969 			ssleep(1);
5970 		else
5971 			break;
5972 	} while (--timeout);
5973 
5974 	if (!timeout)
5975 		goto out_failed;
5976 
5977 	pci_restore_state(pdev);
5978 
5979 	if (mpi3mr_setup_resources(mrioc)) {
5980 		ioc_err(mrioc, "setup resources failed\n");
5981 		goto out_failed;
5982 	}
5983 	mrioc->unrecoverable = 0;
5984 	mrioc->pci_err_recovery = false;
5985 
5986 	if (mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0))
5987 		goto out_failed;
5988 
5989 	return PCI_ERS_RESULT_RECOVERED;
5990 
5991 out_failed:
5992 	mrioc->unrecoverable = 1;
5993 	mrioc->block_on_pci_err = false;
5994 	scsi_unblock_requests(shost);
5995 	mpi3mr_start_watchdog(mrioc);
5996 	return PCI_ERS_RESULT_DISCONNECT;
5997 }
5998 
5999 /**
6000  * mpi3mr_pcierr_resume - PCI error recovery resume
6001  * callback
6002  * @pdev: PCI device instance
6003  *
6004  * This function enables all I/O and IOCTLs post reset issued as
6005  * part of the PCI error recovery
6006  *
6007  * Return: Nothing.
6008  */
mpi3mr_pcierr_resume(struct pci_dev * pdev)6009 static void mpi3mr_pcierr_resume(struct pci_dev *pdev)
6010 {
6011 	struct Scsi_Host *shost;
6012 	struct mpi3mr_ioc *mrioc;
6013 
6014 	dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
6015 
6016 	shost = pci_get_drvdata(pdev);
6017 	mrioc = shost_priv(shost);
6018 
6019 	if (mrioc->block_on_pci_err) {
6020 		mrioc->block_on_pci_err = false;
6021 		scsi_unblock_requests(shost);
6022 		mpi3mr_start_watchdog(mrioc);
6023 	}
6024 }
6025 
6026 /**
6027  * mpi3mr_pcierr_mmio_enabled - PCI error recovery callback
6028  * @pdev: PCI device instance
6029  *
6030  * This is called only if mpi3mr_pcierr_error_detected returns
6031  * PCI_ERS_RESULT_CAN_RECOVER.
6032  *
6033  * Return: PCI_ERS_RESULT_DISCONNECT when the controller is
6034  * unrecoverable or when the shost/mrioc reference cannot be
6035  * found, else return PCI_ERS_RESULT_RECOVERED
6036  */
mpi3mr_pcierr_mmio_enabled(struct pci_dev * pdev)6037 static pci_ers_result_t mpi3mr_pcierr_mmio_enabled(struct pci_dev *pdev)
6038 {
6039 	struct Scsi_Host *shost;
6040 	struct mpi3mr_ioc *mrioc;
6041 
6042 	dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
6043 
6044 	shost = pci_get_drvdata(pdev);
6045 	mrioc = shost_priv(shost);
6046 
6047 	if (mrioc->unrecoverable)
6048 		return PCI_ERS_RESULT_DISCONNECT;
6049 
6050 	return PCI_ERS_RESULT_RECOVERED;
6051 }
6052 
6053 static const struct pci_device_id mpi3mr_pci_id_table[] = {
6054 	{
6055 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
6056 		    MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
6057 	},
6058 	{
6059 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
6060 		    MPI3_MFGPAGE_DEVID_SAS5116_MPI, PCI_ANY_ID, PCI_ANY_ID)
6061 	},
6062 	{
6063 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
6064 		    MPI3_MFGPAGE_DEVID_SAS5116_MPI_MGMT, PCI_ANY_ID, PCI_ANY_ID)
6065 	},
6066 	{ 0 }
6067 };
6068 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
6069 
6070 static const struct pci_error_handlers mpi3mr_err_handler = {
6071 	.error_detected = mpi3mr_pcierr_error_detected,
6072 	.mmio_enabled = mpi3mr_pcierr_mmio_enabled,
6073 	.slot_reset = mpi3mr_pcierr_slot_reset,
6074 	.resume = mpi3mr_pcierr_resume,
6075 };
6076 
6077 static SIMPLE_DEV_PM_OPS(mpi3mr_pm_ops, mpi3mr_suspend, mpi3mr_resume);
6078 
6079 static struct pci_driver mpi3mr_pci_driver = {
6080 	.name = MPI3MR_DRIVER_NAME,
6081 	.id_table = mpi3mr_pci_id_table,
6082 	.probe = mpi3mr_probe,
6083 	.remove = mpi3mr_remove,
6084 	.shutdown = mpi3mr_shutdown,
6085 	.err_handler = &mpi3mr_err_handler,
6086 	.driver = {
6087 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
6088 		.pm = &mpi3mr_pm_ops,
6089 	},
6090 };
6091 
event_counter_show(struct device_driver * dd,char * buf)6092 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
6093 {
6094 	return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
6095 }
6096 static DRIVER_ATTR_RO(event_counter);
6097 
mpi3mr_init(void)6098 static int __init mpi3mr_init(void)
6099 {
6100 	int ret_val;
6101 
6102 	pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
6103 	    MPI3MR_DRIVER_VERSION);
6104 
6105 	mpi3mr_transport_template =
6106 	    sas_attach_transport(&mpi3mr_transport_functions);
6107 	if (!mpi3mr_transport_template) {
6108 		pr_err("%s failed to load due to sas transport attach failure\n",
6109 		    MPI3MR_DRIVER_NAME);
6110 		return -ENODEV;
6111 	}
6112 
6113 	ret_val = pci_register_driver(&mpi3mr_pci_driver);
6114 	if (ret_val) {
6115 		pr_err("%s failed to load due to pci register driver failure\n",
6116 		    MPI3MR_DRIVER_NAME);
6117 		goto err_pci_reg_fail;
6118 	}
6119 
6120 	ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
6121 				     &driver_attr_event_counter);
6122 	if (ret_val)
6123 		goto err_event_counter;
6124 
6125 	return ret_val;
6126 
6127 err_event_counter:
6128 	pci_unregister_driver(&mpi3mr_pci_driver);
6129 
6130 err_pci_reg_fail:
6131 	sas_release_transport(mpi3mr_transport_template);
6132 	return ret_val;
6133 }
6134 
mpi3mr_exit(void)6135 static void __exit mpi3mr_exit(void)
6136 {
6137 	if (warn_non_secure_ctlr)
6138 		pr_warn(
6139 		    "Unloading %s version %s while managing a non secure controller\n",
6140 		    MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
6141 	else
6142 		pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
6143 		    MPI3MR_DRIVER_VERSION);
6144 
6145 	driver_remove_file(&mpi3mr_pci_driver.driver,
6146 			   &driver_attr_event_counter);
6147 	pci_unregister_driver(&mpi3mr_pci_driver);
6148 	sas_release_transport(mpi3mr_transport_template);
6149 	ida_destroy(&mrioc_ida);
6150 }
6151 
6152 module_init(mpi3mr_init);
6153 module_exit(mpi3mr_exit);
6154