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