xref: /openbmc/linux/drivers/message/fusion/mptctl.c (revision 87c2ce3b)
1 /*
2  *  linux/drivers/message/fusion/mptctl.c
3  *      mpt Ioctl driver.
4  *      For use with LSI Logic PCI chip/adapters
5  *      running LSI Logic Fusion MPT (Message Passing Technology) firmware.
6  *
7  *  Copyright (c) 1999-2005 LSI Logic Corporation
8  *  (mailto:mpt_linux_developer@lsil.com)
9  *
10  */
11 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12 /*
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; version 2 of the License.
16 
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU General Public License for more details.
21 
22     NO WARRANTY
23     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
24     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
25     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
26     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
27     solely responsible for determining the appropriateness of using and
28     distributing the Program and assumes all risks associated with its
29     exercise of rights under this Agreement, including but not limited to
30     the risks and costs of program errors, damage to or loss of data,
31     programs or equipment, and unavailability or interruption of operations.
32 
33     DISCLAIMER OF LIABILITY
34     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
35     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
37     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
40     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
41 
42     You should have received a copy of the GNU General Public License
43     along with this program; if not, write to the Free Software
44     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
45 */
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
47 
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/errno.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/types.h>
54 #include <linux/pci.h>
55 #include <linux/delay.h>	/* for mdelay */
56 #include <linux/miscdevice.h>
57 #include <linux/smp_lock.h>
58 #include <linux/compat.h>
59 
60 #include <asm/io.h>
61 #include <asm/uaccess.h>
62 
63 #include <scsi/scsi.h>
64 #include <scsi/scsi_cmnd.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_host.h>
67 #include <scsi/scsi_tcq.h>
68 
69 #define COPYRIGHT	"Copyright (c) 1999-2005 LSI Logic Corporation"
70 #define MODULEAUTHOR	"LSI Logic Corporation"
71 #include "mptbase.h"
72 #include "mptctl.h"
73 
74 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
75 #define my_NAME		"Fusion MPT misc device (ioctl) driver"
76 #define my_VERSION	MPT_LINUX_VERSION_COMMON
77 #define MYNAM		"mptctl"
78 
79 MODULE_AUTHOR(MODULEAUTHOR);
80 MODULE_DESCRIPTION(my_NAME);
81 MODULE_LICENSE("GPL");
82 
83 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
84 
85 static int mptctl_id = -1;
86 
87 static DECLARE_WAIT_QUEUE_HEAD ( mptctl_wait );
88 
89 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
90 
91 struct buflist {
92 	u8	*kptr;
93 	int	 len;
94 };
95 
96 /*
97  * Function prototypes. Called from OS entry point mptctl_ioctl.
98  * arg contents specific to function.
99  */
100 static int mptctl_fw_download(unsigned long arg);
101 static int mptctl_getiocinfo(unsigned long arg, unsigned int cmd);
102 static int mptctl_gettargetinfo(unsigned long arg);
103 static int mptctl_readtest(unsigned long arg);
104 static int mptctl_mpt_command(unsigned long arg);
105 static int mptctl_eventquery(unsigned long arg);
106 static int mptctl_eventenable(unsigned long arg);
107 static int mptctl_eventreport(unsigned long arg);
108 static int mptctl_replace_fw(unsigned long arg);
109 
110 static int mptctl_do_reset(unsigned long arg);
111 static int mptctl_hp_hostinfo(unsigned long arg, unsigned int cmd);
112 static int mptctl_hp_targetinfo(unsigned long arg);
113 
114 static int  mptctl_probe(struct pci_dev *, const struct pci_device_id *);
115 static void mptctl_remove(struct pci_dev *);
116 
117 #ifdef CONFIG_COMPAT
118 static long compat_mpctl_ioctl(struct file *f, unsigned cmd, unsigned long arg);
119 #endif
120 /*
121  * Private function calls.
122  */
123 static int mptctl_do_mpt_command(struct mpt_ioctl_command karg, void __user *mfPtr);
124 static int mptctl_do_fw_download(int ioc, char __user *ufwbuf, size_t fwlen);
125 static MptSge_t *kbuf_alloc_2_sgl(int bytes, u32 dir, int sge_offset, int *frags,
126 		struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc);
127 static void kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma,
128 		struct buflist *buflist, MPT_ADAPTER *ioc);
129 static void mptctl_timeout_expired (MPT_IOCTL *ioctl);
130 static int  mptctl_bus_reset(MPT_IOCTL *ioctl);
131 static int mptctl_set_tm_flags(MPT_SCSI_HOST *hd);
132 static void mptctl_free_tm_flags(MPT_ADAPTER *ioc);
133 
134 /*
135  * Reset Handler cleanup function
136  */
137 static int  mptctl_ioc_reset(MPT_ADAPTER *ioc, int reset_phase);
138 
139 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
140 /*
141  * Scatter gather list (SGL) sizes and limits...
142  */
143 //#define MAX_SCSI_FRAGS	9
144 #define MAX_FRAGS_SPILL1	9
145 #define MAX_FRAGS_SPILL2	15
146 #define FRAGS_PER_BUCKET	(MAX_FRAGS_SPILL2 + 1)
147 
148 //#define MAX_CHAIN_FRAGS	64
149 //#define MAX_CHAIN_FRAGS	(15+15+15+16)
150 #define MAX_CHAIN_FRAGS		(4 * MAX_FRAGS_SPILL2 + 1)
151 
152 //  Define max sg LIST bytes ( == (#frags + #chains) * 8 bytes each)
153 //  Works out to: 592d bytes!     (9+1)*8 + 4*(15+1)*8
154 //                  ^----------------- 80 + 512
155 #define MAX_SGL_BYTES		((MAX_FRAGS_SPILL1 + 1 + (4 * FRAGS_PER_BUCKET)) * 8)
156 
157 /* linux only seems to ever give 128kB MAX contiguous (GFP_USER) mem bytes */
158 #define MAX_KMALLOC_SZ		(128*1024)
159 
160 #define MPT_IOCTL_DEFAULT_TIMEOUT 10	/* Default timeout value (seconds) */
161 
162 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
163 /**
164  *	mptctl_syscall_down - Down the MPT adapter syscall semaphore.
165  *	@ioc: Pointer to MPT adapter
166  *	@nonblock: boolean, non-zero if O_NONBLOCK is set
167  *
168  *	All of the ioctl commands can potentially sleep, which is illegal
169  *	with a spinlock held, thus we perform mutual exclusion here.
170  *
171  *	Returns negative errno on error, or zero for success.
172  */
173 static inline int
174 mptctl_syscall_down(MPT_ADAPTER *ioc, int nonblock)
175 {
176 	int rc = 0;
177 	dctlprintk((KERN_INFO MYNAM "::mptctl_syscall_down(%p,%d) called\n", ioc, nonblock));
178 
179 	if (nonblock) {
180 		if (down_trylock(&ioc->ioctl->sem_ioc))
181 			rc = -EAGAIN;
182 	} else {
183 		if (down_interruptible(&ioc->ioctl->sem_ioc))
184 			rc = -ERESTARTSYS;
185 	}
186 	dctlprintk((KERN_INFO MYNAM "::mptctl_syscall_down return %d\n", rc));
187 	return rc;
188 }
189 
190 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
191 /*
192  *  This is the callback for any message we have posted. The message itself
193  *  will be returned to the message pool when we return from the IRQ
194  *
195  *  This runs in irq context so be short and sweet.
196  */
197 static int
198 mptctl_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req, MPT_FRAME_HDR *reply)
199 {
200 	char *sense_data;
201 	int sz, req_index;
202 	u16 iocStatus;
203 	u8 cmd;
204 
205 	dctlprintk(("mptctl_reply()!\n"));
206 	if (req)
207 		 cmd = req->u.hdr.Function;
208 	else
209 		return 1;
210 
211 	if (ioc->ioctl) {
212 
213 		if (reply==NULL) {
214 
215 			dctlprintk(("mptctl_reply() NULL Reply "
216 				"Function=%x!\n", cmd));
217 
218 			ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
219 			ioc->ioctl->reset &= ~MPTCTL_RESET_OK;
220 
221 			/* We are done, issue wake up
222 	 		*/
223 			ioc->ioctl->wait_done = 1;
224 			wake_up (&mptctl_wait);
225 			return 1;
226 
227 		}
228 
229 		dctlprintk(("mptctl_reply() with req=%p "
230 			"reply=%p Function=%x!\n", req, reply, cmd));
231 
232 		/* Copy the reply frame (which much exist
233 		 * for non-SCSI I/O) to the IOC structure.
234 		 */
235 		dctlprintk(("Copying Reply Frame @%p to ioc%d!\n",
236 			reply, ioc->id));
237 		memcpy(ioc->ioctl->ReplyFrame, reply,
238 			min(ioc->reply_sz, 4*reply->u.reply.MsgLength));
239 		ioc->ioctl->status |= MPT_IOCTL_STATUS_RF_VALID;
240 
241 		/* Set the command status to GOOD if IOC Status is GOOD
242 		 * OR if SCSI I/O cmd and data underrun or recovered error.
243 		 */
244 		iocStatus = le16_to_cpu(reply->u.reply.IOCStatus) & MPI_IOCSTATUS_MASK;
245 		if (iocStatus  == MPI_IOCSTATUS_SUCCESS)
246 			ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
247 
248 		if ((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) ||
249 			(cmd == MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
250 			ioc->ioctl->reset &= ~MPTCTL_RESET_OK;
251 
252 			if ((iocStatus == MPI_IOCSTATUS_SCSI_DATA_UNDERRUN) ||
253 			(iocStatus == MPI_IOCSTATUS_SCSI_RECOVERED_ERROR)) {
254 			ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
255 			}
256 		}
257 
258 		/* Copy the sense data - if present
259 		 */
260 		if ((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) &&
261 			(reply->u.sreply.SCSIState &
262 			 MPI_SCSI_STATE_AUTOSENSE_VALID)){
263 			sz = req->u.scsireq.SenseBufferLength;
264 			req_index =
265 			    le16_to_cpu(req->u.frame.hwhdr.msgctxu.fld.req_idx);
266 			sense_data =
267 			    ((u8 *)ioc->sense_buf_pool +
268 			     (req_index * MPT_SENSE_BUFFER_ALLOC));
269 			memcpy(ioc->ioctl->sense, sense_data, sz);
270 			ioc->ioctl->status |= MPT_IOCTL_STATUS_SENSE_VALID;
271 		}
272 
273 		if (cmd == MPI_FUNCTION_SCSI_TASK_MGMT)
274 			mptctl_free_tm_flags(ioc);
275 
276 		/* We are done, issue wake up
277 		 */
278 		ioc->ioctl->wait_done = 1;
279 		wake_up (&mptctl_wait);
280 	}
281 	return 1;
282 }
283 
284 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
285 /* mptctl_timeout_expired
286  *
287  * Expecting an interrupt, however timed out.
288  *
289  */
290 static void mptctl_timeout_expired (MPT_IOCTL *ioctl)
291 {
292 	int rc = 1;
293 
294 	dctlprintk((KERN_NOTICE MYNAM ": Timeout Expired! Host %d\n",
295 				ioctl->ioc->id));
296 	if (ioctl == NULL)
297 		return;
298 
299 	ioctl->wait_done = 0;
300 	if (ioctl->reset & MPTCTL_RESET_OK)
301 		rc = mptctl_bus_reset(ioctl);
302 
303 	if (rc) {
304 		/* Issue a reset for this device.
305 		 * The IOC is not responding.
306 		 */
307 		dctlprintk((MYIOC_s_INFO_FMT "Calling HardReset! \n",
308 			 ioctl->ioc->name));
309 		mpt_HardResetHandler(ioctl->ioc, NO_SLEEP);
310 	}
311 	return;
312 
313 }
314 
315 /* mptctl_bus_reset
316  *
317  * Bus reset code.
318  *
319  */
320 static int mptctl_bus_reset(MPT_IOCTL *ioctl)
321 {
322 	MPT_FRAME_HDR	*mf;
323 	SCSITaskMgmt_t	*pScsiTm;
324 	MPT_SCSI_HOST	*hd;
325 	int		 ii;
326 	int		 retval;
327 
328 
329 	ioctl->reset &= ~MPTCTL_RESET_OK;
330 
331 	if (ioctl->ioc->sh == NULL)
332 		return -EPERM;
333 
334 	hd = (MPT_SCSI_HOST *) ioctl->ioc->sh->hostdata;
335 	if (hd == NULL)
336 		return -EPERM;
337 
338 	/* Single threading ....
339 	 */
340 	if (mptctl_set_tm_flags(hd) != 0)
341 		return -EPERM;
342 
343 	/* Send request
344 	 */
345 	if ((mf = mpt_get_msg_frame(mptctl_id, ioctl->ioc)) == NULL) {
346 		dctlprintk((MYIOC_s_WARN_FMT "IssueTaskMgmt, no msg frames!!\n",
347 				ioctl->ioc->name));
348 
349 		mptctl_free_tm_flags(ioctl->ioc);
350 		return -ENOMEM;
351 	}
352 
353 	dtmprintk((MYIOC_s_INFO_FMT "IssueTaskMgmt request @ %p\n",
354 			ioctl->ioc->name, mf));
355 
356 	pScsiTm = (SCSITaskMgmt_t *) mf;
357 	pScsiTm->TargetID = ioctl->target;
358 	pScsiTm->Bus = hd->port;	/* 0 */
359 	pScsiTm->ChainOffset = 0;
360 	pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
361 	pScsiTm->Reserved = 0;
362 	pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS;
363 	pScsiTm->Reserved1 = 0;
364 	pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
365 
366 	for (ii= 0; ii < 8; ii++)
367 		pScsiTm->LUN[ii] = 0;
368 
369 	for (ii=0; ii < 7; ii++)
370 		pScsiTm->Reserved2[ii] = 0;
371 
372 	pScsiTm->TaskMsgContext = 0;
373 	dtmprintk((MYIOC_s_INFO_FMT
374 		"mptctl_bus_reset: issued.\n", ioctl->ioc->name));
375 
376 	DBG_DUMP_TM_REQUEST_FRAME((u32 *)mf);
377 
378 	ioctl->wait_done=0;
379 	if ((retval = mpt_send_handshake_request(mptctl_id, ioctl->ioc,
380 	     sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, CAN_SLEEP)) != 0) {
381 		dfailprintk((MYIOC_s_ERR_FMT "_send_handshake FAILED!"
382 			" (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
383 			hd->ioc, mf));
384 		goto mptctl_bus_reset_done;
385 	}
386 
387 	/* Now wait for the command to complete */
388 	ii = wait_event_interruptible_timeout(mptctl_wait,
389 	     ioctl->wait_done == 1,
390 	     HZ*5 /* 5 second timeout */);
391 
392 	if(ii <=0 && (ioctl->wait_done != 1 ))  {
393 		ioctl->wait_done = 0;
394 		retval = -1; /* return failure */
395 	}
396 
397 mptctl_bus_reset_done:
398 
399 	mpt_free_msg_frame(hd->ioc, mf);
400 	mptctl_free_tm_flags(ioctl->ioc);
401 	return retval;
402 }
403 
404 static int
405 mptctl_set_tm_flags(MPT_SCSI_HOST *hd) {
406 	unsigned long flags;
407 
408 	spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
409 
410 	if (hd->tmState == TM_STATE_NONE) {
411 		hd->tmState = TM_STATE_IN_PROGRESS;
412 		hd->tmPending = 1;
413 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
414 	} else {
415 		spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
416 		return -EBUSY;
417 	}
418 
419 	return 0;
420 }
421 
422 static void
423 mptctl_free_tm_flags(MPT_ADAPTER *ioc)
424 {
425 	MPT_SCSI_HOST * hd;
426 	unsigned long flags;
427 
428 	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
429 	if (hd == NULL)
430 		return;
431 
432 	spin_lock_irqsave(&ioc->FreeQlock, flags);
433 
434 	hd->tmState = TM_STATE_NONE;
435 	hd->tmPending = 0;
436 	spin_unlock_irqrestore(&ioc->FreeQlock, flags);
437 
438 	return;
439 }
440 
441 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
442 /* mptctl_ioc_reset
443  *
444  * Clean-up functionality. Used only if there has been a
445  * reload of the FW due.
446  *
447  */
448 static int
449 mptctl_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
450 {
451 	MPT_IOCTL *ioctl = ioc->ioctl;
452 	dctlprintk((KERN_INFO MYNAM ": IOC %s_reset routed to IOCTL driver!\n",
453 		reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
454 		reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
455 
456 	if(ioctl == NULL)
457 		return 1;
458 
459 	switch(reset_phase) {
460 	case MPT_IOC_SETUP_RESET:
461 		ioctl->status |= MPT_IOCTL_STATUS_DID_IOCRESET;
462 		break;
463 	case MPT_IOC_POST_RESET:
464 		ioctl->status &= ~MPT_IOCTL_STATUS_DID_IOCRESET;
465 		break;
466 	case MPT_IOC_PRE_RESET:
467 	default:
468 		break;
469 	}
470 
471 	return 1;
472 }
473 
474 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
475 /*
476  *  MPT ioctl handler
477  *  cmd - specify the particular IOCTL command to be issued
478  *  arg - data specific to the command. Must not be null.
479  */
480 static long
481 __mptctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
482 {
483 	mpt_ioctl_header __user *uhdr = (void __user *) arg;
484 	mpt_ioctl_header	 khdr;
485 	int iocnum;
486 	unsigned iocnumX;
487 	int nonblock = (file->f_flags & O_NONBLOCK);
488 	int ret;
489 	MPT_ADAPTER *iocp = NULL;
490 
491 	dctlprintk(("mptctl_ioctl() called\n"));
492 
493 	if (copy_from_user(&khdr, uhdr, sizeof(khdr))) {
494 		printk(KERN_ERR "%s::mptctl_ioctl() @%d - "
495 				"Unable to copy mpt_ioctl_header data @ %p\n",
496 				__FILE__, __LINE__, uhdr);
497 		return -EFAULT;
498 	}
499 	ret = -ENXIO;				/* (-6) No such device or address */
500 
501 	/* Verify intended MPT adapter - set iocnum and the adapter
502 	 * pointer (iocp)
503 	 */
504 	iocnumX = khdr.iocnum & 0xFF;
505 	if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
506 	    (iocp == NULL)) {
507 		dctlprintk((KERN_ERR "%s::mptctl_ioctl() @%d - ioc%d not found!\n",
508 				__FILE__, __LINE__, iocnumX));
509 		return -ENODEV;
510 	}
511 
512 	if (!iocp->active) {
513 		printk(KERN_ERR "%s::mptctl_ioctl() @%d - Controller disabled.\n",
514 				__FILE__, __LINE__);
515 		return -EFAULT;
516 	}
517 
518 	/* Handle those commands that are just returning
519 	 * information stored in the driver.
520 	 * These commands should never time out and are unaffected
521 	 * by TM and FW reloads.
522 	 */
523 	if ((cmd & ~IOCSIZE_MASK) == (MPTIOCINFO & ~IOCSIZE_MASK)) {
524 		return mptctl_getiocinfo(arg, _IOC_SIZE(cmd));
525 	} else if (cmd == MPTTARGETINFO) {
526 		return mptctl_gettargetinfo(arg);
527 	} else if (cmd == MPTTEST) {
528 		return mptctl_readtest(arg);
529 	} else if (cmd == MPTEVENTQUERY) {
530 		return mptctl_eventquery(arg);
531 	} else if (cmd == MPTEVENTENABLE) {
532 		return mptctl_eventenable(arg);
533 	} else if (cmd == MPTEVENTREPORT) {
534 		return mptctl_eventreport(arg);
535 	} else if (cmd == MPTFWREPLACE) {
536 		return mptctl_replace_fw(arg);
537 	}
538 
539 	/* All of these commands require an interrupt or
540 	 * are unknown/illegal.
541 	 */
542 	if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
543 		return ret;
544 
545 	dctlprintk((MYIOC_s_INFO_FMT ": mptctl_ioctl()\n", iocp->name));
546 
547 	if (cmd == MPTFWDOWNLOAD)
548 		ret = mptctl_fw_download(arg);
549 	else if (cmd == MPTCOMMAND)
550 		ret = mptctl_mpt_command(arg);
551 	else if (cmd == MPTHARDRESET)
552 		ret = mptctl_do_reset(arg);
553 	else if ((cmd & ~IOCSIZE_MASK) == (HP_GETHOSTINFO & ~IOCSIZE_MASK))
554 		ret = mptctl_hp_hostinfo(arg, _IOC_SIZE(cmd));
555 	else if (cmd == HP_GETTARGETINFO)
556 		ret = mptctl_hp_targetinfo(arg);
557 	else
558 		ret = -EINVAL;
559 
560 	up(&iocp->ioctl->sem_ioc);
561 
562 	return ret;
563 }
564 
565 static long
566 mptctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
567 {
568 	long ret;
569 	lock_kernel();
570 	ret = __mptctl_ioctl(file, cmd, arg);
571 	unlock_kernel();
572 	return ret;
573 }
574 
575 static int mptctl_do_reset(unsigned long arg)
576 {
577 	struct mpt_ioctl_diag_reset __user *urinfo = (void __user *) arg;
578 	struct mpt_ioctl_diag_reset krinfo;
579 	MPT_ADAPTER		*iocp;
580 
581 	dctlprintk((KERN_INFO "mptctl_do_reset called.\n"));
582 
583 	if (copy_from_user(&krinfo, urinfo, sizeof(struct mpt_ioctl_diag_reset))) {
584 		printk(KERN_ERR "%s@%d::mptctl_do_reset - "
585 				"Unable to copy mpt_ioctl_diag_reset struct @ %p\n",
586 				__FILE__, __LINE__, urinfo);
587 		return -EFAULT;
588 	}
589 
590 	if (mpt_verify_adapter(krinfo.hdr.iocnum, &iocp) < 0) {
591 		dctlprintk((KERN_ERR "%s@%d::mptctl_do_reset - ioc%d not found!\n",
592 				__FILE__, __LINE__, krinfo.hdr.iocnum));
593 		return -ENODEV; /* (-6) No such device or address */
594 	}
595 
596 	if (mpt_HardResetHandler(iocp, CAN_SLEEP) != 0) {
597 		printk (KERN_ERR "%s@%d::mptctl_do_reset - reset failed.\n",
598 			__FILE__, __LINE__);
599 		return -1;
600 	}
601 
602 	return 0;
603 }
604 
605 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
606 /*
607  * MPT FW download function.  Cast the arg into the mpt_fw_xfer structure.
608  * This structure contains: iocnum, firmware length (bytes),
609  *      pointer to user space memory where the fw image is stored.
610  *
611  * Outputs:	None.
612  * Return:	0 if successful
613  *		-EFAULT if data unavailable
614  *		-ENXIO  if no such device
615  *		-EAGAIN if resource problem
616  *		-ENOMEM if no memory for SGE
617  *		-EMLINK if too many chain buffers required
618  *		-EBADRQC if adapter does not support FW download
619  *		-EBUSY if adapter is busy
620  *		-ENOMSG if FW upload returned bad status
621  */
622 static int
623 mptctl_fw_download(unsigned long arg)
624 {
625 	struct mpt_fw_xfer __user *ufwdl = (void __user *) arg;
626 	struct mpt_fw_xfer	 kfwdl;
627 
628 	dctlprintk((KERN_INFO "mptctl_fwdl called. mptctl_id = %xh\n", mptctl_id)); //tc
629 	if (copy_from_user(&kfwdl, ufwdl, sizeof(struct mpt_fw_xfer))) {
630 		printk(KERN_ERR "%s@%d::_ioctl_fwdl - "
631 				"Unable to copy mpt_fw_xfer struct @ %p\n",
632 				__FILE__, __LINE__, ufwdl);
633 		return -EFAULT;
634 	}
635 
636 	return mptctl_do_fw_download(kfwdl.iocnum, kfwdl.bufp, kfwdl.fwlen);
637 }
638 
639 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
640 /*
641  * FW Download engine.
642  * Outputs:	None.
643  * Return:	0 if successful
644  *		-EFAULT if data unavailable
645  *		-ENXIO  if no such device
646  *		-EAGAIN if resource problem
647  *		-ENOMEM if no memory for SGE
648  *		-EMLINK if too many chain buffers required
649  *		-EBADRQC if adapter does not support FW download
650  *		-EBUSY if adapter is busy
651  *		-ENOMSG if FW upload returned bad status
652  */
653 static int
654 mptctl_do_fw_download(int ioc, char __user *ufwbuf, size_t fwlen)
655 {
656 	FWDownload_t		*dlmsg;
657 	MPT_FRAME_HDR		*mf;
658 	MPT_ADAPTER		*iocp;
659 	FWDownloadTCSGE_t	*ptsge;
660 	MptSge_t		*sgl, *sgIn;
661 	char			*sgOut;
662 	struct buflist		*buflist;
663 	struct buflist		*bl;
664 	dma_addr_t		 sgl_dma;
665 	int			 ret;
666 	int			 numfrags = 0;
667 	int			 maxfrags;
668 	int			 n = 0;
669 	u32			 sgdir;
670 	u32			 nib;
671 	int			 fw_bytes_copied = 0;
672 	int			 i;
673 	int			 sge_offset = 0;
674 	u16			 iocstat;
675 	pFWDownloadReply_t	 ReplyMsg = NULL;
676 
677 	dctlprintk((KERN_INFO "mptctl_do_fwdl called. mptctl_id = %xh.\n", mptctl_id));
678 
679 	dctlprintk((KERN_INFO "DbG: kfwdl.bufp  = %p\n", ufwbuf));
680 	dctlprintk((KERN_INFO "DbG: kfwdl.fwlen = %d\n", (int)fwlen));
681 	dctlprintk((KERN_INFO "DbG: kfwdl.ioc   = %04xh\n", ioc));
682 
683 	if ((ioc = mpt_verify_adapter(ioc, &iocp)) < 0) {
684 		dctlprintk(("%s@%d::_ioctl_fwdl - ioc%d not found!\n",
685 				__FILE__, __LINE__, ioc));
686 		return -ENODEV; /* (-6) No such device or address */
687 	}
688 
689 	/*  Valid device. Get a message frame and construct the FW download message.
690 	 */
691 	if ((mf = mpt_get_msg_frame(mptctl_id, iocp)) == NULL)
692 		return -EAGAIN;
693 	dlmsg = (FWDownload_t*) mf;
694 	ptsge = (FWDownloadTCSGE_t *) &dlmsg->SGL;
695 	sgOut = (char *) (ptsge + 1);
696 
697 	/*
698 	 * Construct f/w download request
699 	 */
700 	dlmsg->ImageType = MPI_FW_DOWNLOAD_ITYPE_FW;
701 	dlmsg->Reserved = 0;
702 	dlmsg->ChainOffset = 0;
703 	dlmsg->Function = MPI_FUNCTION_FW_DOWNLOAD;
704 	dlmsg->Reserved1[0] = dlmsg->Reserved1[1] = dlmsg->Reserved1[2] = 0;
705 	dlmsg->MsgFlags = 0;
706 
707 	/* Set up the Transaction SGE.
708 	 */
709 	ptsge->Reserved = 0;
710 	ptsge->ContextSize = 0;
711 	ptsge->DetailsLength = 12;
712 	ptsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
713 	ptsge->Reserved_0100_Checksum = 0;
714 	ptsge->ImageOffset = 0;
715 	ptsge->ImageSize = cpu_to_le32(fwlen);
716 
717 	/* Add the SGL
718 	 */
719 
720 	/*
721 	 * Need to kmalloc area(s) for holding firmware image bytes.
722 	 * But we need to do it piece meal, using a proper
723 	 * scatter gather list (with 128kB MAX hunks).
724 	 *
725 	 * A practical limit here might be # of sg hunks that fit into
726 	 * a single IOC request frame; 12 or 8 (see below), so:
727 	 * For FC9xx: 12 x 128kB == 1.5 mB (max)
728 	 * For C1030:  8 x 128kB == 1   mB (max)
729 	 * We could support chaining, but things get ugly(ier:)
730 	 *
731 	 * Set the sge_offset to the start of the sgl (bytes).
732 	 */
733 	sgdir = 0x04000000;		/* IOC will READ from sys mem */
734 	sge_offset = sizeof(MPIHeader_t) + sizeof(FWDownloadTCSGE_t);
735 	if ((sgl = kbuf_alloc_2_sgl(fwlen, sgdir, sge_offset,
736 				    &numfrags, &buflist, &sgl_dma, iocp)) == NULL)
737 		return -ENOMEM;
738 
739 	/*
740 	 * We should only need SGL with 2 simple_32bit entries (up to 256 kB)
741 	 * for FC9xx f/w image, but calculate max number of sge hunks
742 	 * we can fit into a request frame, and limit ourselves to that.
743 	 * (currently no chain support)
744 	 * maxfrags = (Request Size - FWdownload Size ) / Size of 32 bit SGE
745 	 *	Request		maxfrags
746 	 *	128		12
747 	 *	96		8
748 	 *	64		4
749 	 */
750 	maxfrags = (iocp->req_sz - sizeof(MPIHeader_t) - sizeof(FWDownloadTCSGE_t))
751 			/ (sizeof(dma_addr_t) + sizeof(u32));
752 	if (numfrags > maxfrags) {
753 		ret = -EMLINK;
754 		goto fwdl_out;
755 	}
756 
757 	dctlprintk((KERN_INFO "DbG: sgl buffer  = %p, sgfrags = %d\n", sgl, numfrags));
758 
759 	/*
760 	 * Parse SG list, copying sgl itself,
761 	 * plus f/w image hunks from user space as we go...
762 	 */
763 	ret = -EFAULT;
764 	sgIn = sgl;
765 	bl = buflist;
766 	for (i=0; i < numfrags; i++) {
767 
768 		/* Get the SGE type: 0 - TCSGE, 3 - Chain, 1 - Simple SGE
769 		 * Skip everything but Simple. If simple, copy from
770 		 *	user space into kernel space.
771 		 * Note: we should not have anything but Simple as
772 		 *	Chain SGE are illegal.
773 		 */
774 		nib = (sgIn->FlagsLength & 0x30000000) >> 28;
775 		if (nib == 0 || nib == 3) {
776 			;
777 		} else if (sgIn->Address) {
778 			mpt_add_sge(sgOut, sgIn->FlagsLength, sgIn->Address);
779 			n++;
780 			if (copy_from_user(bl->kptr, ufwbuf+fw_bytes_copied, bl->len)) {
781 				printk(KERN_ERR "%s@%d::_ioctl_fwdl - "
782 						"Unable to copy f/w buffer hunk#%d @ %p\n",
783 						__FILE__, __LINE__, n, ufwbuf);
784 				goto fwdl_out;
785 			}
786 			fw_bytes_copied += bl->len;
787 		}
788 		sgIn++;
789 		bl++;
790 		sgOut += (sizeof(dma_addr_t) + sizeof(u32));
791 	}
792 
793 #ifdef MPT_DEBUG
794 	{
795 		u32 *m = (u32 *)mf;
796 		printk(KERN_INFO MYNAM ": F/W download request:\n" KERN_INFO " ");
797 		for (i=0; i < 7+numfrags*2; i++)
798 			printk(" %08x", le32_to_cpu(m[i]));
799 		printk("\n");
800 	}
801 #endif
802 
803 	/*
804 	 * Finally, perform firmware download.
805 	 */
806 	iocp->ioctl->wait_done = 0;
807 	mpt_put_msg_frame(mptctl_id, iocp, mf);
808 
809 	/* Now wait for the command to complete */
810 	ret = wait_event_interruptible_timeout(mptctl_wait,
811 	     iocp->ioctl->wait_done == 1,
812 	     HZ*60);
813 
814 	if(ret <=0 && (iocp->ioctl->wait_done != 1 )) {
815 	/* Now we need to reset the board */
816 		mptctl_timeout_expired(iocp->ioctl);
817 		ret = -ENODATA;
818 		goto fwdl_out;
819 	}
820 
821 	if (sgl)
822 		kfree_sgl(sgl, sgl_dma, buflist, iocp);
823 
824 	ReplyMsg = (pFWDownloadReply_t)iocp->ioctl->ReplyFrame;
825 	iocstat = le16_to_cpu(ReplyMsg->IOCStatus) & MPI_IOCSTATUS_MASK;
826 	if (iocstat == MPI_IOCSTATUS_SUCCESS) {
827 		printk(KERN_INFO MYNAM ": F/W update successfully sent to %s!\n", iocp->name);
828 		return 0;
829 	} else if (iocstat == MPI_IOCSTATUS_INVALID_FUNCTION) {
830 		printk(KERN_WARNING MYNAM ": ?Hmmm...  %s says it doesn't support F/W download!?!\n",
831 				iocp->name);
832 		printk(KERN_WARNING MYNAM ": (time to go bang on somebodies door)\n");
833 		return -EBADRQC;
834 	} else if (iocstat == MPI_IOCSTATUS_BUSY) {
835 		printk(KERN_WARNING MYNAM ": Warning!  %s says: IOC_BUSY!\n", iocp->name);
836 		printk(KERN_WARNING MYNAM ": (try again later?)\n");
837 		return -EBUSY;
838 	} else {
839 		printk(KERN_WARNING MYNAM "::ioctl_fwdl() ERROR!  %s returned [bad] status = %04xh\n",
840 				    iocp->name, iocstat);
841 		printk(KERN_WARNING MYNAM ": (bad VooDoo)\n");
842 		return -ENOMSG;
843 	}
844 	return 0;
845 
846 fwdl_out:
847         kfree_sgl(sgl, sgl_dma, buflist, iocp);
848 	return ret;
849 }
850 
851 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
852 /*
853  * SGE Allocation routine
854  *
855  * Inputs:	bytes - number of bytes to be transferred
856  *		sgdir - data direction
857  *		sge_offset - offset (in bytes) from the start of the request
858  *			frame to the first SGE
859  *		ioc - pointer to the mptadapter
860  * Outputs:	frags - number of scatter gather elements
861  *		blp - point to the buflist pointer
862  *		sglbuf_dma - pointer to the (dma) sgl
863  * Returns:	Null if failes
864  *		pointer to the (virtual) sgl if successful.
865  */
866 static MptSge_t *
867 kbuf_alloc_2_sgl(int bytes, u32 sgdir, int sge_offset, int *frags,
868 		 struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc)
869 {
870 	MptSge_t	*sglbuf = NULL;		/* pointer to array of SGE */
871 						/* and chain buffers */
872 	struct buflist	*buflist = NULL;	/* kernel routine */
873 	MptSge_t	*sgl;
874 	int		 numfrags = 0;
875 	int		 fragcnt = 0;
876 	int		 alloc_sz = min(bytes,MAX_KMALLOC_SZ);	// avoid kernel warning msg!
877 	int		 bytes_allocd = 0;
878 	int		 this_alloc;
879 	dma_addr_t	 pa;					// phys addr
880 	int		 i, buflist_ent;
881 	int		 sg_spill = MAX_FRAGS_SPILL1;
882 	int		 dir;
883 	/* initialization */
884 	*frags = 0;
885 	*blp = NULL;
886 
887 	/* Allocate and initialize an array of kernel
888 	 * structures for the SG elements.
889 	 */
890 	i = MAX_SGL_BYTES / 8;
891 	buflist = kmalloc(i, GFP_USER);
892 	if (buflist == NULL)
893 		return NULL;
894 	memset(buflist, 0, i);
895 	buflist_ent = 0;
896 
897 	/* Allocate a single block of memory to store the sg elements and
898 	 * the chain buffers.  The calling routine is responsible for
899 	 * copying the data in this array into the correct place in the
900 	 * request and chain buffers.
901 	 */
902 	sglbuf = pci_alloc_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf_dma);
903 	if (sglbuf == NULL)
904 		goto free_and_fail;
905 
906 	if (sgdir & 0x04000000)
907 		dir = PCI_DMA_TODEVICE;
908 	else
909 		dir = PCI_DMA_FROMDEVICE;
910 
911 	/* At start:
912 	 *	sgl = sglbuf = point to beginning of sg buffer
913 	 *	buflist_ent = 0 = first kernel structure
914 	 *	sg_spill = number of SGE that can be written before the first
915 	 *		chain element.
916 	 *
917 	 */
918 	sgl = sglbuf;
919 	sg_spill = ((ioc->req_sz - sge_offset)/(sizeof(dma_addr_t) + sizeof(u32))) - 1;
920 	while (bytes_allocd < bytes) {
921 		this_alloc = min(alloc_sz, bytes-bytes_allocd);
922 		buflist[buflist_ent].len = this_alloc;
923 		buflist[buflist_ent].kptr = pci_alloc_consistent(ioc->pcidev,
924 								 this_alloc,
925 								 &pa);
926 		if (buflist[buflist_ent].kptr == NULL) {
927 			alloc_sz = alloc_sz / 2;
928 			if (alloc_sz == 0) {
929 				printk(KERN_WARNING MYNAM "-SG: No can do - "
930 						    "not enough memory!   :-(\n");
931 				printk(KERN_WARNING MYNAM "-SG: (freeing %d frags)\n",
932 						    numfrags);
933 				goto free_and_fail;
934 			}
935 			continue;
936 		} else {
937 			dma_addr_t dma_addr;
938 
939 			bytes_allocd += this_alloc;
940 			sgl->FlagsLength = (0x10000000|MPT_SGE_FLAGS_ADDRESSING|sgdir|this_alloc);
941 			dma_addr = pci_map_single(ioc->pcidev, buflist[buflist_ent].kptr, this_alloc, dir);
942 			sgl->Address = dma_addr;
943 
944 			fragcnt++;
945 			numfrags++;
946 			sgl++;
947 			buflist_ent++;
948 		}
949 
950 		if (bytes_allocd >= bytes)
951 			break;
952 
953 		/* Need to chain? */
954 		if (fragcnt == sg_spill) {
955 			printk(KERN_WARNING MYNAM "-SG: No can do - " "Chain required!   :-(\n");
956 			printk(KERN_WARNING MYNAM "(freeing %d frags)\n", numfrags);
957 			goto free_and_fail;
958 		}
959 
960 		/* overflow check... */
961 		if (numfrags*8 > MAX_SGL_BYTES){
962 			/* GRRRRR... */
963 			printk(KERN_WARNING MYNAM "-SG: No can do - "
964 					    "too many SG frags!   :-(\n");
965 			printk(KERN_WARNING MYNAM "-SG: (freeing %d frags)\n",
966 					    numfrags);
967 			goto free_and_fail;
968 		}
969 	}
970 
971 	/* Last sge fixup: set LE+eol+eob bits */
972 	sgl[-1].FlagsLength |= 0xC1000000;
973 
974 	*frags = numfrags;
975 	*blp = buflist;
976 
977 	dctlprintk((KERN_INFO MYNAM "-SG: kbuf_alloc_2_sgl() - "
978 			   "%d SG frags generated!\n",
979 			   numfrags));
980 
981 	dctlprintk((KERN_INFO MYNAM "-SG: kbuf_alloc_2_sgl() - "
982 			   "last (big) alloc_sz=%d\n",
983 			   alloc_sz));
984 
985 	return sglbuf;
986 
987 free_and_fail:
988 	if (sglbuf != NULL) {
989 		int i;
990 
991 		for (i = 0; i < numfrags; i++) {
992 			dma_addr_t dma_addr;
993 			u8 *kptr;
994 			int len;
995 
996 			if ((sglbuf[i].FlagsLength >> 24) == 0x30)
997 				continue;
998 
999 			dma_addr = sglbuf[i].Address;
1000 			kptr = buflist[i].kptr;
1001 			len = buflist[i].len;
1002 
1003 			pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1004 		}
1005 		pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf, *sglbuf_dma);
1006 	}
1007 	kfree(buflist);
1008 	return NULL;
1009 }
1010 
1011 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1012 /*
1013  * Routine to free the SGL elements.
1014  */
1015 static void
1016 kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma, struct buflist *buflist, MPT_ADAPTER *ioc)
1017 {
1018 	MptSge_t	*sg = sgl;
1019 	struct buflist	*bl = buflist;
1020 	u32		 nib;
1021 	int		 dir;
1022 	int		 n = 0;
1023 
1024 	if (sg->FlagsLength & 0x04000000)
1025 		dir = PCI_DMA_TODEVICE;
1026 	else
1027 		dir = PCI_DMA_FROMDEVICE;
1028 
1029 	nib = (sg->FlagsLength & 0xF0000000) >> 28;
1030 	while (! (nib & 0x4)) { /* eob */
1031 		/* skip ignore/chain. */
1032 		if (nib == 0 || nib == 3) {
1033 			;
1034 		} else if (sg->Address) {
1035 			dma_addr_t dma_addr;
1036 			void *kptr;
1037 			int len;
1038 
1039 			dma_addr = sg->Address;
1040 			kptr = bl->kptr;
1041 			len = bl->len;
1042 			pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1043 			pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1044 			n++;
1045 		}
1046 		sg++;
1047 		bl++;
1048 		nib = (le32_to_cpu(sg->FlagsLength) & 0xF0000000) >> 28;
1049 	}
1050 
1051 	/* we're at eob! */
1052 	if (sg->Address) {
1053 		dma_addr_t dma_addr;
1054 		void *kptr;
1055 		int len;
1056 
1057 		dma_addr = sg->Address;
1058 		kptr = bl->kptr;
1059 		len = bl->len;
1060 		pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1061 		pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1062 		n++;
1063 	}
1064 
1065 	pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sgl, sgl_dma);
1066 	kfree(buflist);
1067 	dctlprintk((KERN_INFO MYNAM "-SG: Free'd 1 SGL buf + %d kbufs!\n", n));
1068 }
1069 
1070 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1071 /*
1072  *	mptctl_getiocinfo - Query the host adapter for IOC information.
1073  *	@arg: User space argument
1074  *
1075  * Outputs:	None.
1076  * Return:	0 if successful
1077  *		-EFAULT if data unavailable
1078  *		-ENODEV  if no such device/adapter
1079  */
1080 static int
1081 mptctl_getiocinfo (unsigned long arg, unsigned int data_size)
1082 {
1083 	struct mpt_ioctl_iocinfo __user *uarg = (void __user *) arg;
1084 	struct mpt_ioctl_iocinfo *karg;
1085 	MPT_ADAPTER		*ioc;
1086 	struct pci_dev		*pdev;
1087 	struct Scsi_Host	*sh;
1088 	MPT_SCSI_HOST		*hd;
1089 	int			iocnum;
1090 	int			numDevices = 0;
1091 	unsigned int		max_id;
1092 	int			ii;
1093 	unsigned int		port;
1094 	int			cim_rev;
1095 	u8			revision;
1096 
1097 	dctlprintk((": mptctl_getiocinfo called.\n"));
1098 	/* Add of PCI INFO results in unaligned access for
1099 	 * IA64 and Sparc. Reset long to int. Return no PCI
1100 	 * data for obsolete format.
1101 	 */
1102 	if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev0))
1103 		cim_rev = 0;
1104 	else if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev1))
1105 		cim_rev = 1;
1106 	else if (data_size == sizeof(struct mpt_ioctl_iocinfo))
1107 		cim_rev = 2;
1108 	else if (data_size == (sizeof(struct mpt_ioctl_iocinfo_rev0)+12))
1109 		cim_rev = 0;	/* obsolete */
1110 	else
1111 		return -EFAULT;
1112 
1113 	karg = kmalloc(data_size, GFP_KERNEL);
1114 	if (karg == NULL) {
1115 		printk(KERN_ERR "%s::mpt_ioctl_iocinfo() @%d - no memory available!\n",
1116 				__FILE__, __LINE__);
1117 		return -ENOMEM;
1118 	}
1119 
1120 	if (copy_from_user(karg, uarg, data_size)) {
1121 		printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
1122 			"Unable to read in mpt_ioctl_iocinfo struct @ %p\n",
1123 				__FILE__, __LINE__, uarg);
1124 		kfree(karg);
1125 		return -EFAULT;
1126 	}
1127 
1128 	if (((iocnum = mpt_verify_adapter(karg->hdr.iocnum, &ioc)) < 0) ||
1129 	    (ioc == NULL)) {
1130 		dctlprintk((KERN_ERR "%s::mptctl_getiocinfo() @%d - ioc%d not found!\n",
1131 				__FILE__, __LINE__, iocnum));
1132 		kfree(karg);
1133 		return -ENODEV;
1134 	}
1135 
1136 	/* Verify the data transfer size is correct. */
1137 	if (karg->hdr.maxDataSize != data_size) {
1138 		printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
1139 			"Structure size mismatch. Command not completed.\n",
1140 				__FILE__, __LINE__);
1141 		kfree(karg);
1142 		return -EFAULT;
1143 	}
1144 
1145 	/* Fill in the data and return the structure to the calling
1146 	 * program
1147 	 */
1148 	if (ioc->bus_type == FC)
1149 		karg->adapterType = MPT_IOCTL_INTERFACE_FC;
1150 	else
1151 		karg->adapterType = MPT_IOCTL_INTERFACE_SCSI;
1152 
1153 	if (karg->hdr.port > 1)
1154 		return -EINVAL;
1155 	port = karg->hdr.port;
1156 
1157 	karg->port = port;
1158 	pdev = (struct pci_dev *) ioc->pcidev;
1159 
1160 	karg->pciId = pdev->device;
1161 	pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1162 	karg->hwRev = revision;
1163 	karg->subSystemDevice = pdev->subsystem_device;
1164 	karg->subSystemVendor = pdev->subsystem_vendor;
1165 
1166 	if (cim_rev == 1) {
1167 		/* Get the PCI bus, device, and function numbers for the IOC
1168 		 */
1169 		karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1170 		karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1171 		karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1172 	} else if (cim_rev == 2) {
1173 		/* Get the PCI bus, device, function and segment ID numbers
1174 		   for the IOC */
1175 		karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1176 		karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1177 		karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1178 		karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1179 		karg->pciInfo.segmentID = pci_domain_nr(pdev->bus);
1180 	}
1181 
1182 	/* Get number of devices
1183          */
1184 	if ((sh = ioc->sh) != NULL) {
1185 		 /* sh->max_id = maximum target ID + 1
1186 		 */
1187 		max_id = sh->max_id - 1;
1188 		hd = (MPT_SCSI_HOST *) sh->hostdata;
1189 
1190 		/* Check all of the target structures and
1191 		 * keep a counter.
1192 		 */
1193 		if (hd && hd->Targets) {
1194 			for (ii = 0; ii <= max_id; ii++) {
1195 				if (hd->Targets[ii])
1196 					numDevices++;
1197 			}
1198 		}
1199 	}
1200 	karg->numDevices = numDevices;
1201 
1202 	/* Set the BIOS and FW Version
1203 	 */
1204 	karg->FWVersion = ioc->facts.FWVersion.Word;
1205 	karg->BIOSVersion = ioc->biosVersion;
1206 
1207 	/* Set the Version Strings.
1208 	 */
1209 	strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
1210 	karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
1211 
1212 	karg->busChangeEvent = 0;
1213 	karg->hostId = ioc->pfacts[port].PortSCSIID;
1214 	karg->rsvd[0] = karg->rsvd[1] = 0;
1215 
1216 	/* Copy the data from kernel memory to user memory
1217 	 */
1218 	if (copy_to_user((char __user *)arg, karg, data_size)) {
1219 		printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
1220 			"Unable to write out mpt_ioctl_iocinfo struct @ %p\n",
1221 				__FILE__, __LINE__, uarg);
1222 		kfree(karg);
1223 		return -EFAULT;
1224 	}
1225 
1226 	kfree(karg);
1227 	return 0;
1228 }
1229 
1230 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1231 /*
1232  *	mptctl_gettargetinfo - Query the host adapter for target information.
1233  *	@arg: User space argument
1234  *
1235  * Outputs:	None.
1236  * Return:	0 if successful
1237  *		-EFAULT if data unavailable
1238  *		-ENODEV  if no such device/adapter
1239  */
1240 static int
1241 mptctl_gettargetinfo (unsigned long arg)
1242 {
1243 	struct mpt_ioctl_targetinfo __user *uarg = (void __user *) arg;
1244 	struct mpt_ioctl_targetinfo karg;
1245 	MPT_ADAPTER		*ioc;
1246 	struct Scsi_Host	*sh;
1247 	MPT_SCSI_HOST		*hd;
1248 	VirtTarget		*vdev;
1249 	char			*pmem;
1250 	int			*pdata;
1251 	IOCPage2_t		*pIoc2;
1252 	IOCPage3_t		*pIoc3;
1253 	int			iocnum;
1254 	int			numDevices = 0;
1255 	unsigned int		max_id;
1256 	int			id, jj, indexed_lun, lun_index;
1257 	u32			lun;
1258 	int			maxWordsLeft;
1259 	int			numBytes;
1260 	u8			port, devType, bus_id;
1261 
1262 	dctlprintk(("mptctl_gettargetinfo called.\n"));
1263 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_targetinfo))) {
1264 		printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
1265 			"Unable to read in mpt_ioctl_targetinfo struct @ %p\n",
1266 				__FILE__, __LINE__, uarg);
1267 		return -EFAULT;
1268 	}
1269 
1270 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1271 	    (ioc == NULL)) {
1272 		dctlprintk((KERN_ERR "%s::mptctl_gettargetinfo() @%d - ioc%d not found!\n",
1273 				__FILE__, __LINE__, iocnum));
1274 		return -ENODEV;
1275 	}
1276 
1277 	/* Get the port number and set the maximum number of bytes
1278 	 * in the returned structure.
1279 	 * Ignore the port setting.
1280 	 */
1281 	numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1282 	maxWordsLeft = numBytes/sizeof(int);
1283 	port = karg.hdr.port;
1284 
1285 	if (maxWordsLeft <= 0) {
1286 		printk(KERN_ERR "%s::mptctl_gettargetinfo() @%d - no memory available!\n",
1287 				__FILE__, __LINE__);
1288 		return -ENOMEM;
1289 	}
1290 
1291 	/* Fill in the data and return the structure to the calling
1292 	 * program
1293 	 */
1294 
1295 	/* struct mpt_ioctl_targetinfo does not contain sufficient space
1296 	 * for the target structures so when the IOCTL is called, there is
1297 	 * not sufficient stack space for the structure. Allocate memory,
1298 	 * populate the memory, copy back to the user, then free memory.
1299 	 * targetInfo format:
1300 	 * bits 31-24: reserved
1301 	 *      23-16: LUN
1302 	 *      15- 8: Bus Number
1303 	 *       7- 0: Target ID
1304 	 */
1305 	pmem = kmalloc(numBytes, GFP_KERNEL);
1306 	if (pmem == NULL) {
1307 		printk(KERN_ERR "%s::mptctl_gettargetinfo() @%d - no memory available!\n",
1308 				__FILE__, __LINE__);
1309 		return -ENOMEM;
1310 	}
1311 	memset(pmem, 0, numBytes);
1312 	pdata =  (int *) pmem;
1313 
1314 	/* Get number of devices
1315          */
1316 	if ((sh = ioc->sh) != NULL) {
1317 
1318 		max_id = sh->max_id - 1;
1319 		hd = (MPT_SCSI_HOST *) sh->hostdata;
1320 
1321 		/* Check all of the target structures.
1322 		 * Save the Id and increment the counter,
1323 		 * if ptr non-null.
1324 		 * sh->max_id = maximum target ID + 1
1325 		 */
1326 		if (hd && hd->Targets) {
1327 			mpt_findImVolumes(ioc);
1328 			pIoc2 = ioc->raid_data.pIocPg2;
1329 			for ( id = 0; id <= max_id; ) {
1330 				if ( pIoc2 && pIoc2->NumActiveVolumes ) {
1331 					if ( id == pIoc2->RaidVolume[0].VolumeID ) {
1332 						if (maxWordsLeft <= 0) {
1333 							printk(KERN_ERR "mptctl_gettargetinfo - "
1334 			"buffer is full but volume is available on ioc %d\n, numDevices=%d", iocnum, numDevices);
1335 							goto data_space_full;
1336 						}
1337 						if ( ( pIoc2->RaidVolume[0].Flags & MPI_IOCPAGE2_FLAG_VOLUME_INACTIVE ) == 0 )
1338                         				devType = 0x80;
1339                     				else
1340                         				devType = 0xC0;
1341 						bus_id = pIoc2->RaidVolume[0].VolumeBus;
1342 	            				numDevices++;
1343                     				*pdata = ( (devType << 24) | (bus_id << 8) | id );
1344 						dctlprintk((KERN_ERR "mptctl_gettargetinfo - "
1345 		"volume ioc=%d target=%x numDevices=%d pdata=%p\n", iocnum, *pdata, numDevices, pdata));
1346                     				pdata++;
1347 						--maxWordsLeft;
1348 						goto next_id;
1349 					} else {
1350 						pIoc3 = ioc->raid_data.pIocPg3;
1351             					for ( jj = 0; jj < pIoc3->NumPhysDisks; jj++ ) {
1352                     					if ( pIoc3->PhysDisk[jj].PhysDiskID == id )
1353 								goto next_id;
1354 						}
1355 					}
1356 				}
1357 				if ( (vdev = hd->Targets[id]) ) {
1358 					for (jj = 0; jj <= MPT_LAST_LUN; jj++) {
1359 						lun_index = (jj >> 5);
1360 						indexed_lun = (jj % 32);
1361 						lun = (1 << indexed_lun);
1362 						if (vdev->luns[lun_index] & lun) {
1363 							if (maxWordsLeft <= 0) {
1364 								printk(KERN_ERR "mptctl_gettargetinfo - "
1365 			"buffer is full but more targets are available on ioc %d numDevices=%d\n", iocnum, numDevices);
1366 								goto data_space_full;
1367 							}
1368 							bus_id = vdev->bus_id;
1369 							numDevices++;
1370                             				*pdata = ( (jj << 16) | (bus_id << 8) | id );
1371 							dctlprintk((KERN_ERR "mptctl_gettargetinfo - "
1372 		"target ioc=%d target=%x numDevices=%d pdata=%p\n", iocnum, *pdata, numDevices, pdata));
1373 							pdata++;
1374 							--maxWordsLeft;
1375 						}
1376 					}
1377 				}
1378 next_id:
1379 				id++;
1380 			}
1381 		}
1382 	}
1383 data_space_full:
1384 	karg.numDevices = numDevices;
1385 
1386 	/* Copy part of the data from kernel memory to user memory
1387 	 */
1388 	if (copy_to_user((char __user *)arg, &karg,
1389 				sizeof(struct mpt_ioctl_targetinfo))) {
1390 		printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
1391 			"Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
1392 				__FILE__, __LINE__, uarg);
1393 		kfree(pmem);
1394 		return -EFAULT;
1395 	}
1396 
1397 	/* Copy the remaining data from kernel memory to user memory
1398 	 */
1399 	if (copy_to_user(uarg->targetInfo, pmem, numBytes)) {
1400 		printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
1401 			"Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
1402 				__FILE__, __LINE__, pdata);
1403 		kfree(pmem);
1404 		return -EFAULT;
1405 	}
1406 
1407 	kfree(pmem);
1408 
1409 	return 0;
1410 }
1411 
1412 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1413 /* MPT IOCTL Test function.
1414  *
1415  * Outputs:	None.
1416  * Return:	0 if successful
1417  *		-EFAULT if data unavailable
1418  *		-ENODEV  if no such device/adapter
1419  */
1420 static int
1421 mptctl_readtest (unsigned long arg)
1422 {
1423 	struct mpt_ioctl_test __user *uarg = (void __user *) arg;
1424 	struct mpt_ioctl_test	 karg;
1425 	MPT_ADAPTER *ioc;
1426 	int iocnum;
1427 
1428 	dctlprintk(("mptctl_readtest called.\n"));
1429 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_test))) {
1430 		printk(KERN_ERR "%s@%d::mptctl_readtest - "
1431 			"Unable to read in mpt_ioctl_test struct @ %p\n",
1432 				__FILE__, __LINE__, uarg);
1433 		return -EFAULT;
1434 	}
1435 
1436 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1437 	    (ioc == NULL)) {
1438 		dctlprintk((KERN_ERR "%s::mptctl_readtest() @%d - ioc%d not found!\n",
1439 				__FILE__, __LINE__, iocnum));
1440 		return -ENODEV;
1441 	}
1442 
1443 	/* Fill in the data and return the structure to the calling
1444 	 * program
1445 	 */
1446 
1447 #ifdef MFCNT
1448 	karg.chip_type = ioc->mfcnt;
1449 #else
1450 	karg.chip_type = ioc->pcidev->device;
1451 #endif
1452 	strncpy (karg.name, ioc->name, MPT_MAX_NAME);
1453 	karg.name[MPT_MAX_NAME-1]='\0';
1454 	strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
1455 	karg.product[MPT_PRODUCT_LENGTH-1]='\0';
1456 
1457 	/* Copy the data from kernel memory to user memory
1458 	 */
1459 	if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_test))) {
1460 		printk(KERN_ERR "%s@%d::mptctl_readtest - "
1461 			"Unable to write out mpt_ioctl_test struct @ %p\n",
1462 				__FILE__, __LINE__, uarg);
1463 		return -EFAULT;
1464 	}
1465 
1466 	return 0;
1467 }
1468 
1469 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1470 /*
1471  *	mptctl_eventquery - Query the host adapter for the event types
1472  *	that are being logged.
1473  *	@arg: User space argument
1474  *
1475  * Outputs:	None.
1476  * Return:	0 if successful
1477  *		-EFAULT if data unavailable
1478  *		-ENODEV  if no such device/adapter
1479  */
1480 static int
1481 mptctl_eventquery (unsigned long arg)
1482 {
1483 	struct mpt_ioctl_eventquery __user *uarg = (void __user *) arg;
1484 	struct mpt_ioctl_eventquery	 karg;
1485 	MPT_ADAPTER *ioc;
1486 	int iocnum;
1487 
1488 	dctlprintk(("mptctl_eventquery called.\n"));
1489 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventquery))) {
1490 		printk(KERN_ERR "%s@%d::mptctl_eventquery - "
1491 			"Unable to read in mpt_ioctl_eventquery struct @ %p\n",
1492 				__FILE__, __LINE__, uarg);
1493 		return -EFAULT;
1494 	}
1495 
1496 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1497 	    (ioc == NULL)) {
1498 		dctlprintk((KERN_ERR "%s::mptctl_eventquery() @%d - ioc%d not found!\n",
1499 				__FILE__, __LINE__, iocnum));
1500 		return -ENODEV;
1501 	}
1502 
1503 	karg.eventEntries = ioc->eventLogSize;
1504 	karg.eventTypes = ioc->eventTypes;
1505 
1506 	/* Copy the data from kernel memory to user memory
1507 	 */
1508 	if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_eventquery))) {
1509 		printk(KERN_ERR "%s@%d::mptctl_eventquery - "
1510 			"Unable to write out mpt_ioctl_eventquery struct @ %p\n",
1511 				__FILE__, __LINE__, uarg);
1512 		return -EFAULT;
1513 	}
1514 	return 0;
1515 }
1516 
1517 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1518 static int
1519 mptctl_eventenable (unsigned long arg)
1520 {
1521 	struct mpt_ioctl_eventenable __user *uarg = (void __user *) arg;
1522 	struct mpt_ioctl_eventenable	 karg;
1523 	MPT_ADAPTER *ioc;
1524 	int iocnum;
1525 
1526 	dctlprintk(("mptctl_eventenable called.\n"));
1527 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventenable))) {
1528 		printk(KERN_ERR "%s@%d::mptctl_eventenable - "
1529 			"Unable to read in mpt_ioctl_eventenable struct @ %p\n",
1530 				__FILE__, __LINE__, uarg);
1531 		return -EFAULT;
1532 	}
1533 
1534 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1535 	    (ioc == NULL)) {
1536 		dctlprintk((KERN_ERR "%s::mptctl_eventenable() @%d - ioc%d not found!\n",
1537 				__FILE__, __LINE__, iocnum));
1538 		return -ENODEV;
1539 	}
1540 
1541 	if (ioc->events == NULL) {
1542 		/* Have not yet allocated memory - do so now.
1543 		 */
1544 		int sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
1545 		ioc->events = kmalloc(sz, GFP_KERNEL);
1546 		if (ioc->events == NULL) {
1547 			printk(KERN_ERR MYNAM ": ERROR - Insufficient memory to add adapter!\n");
1548 			return -ENOMEM;
1549 		}
1550 		memset(ioc->events, 0, sz);
1551 		ioc->alloc_total += sz;
1552 
1553 		ioc->eventLogSize = MPTCTL_EVENT_LOG_SIZE;
1554 		ioc->eventContext = 0;
1555         }
1556 
1557 	/* Update the IOC event logging flag.
1558 	 */
1559 	ioc->eventTypes = karg.eventTypes;
1560 
1561 	return 0;
1562 }
1563 
1564 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1565 static int
1566 mptctl_eventreport (unsigned long arg)
1567 {
1568 	struct mpt_ioctl_eventreport __user *uarg = (void __user *) arg;
1569 	struct mpt_ioctl_eventreport	 karg;
1570 	MPT_ADAPTER		 *ioc;
1571 	int			 iocnum;
1572 	int			 numBytes, maxEvents, max;
1573 
1574 	dctlprintk(("mptctl_eventreport called.\n"));
1575 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventreport))) {
1576 		printk(KERN_ERR "%s@%d::mptctl_eventreport - "
1577 			"Unable to read in mpt_ioctl_eventreport struct @ %p\n",
1578 				__FILE__, __LINE__, uarg);
1579 		return -EFAULT;
1580 	}
1581 
1582 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1583 	    (ioc == NULL)) {
1584 		dctlprintk((KERN_ERR "%s::mptctl_eventreport() @%d - ioc%d not found!\n",
1585 				__FILE__, __LINE__, iocnum));
1586 		return -ENODEV;
1587 	}
1588 
1589 	numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1590 	maxEvents = numBytes/sizeof(MPT_IOCTL_EVENTS);
1591 
1592 
1593 	max = ioc->eventLogSize < maxEvents ? ioc->eventLogSize : maxEvents;
1594 
1595 	/* If fewer than 1 event is requested, there must have
1596 	 * been some type of error.
1597 	 */
1598 	if ((max < 1) || !ioc->events)
1599 		return -ENODATA;
1600 
1601 	/* Copy the data from kernel memory to user memory
1602 	 */
1603 	numBytes = max * sizeof(MPT_IOCTL_EVENTS);
1604 	if (copy_to_user(uarg->eventData, ioc->events, numBytes)) {
1605 		printk(KERN_ERR "%s@%d::mptctl_eventreport - "
1606 			"Unable to write out mpt_ioctl_eventreport struct @ %p\n",
1607 				__FILE__, __LINE__, ioc->events);
1608 		return -EFAULT;
1609 	}
1610 
1611 	return 0;
1612 }
1613 
1614 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1615 static int
1616 mptctl_replace_fw (unsigned long arg)
1617 {
1618 	struct mpt_ioctl_replace_fw __user *uarg = (void __user *) arg;
1619 	struct mpt_ioctl_replace_fw	 karg;
1620 	MPT_ADAPTER		 *ioc;
1621 	int			 iocnum;
1622 	int			 newFwSize;
1623 
1624 	dctlprintk(("mptctl_replace_fw called.\n"));
1625 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_replace_fw))) {
1626 		printk(KERN_ERR "%s@%d::mptctl_replace_fw - "
1627 			"Unable to read in mpt_ioctl_replace_fw struct @ %p\n",
1628 				__FILE__, __LINE__, uarg);
1629 		return -EFAULT;
1630 	}
1631 
1632 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1633 	    (ioc == NULL)) {
1634 		dctlprintk((KERN_ERR "%s::mptctl_replace_fw() @%d - ioc%d not found!\n",
1635 				__FILE__, __LINE__, iocnum));
1636 		return -ENODEV;
1637 	}
1638 
1639 	/* If caching FW, Free the old FW image
1640 	 */
1641 	if (ioc->cached_fw == NULL)
1642 		return 0;
1643 
1644 	mpt_free_fw_memory(ioc);
1645 
1646 	/* Allocate memory for the new FW image
1647 	 */
1648 	newFwSize = karg.newImageSize;
1649 
1650 	if (newFwSize & 0x01)
1651 		newFwSize += 1;
1652 	if (newFwSize & 0x02)
1653 		newFwSize += 2;
1654 
1655 	mpt_alloc_fw_memory(ioc, newFwSize);
1656 	if (ioc->cached_fw == NULL)
1657 		return -ENOMEM;
1658 
1659 	/* Copy the data from user memory to kernel space
1660 	 */
1661 	if (copy_from_user(ioc->cached_fw, uarg->newImage, newFwSize)) {
1662 		printk(KERN_ERR "%s@%d::mptctl_replace_fw - "
1663 				"Unable to read in mpt_ioctl_replace_fw image "
1664 				"@ %p\n", __FILE__, __LINE__, uarg);
1665 		mpt_free_fw_memory(ioc);
1666 		return -EFAULT;
1667 	}
1668 
1669 	/* Update IOCFactsReply
1670 	 */
1671 	ioc->facts.FWImageSize = newFwSize;
1672 	return 0;
1673 }
1674 
1675 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1676 /* MPT IOCTL MPTCOMMAND function.
1677  * Cast the arg into the mpt_ioctl_mpt_command structure.
1678  *
1679  * Outputs:	None.
1680  * Return:	0 if successful
1681  *		-EBUSY  if previous command timout and IOC reset is not complete.
1682  *		-EFAULT if data unavailable
1683  *		-ENODEV if no such device/adapter
1684  *		-ETIME	if timer expires
1685  *		-ENOMEM if memory allocation error
1686  */
1687 static int
1688 mptctl_mpt_command (unsigned long arg)
1689 {
1690 	struct mpt_ioctl_command __user *uarg = (void __user *) arg;
1691 	struct mpt_ioctl_command  karg;
1692 	MPT_ADAPTER	*ioc;
1693 	int		iocnum;
1694 	int		rc;
1695 
1696 	dctlprintk(("mptctl_command called.\n"));
1697 
1698 	if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_command))) {
1699 		printk(KERN_ERR "%s@%d::mptctl_mpt_command - "
1700 			"Unable to read in mpt_ioctl_command struct @ %p\n",
1701 				__FILE__, __LINE__, uarg);
1702 		return -EFAULT;
1703 	}
1704 
1705 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1706 	    (ioc == NULL)) {
1707 		dctlprintk((KERN_ERR "%s::mptctl_mpt_command() @%d - ioc%d not found!\n",
1708 				__FILE__, __LINE__, iocnum));
1709 		return -ENODEV;
1710 	}
1711 
1712 	rc = mptctl_do_mpt_command (karg, &uarg->MF);
1713 
1714 	return rc;
1715 }
1716 
1717 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1718 /* Worker routine for the IOCTL MPTCOMMAND and MPTCOMMAND32 (sparc) commands.
1719  *
1720  * Outputs:	None.
1721  * Return:	0 if successful
1722  *		-EBUSY  if previous command timout and IOC reset is not complete.
1723  *		-EFAULT if data unavailable
1724  *		-ENODEV if no such device/adapter
1725  *		-ETIME	if timer expires
1726  *		-ENOMEM if memory allocation error
1727  *		-EPERM if SCSI I/O and target is untagged
1728  */
1729 static int
1730 mptctl_do_mpt_command (struct mpt_ioctl_command karg, void __user *mfPtr)
1731 {
1732 	MPT_ADAPTER	*ioc;
1733 	MPT_FRAME_HDR	*mf = NULL;
1734 	MPIHeader_t	*hdr;
1735 	char		*psge;
1736 	struct buflist	bufIn;	/* data In buffer */
1737 	struct buflist	bufOut; /* data Out buffer */
1738 	dma_addr_t	dma_addr_in;
1739 	dma_addr_t	dma_addr_out;
1740 	int		sgSize = 0;	/* Num SG elements */
1741 	int		iocnum, flagsLength;
1742 	int		sz, rc = 0;
1743 	int		msgContext;
1744 	u16		req_idx;
1745 	ulong 		timeout;
1746 
1747 	dctlprintk(("mptctl_do_mpt_command called.\n"));
1748 	bufIn.kptr = bufOut.kptr = NULL;
1749 
1750 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1751 	    (ioc == NULL)) {
1752 		dctlprintk((KERN_ERR "%s::mptctl_do_mpt_command() @%d - ioc%d not found!\n",
1753 				__FILE__, __LINE__, iocnum));
1754 		return -ENODEV;
1755 	}
1756 	if (!ioc->ioctl) {
1757 		printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1758 			"No memory available during driver init.\n",
1759 				__FILE__, __LINE__);
1760 		return -ENOMEM;
1761 	} else if (ioc->ioctl->status & MPT_IOCTL_STATUS_DID_IOCRESET) {
1762 		printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1763 			"Busy with IOC Reset \n", __FILE__, __LINE__);
1764 		return -EBUSY;
1765 	}
1766 
1767 	/* Verify that the final request frame will not be too large.
1768 	 */
1769 	sz = karg.dataSgeOffset * 4;
1770 	if (karg.dataInSize > 0)
1771 		sz += sizeof(dma_addr_t) + sizeof(u32);
1772 	if (karg.dataOutSize > 0)
1773 		sz += sizeof(dma_addr_t) + sizeof(u32);
1774 
1775 	if (sz > ioc->req_sz) {
1776 		printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1777 			"Request frame too large (%d) maximum (%d)\n",
1778 				__FILE__, __LINE__, sz, ioc->req_sz);
1779 		return -EFAULT;
1780 	}
1781 
1782 	/* Get a free request frame and save the message context.
1783 	 */
1784         if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL)
1785                 return -EAGAIN;
1786 
1787 	hdr = (MPIHeader_t *) mf;
1788 	msgContext = le32_to_cpu(hdr->MsgContext);
1789 	req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
1790 
1791 	/* Copy the request frame
1792 	 * Reset the saved message context.
1793 	 * Request frame in user space
1794 	 */
1795 	if (copy_from_user(mf, mfPtr, karg.dataSgeOffset * 4)) {
1796 		printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1797 			"Unable to read MF from mpt_ioctl_command struct @ %p\n",
1798 			__FILE__, __LINE__, mfPtr);
1799 		rc = -EFAULT;
1800 		goto done_free_mem;
1801 	}
1802 	hdr->MsgContext = cpu_to_le32(msgContext);
1803 
1804 
1805 	/* Verify that this request is allowed.
1806 	 */
1807 	switch (hdr->Function) {
1808 	case MPI_FUNCTION_IOC_FACTS:
1809 	case MPI_FUNCTION_PORT_FACTS:
1810 		karg.dataOutSize  = karg.dataInSize = 0;
1811 		break;
1812 
1813 	case MPI_FUNCTION_CONFIG:
1814 	case MPI_FUNCTION_FC_COMMON_TRANSPORT_SEND:
1815 	case MPI_FUNCTION_FC_EX_LINK_SRVC_SEND:
1816 	case MPI_FUNCTION_FW_UPLOAD:
1817 	case MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
1818 	case MPI_FUNCTION_FW_DOWNLOAD:
1819 	case MPI_FUNCTION_FC_PRIMITIVE_SEND:
1820 		break;
1821 
1822 	case MPI_FUNCTION_SCSI_IO_REQUEST:
1823 		if (ioc->sh) {
1824 			SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
1825 			VirtTarget	*pTarget = NULL;
1826 			MPT_SCSI_HOST	*hd = NULL;
1827 			int qtag = MPI_SCSIIO_CONTROL_UNTAGGED;
1828 			int scsidir = 0;
1829 			int target = (int) pScsiReq->TargetID;
1830 			int dataSize;
1831 
1832 			if ((target < 0) || (target >= ioc->sh->max_id)) {
1833 				printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1834 					"Target ID out of bounds. \n",
1835 					__FILE__, __LINE__);
1836 				rc = -ENODEV;
1837 				goto done_free_mem;
1838 			}
1839 
1840 			pScsiReq->MsgFlags = mpt_msg_flags();
1841 
1842 			/* verify that app has not requested
1843 			 *	more sense data than driver
1844 			 *	can provide, if so, reset this parameter
1845 			 * set the sense buffer pointer low address
1846 			 * update the control field to specify Q type
1847 			 */
1848 			if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1849 				pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1850 			else
1851 				pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1852 
1853 			pScsiReq->SenseBufferLowAddr =
1854 				cpu_to_le32(ioc->sense_buf_low_dma
1855 				   + (req_idx * MPT_SENSE_BUFFER_ALLOC));
1856 
1857 			if ((hd = (MPT_SCSI_HOST *) ioc->sh->hostdata)) {
1858 				if (hd->Targets)
1859 					pTarget = hd->Targets[target];
1860 			}
1861 
1862 			if (pTarget &&(pTarget->tflags & MPT_TARGET_FLAGS_Q_YES))
1863 				qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1864 
1865 			/* Have the IOCTL driver set the direction based
1866 			 * on the dataOutSize (ordering issue with Sparc).
1867 			 */
1868 			if (karg.dataOutSize > 0) {
1869 				scsidir = MPI_SCSIIO_CONTROL_WRITE;
1870 				dataSize = karg.dataOutSize;
1871 			} else {
1872 				scsidir = MPI_SCSIIO_CONTROL_READ;
1873 				dataSize = karg.dataInSize;
1874 			}
1875 
1876 			pScsiReq->Control = cpu_to_le32(scsidir | qtag);
1877 			pScsiReq->DataLength = cpu_to_le32(dataSize);
1878 
1879 			ioc->ioctl->reset = MPTCTL_RESET_OK;
1880 			ioc->ioctl->target = target;
1881 
1882 		} else {
1883 			printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1884 				"SCSI driver is not loaded. \n",
1885 					__FILE__, __LINE__);
1886 			rc = -EFAULT;
1887 			goto done_free_mem;
1888 		}
1889 		break;
1890 
1891 	case MPI_FUNCTION_RAID_ACTION:
1892 		/* Just add a SGE
1893 		 */
1894 		break;
1895 
1896 	case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1897 		if (ioc->sh) {
1898 			SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
1899 			int qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1900 			int scsidir = MPI_SCSIIO_CONTROL_READ;
1901 			int dataSize;
1902 
1903 			pScsiReq->MsgFlags = mpt_msg_flags();
1904 
1905 			/* verify that app has not requested
1906 			 *	more sense data than driver
1907 			 *	can provide, if so, reset this parameter
1908 			 * set the sense buffer pointer low address
1909 			 * update the control field to specify Q type
1910 			 */
1911 			if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1912 				pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1913 			else
1914 				pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1915 
1916 			pScsiReq->SenseBufferLowAddr =
1917 				cpu_to_le32(ioc->sense_buf_low_dma
1918 				   + (req_idx * MPT_SENSE_BUFFER_ALLOC));
1919 
1920 			/* All commands to physical devices are tagged
1921 			 */
1922 
1923 			/* Have the IOCTL driver set the direction based
1924 			 * on the dataOutSize (ordering issue with Sparc).
1925 			 */
1926 			if (karg.dataOutSize > 0) {
1927 				scsidir = MPI_SCSIIO_CONTROL_WRITE;
1928 				dataSize = karg.dataOutSize;
1929 			} else {
1930 				scsidir = MPI_SCSIIO_CONTROL_READ;
1931 				dataSize = karg.dataInSize;
1932 			}
1933 
1934 			pScsiReq->Control = cpu_to_le32(scsidir | qtag);
1935 			pScsiReq->DataLength = cpu_to_le32(dataSize);
1936 
1937 			ioc->ioctl->reset = MPTCTL_RESET_OK;
1938 			ioc->ioctl->target = pScsiReq->TargetID;
1939 		} else {
1940 			printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1941 				"SCSI driver is not loaded. \n",
1942 					__FILE__, __LINE__);
1943 			rc = -EFAULT;
1944 			goto done_free_mem;
1945 		}
1946 		break;
1947 
1948 	case MPI_FUNCTION_SCSI_TASK_MGMT:
1949 		{
1950 			MPT_SCSI_HOST *hd = NULL;
1951 			if ((ioc->sh == NULL) || ((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL)) {
1952 				printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1953 					"SCSI driver not loaded or SCSI host not found. \n",
1954 					__FILE__, __LINE__);
1955 				rc = -EFAULT;
1956 				goto done_free_mem;
1957 			} else if (mptctl_set_tm_flags(hd) != 0) {
1958 				rc = -EPERM;
1959 				goto done_free_mem;
1960 			}
1961 		}
1962 		break;
1963 
1964 	case MPI_FUNCTION_IOC_INIT:
1965 		{
1966 			IOCInit_t	*pInit = (IOCInit_t *) mf;
1967 			u32		high_addr, sense_high;
1968 
1969 			/* Verify that all entries in the IOC INIT match
1970 			 * existing setup (and in LE format).
1971 			 */
1972 			if (sizeof(dma_addr_t) == sizeof(u64)) {
1973 				high_addr = cpu_to_le32((u32)((u64)ioc->req_frames_dma >> 32));
1974 				sense_high= cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
1975 			} else {
1976 				high_addr = 0;
1977 				sense_high= 0;
1978 			}
1979 
1980 			if ((pInit->Flags != 0) || (pInit->MaxDevices != ioc->facts.MaxDevices) ||
1981 				(pInit->MaxBuses != ioc->facts.MaxBuses) ||
1982 				(pInit->ReplyFrameSize != cpu_to_le16(ioc->reply_sz)) ||
1983 				(pInit->HostMfaHighAddr != high_addr) ||
1984 				(pInit->SenseBufferHighAddr != sense_high)) {
1985 				printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1986 					"IOC_INIT issued with 1 or more incorrect parameters. Rejected.\n",
1987 					__FILE__, __LINE__);
1988 				rc = -EFAULT;
1989 				goto done_free_mem;
1990 			}
1991 		}
1992 		break;
1993 	default:
1994 		/*
1995 		 * MPI_FUNCTION_PORT_ENABLE
1996 		 * MPI_FUNCTION_TARGET_CMD_BUFFER_POST
1997 		 * MPI_FUNCTION_TARGET_ASSIST
1998 		 * MPI_FUNCTION_TARGET_STATUS_SEND
1999 		 * MPI_FUNCTION_TARGET_MODE_ABORT
2000 		 * MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET
2001 		 * MPI_FUNCTION_IO_UNIT_RESET
2002 		 * MPI_FUNCTION_HANDSHAKE
2003 		 * MPI_FUNCTION_REPLY_FRAME_REMOVAL
2004 		 * MPI_FUNCTION_EVENT_NOTIFICATION
2005 		 *  (driver handles event notification)
2006 		 * MPI_FUNCTION_EVENT_ACK
2007 		 */
2008 
2009 		/*  What to do with these???  CHECK ME!!!
2010 			MPI_FUNCTION_FC_LINK_SRVC_BUF_POST
2011 			MPI_FUNCTION_FC_LINK_SRVC_RSP
2012 			MPI_FUNCTION_FC_ABORT
2013 			MPI_FUNCTION_LAN_SEND
2014 			MPI_FUNCTION_LAN_RECEIVE
2015 		 	MPI_FUNCTION_LAN_RESET
2016 		*/
2017 
2018 		printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2019 			"Illegal request (function 0x%x) \n",
2020 			__FILE__, __LINE__, hdr->Function);
2021 		rc = -EFAULT;
2022 		goto done_free_mem;
2023 	}
2024 
2025 	/* Add the SGL ( at most one data in SGE and one data out SGE )
2026 	 * In the case of two SGE's - the data out (write) will always
2027 	 * preceede the data in (read) SGE. psgList is used to free the
2028 	 * allocated memory.
2029 	 */
2030 	psge = (char *) (((int *) mf) + karg.dataSgeOffset);
2031 	flagsLength = 0;
2032 
2033 	/* bufIn and bufOut are used for user to kernel space transfers
2034 	 */
2035 	bufIn.kptr = bufOut.kptr = NULL;
2036 	bufIn.len = bufOut.len = 0;
2037 
2038 	if (karg.dataOutSize > 0)
2039 		sgSize ++;
2040 
2041 	if (karg.dataInSize > 0)
2042 		sgSize ++;
2043 
2044 	if (sgSize > 0) {
2045 
2046 		/* Set up the dataOut memory allocation */
2047 		if (karg.dataOutSize > 0) {
2048 			if (karg.dataInSize > 0) {
2049 				flagsLength = ( MPI_SGE_FLAGS_SIMPLE_ELEMENT |
2050 						MPI_SGE_FLAGS_END_OF_BUFFER |
2051 						MPI_SGE_FLAGS_DIRECTION |
2052 						mpt_addr_size() )
2053 						<< MPI_SGE_FLAGS_SHIFT;
2054 			} else {
2055 				flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE;
2056 			}
2057 			flagsLength |= karg.dataOutSize;
2058 			bufOut.len = karg.dataOutSize;
2059 			bufOut.kptr = pci_alloc_consistent(
2060 					ioc->pcidev, bufOut.len, &dma_addr_out);
2061 
2062 			if (bufOut.kptr == NULL) {
2063 				rc = -ENOMEM;
2064 				goto done_free_mem;
2065 			} else {
2066 				/* Set up this SGE.
2067 				 * Copy to MF and to sglbuf
2068 				 */
2069 				mpt_add_sge(psge, flagsLength, dma_addr_out);
2070 				psge += (sizeof(u32) + sizeof(dma_addr_t));
2071 
2072 				/* Copy user data to kernel space.
2073 				 */
2074 				if (copy_from_user(bufOut.kptr,
2075 						karg.dataOutBufPtr,
2076 						bufOut.len)) {
2077 					printk(KERN_ERR
2078 						"%s@%d::mptctl_do_mpt_command - Unable "
2079 						"to read user data "
2080 						"struct @ %p\n",
2081 						__FILE__, __LINE__,karg.dataOutBufPtr);
2082 					rc =  -EFAULT;
2083 					goto done_free_mem;
2084 				}
2085 			}
2086 		}
2087 
2088 		if (karg.dataInSize > 0) {
2089 			flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
2090 			flagsLength |= karg.dataInSize;
2091 
2092 			bufIn.len = karg.dataInSize;
2093 			bufIn.kptr = pci_alloc_consistent(ioc->pcidev,
2094 					bufIn.len, &dma_addr_in);
2095 
2096 			if (bufIn.kptr == NULL) {
2097 				rc = -ENOMEM;
2098 				goto done_free_mem;
2099 			} else {
2100 				/* Set up this SGE
2101 				 * Copy to MF and to sglbuf
2102 				 */
2103 				mpt_add_sge(psge, flagsLength, dma_addr_in);
2104 			}
2105 		}
2106 	} else  {
2107 		/* Add a NULL SGE
2108 		 */
2109 		mpt_add_sge(psge, flagsLength, (dma_addr_t) -1);
2110 	}
2111 
2112 	ioc->ioctl->wait_done = 0;
2113 	if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT) {
2114 
2115 		DBG_DUMP_TM_REQUEST_FRAME((u32 *)mf);
2116 
2117 		if (mpt_send_handshake_request(mptctl_id, ioc,
2118 			sizeof(SCSITaskMgmt_t), (u32*)mf,
2119 			CAN_SLEEP) != 0) {
2120 			dfailprintk((MYIOC_s_ERR_FMT "_send_handshake FAILED!"
2121 				" (ioc %p, mf %p) \n", ioc->name,
2122 				ioc, mf));
2123 			mptctl_free_tm_flags(ioc);
2124 			rc = -ENODATA;
2125 			goto done_free_mem;
2126 		}
2127 
2128 	} else
2129 		mpt_put_msg_frame(mptctl_id, ioc, mf);
2130 
2131 	/* Now wait for the command to complete */
2132 	timeout = (karg.timeout > 0) ? karg.timeout : MPT_IOCTL_DEFAULT_TIMEOUT;
2133 	timeout = wait_event_interruptible_timeout(mptctl_wait,
2134 	     ioc->ioctl->wait_done == 1,
2135 	     HZ*timeout);
2136 
2137 	if(timeout <=0 && (ioc->ioctl->wait_done != 1 )) {
2138 	/* Now we need to reset the board */
2139 
2140 		if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT)
2141 			mptctl_free_tm_flags(ioc);
2142 
2143 		mptctl_timeout_expired(ioc->ioctl);
2144 		rc = -ENODATA;
2145 		goto done_free_mem;
2146 	}
2147 
2148 	mf = NULL;
2149 
2150 	/* If a valid reply frame, copy to the user.
2151 	 * Offset 2: reply length in U32's
2152 	 */
2153 	if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID) {
2154 		if (karg.maxReplyBytes < ioc->reply_sz) {
2155 			 sz = min(karg.maxReplyBytes, 4*ioc->ioctl->ReplyFrame[2]);
2156 		} else {
2157 			 sz = min(ioc->reply_sz, 4*ioc->ioctl->ReplyFrame[2]);
2158 		}
2159 
2160 		if (sz > 0) {
2161 			if (copy_to_user(karg.replyFrameBufPtr,
2162 				 &ioc->ioctl->ReplyFrame, sz)){
2163 				 printk(KERN_ERR
2164 				     "%s@%d::mptctl_do_mpt_command - "
2165 				 "Unable to write out reply frame %p\n",
2166 				 __FILE__, __LINE__, karg.replyFrameBufPtr);
2167 				 rc =  -ENODATA;
2168 				 goto done_free_mem;
2169 			}
2170 		}
2171 	}
2172 
2173 	/* If valid sense data, copy to user.
2174 	 */
2175 	if (ioc->ioctl->status & MPT_IOCTL_STATUS_SENSE_VALID) {
2176 		sz = min(karg.maxSenseBytes, MPT_SENSE_BUFFER_SIZE);
2177 		if (sz > 0) {
2178 			if (copy_to_user(karg.senseDataPtr, ioc->ioctl->sense, sz)) {
2179 				printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2180 				"Unable to write sense data to user %p\n",
2181 				__FILE__, __LINE__,
2182 				karg.senseDataPtr);
2183 				rc =  -ENODATA;
2184 				goto done_free_mem;
2185 			}
2186 		}
2187 	}
2188 
2189 	/* If the overall status is _GOOD and data in, copy data
2190 	 * to user.
2191 	 */
2192 	if ((ioc->ioctl->status & MPT_IOCTL_STATUS_COMMAND_GOOD) &&
2193 				(karg.dataInSize > 0) && (bufIn.kptr)) {
2194 
2195 		if (copy_to_user(karg.dataInBufPtr,
2196 				 bufIn.kptr, karg.dataInSize)) {
2197 			printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2198 				"Unable to write data to user %p\n",
2199 				__FILE__, __LINE__,
2200 				karg.dataInBufPtr);
2201 			rc =  -ENODATA;
2202 		}
2203 	}
2204 
2205 done_free_mem:
2206 
2207 	ioc->ioctl->status &= ~(MPT_IOCTL_STATUS_COMMAND_GOOD |
2208 		MPT_IOCTL_STATUS_SENSE_VALID |
2209 		MPT_IOCTL_STATUS_RF_VALID );
2210 
2211 	/* Free the allocated memory.
2212 	 */
2213 	if (bufOut.kptr != NULL) {
2214 		pci_free_consistent(ioc->pcidev,
2215 			bufOut.len, (void *) bufOut.kptr, dma_addr_out);
2216 	}
2217 
2218 	if (bufIn.kptr != NULL) {
2219 		pci_free_consistent(ioc->pcidev,
2220 			bufIn.len, (void *) bufIn.kptr, dma_addr_in);
2221 	}
2222 
2223 	/* mf is null if command issued successfully
2224 	 * otherwise, failure occured after mf acquired.
2225 	 */
2226 	if (mf)
2227 		mpt_free_msg_frame(ioc, mf);
2228 
2229 	return rc;
2230 }
2231 
2232 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2233 /* Prototype Routine for the HP HOST INFO command.
2234  *
2235  * Outputs:	None.
2236  * Return:	0 if successful
2237  *		-EFAULT if data unavailable
2238  *		-EBUSY  if previous command timout and IOC reset is not complete.
2239  *		-ENODEV if no such device/adapter
2240  *		-ETIME	if timer expires
2241  *		-ENOMEM if memory allocation error
2242  */
2243 static int
2244 mptctl_hp_hostinfo(unsigned long arg, unsigned int data_size)
2245 {
2246 	hp_host_info_t	__user *uarg = (void __user *) arg;
2247 	MPT_ADAPTER		*ioc;
2248 	struct pci_dev		*pdev;
2249 	char			*pbuf;
2250 	dma_addr_t		buf_dma;
2251 	hp_host_info_t		karg;
2252 	CONFIGPARMS		cfg;
2253 	ConfigPageHeader_t	hdr;
2254 	int			iocnum;
2255 	int			rc, cim_rev;
2256 
2257 	dctlprintk((": mptctl_hp_hostinfo called.\n"));
2258 	/* Reset long to int. Should affect IA64 and SPARC only
2259 	 */
2260 	if (data_size == sizeof(hp_host_info_t))
2261 		cim_rev = 1;
2262 	else if (data_size == sizeof(hp_host_info_rev0_t))
2263 		cim_rev = 0;	/* obsolete */
2264 	else
2265 		return -EFAULT;
2266 
2267 	if (copy_from_user(&karg, uarg, sizeof(hp_host_info_t))) {
2268 		printk(KERN_ERR "%s@%d::mptctl_hp_host_info - "
2269 			"Unable to read in hp_host_info struct @ %p\n",
2270 				__FILE__, __LINE__, uarg);
2271 		return -EFAULT;
2272 	}
2273 
2274 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2275 	    (ioc == NULL)) {
2276 		dctlprintk((KERN_ERR "%s::mptctl_hp_hostinfo() @%d - ioc%d not found!\n",
2277 				__FILE__, __LINE__, iocnum));
2278 		return -ENODEV;
2279 	}
2280 
2281 	/* Fill in the data and return the structure to the calling
2282 	 * program
2283 	 */
2284 	pdev = (struct pci_dev *) ioc->pcidev;
2285 
2286 	karg.vendor = pdev->vendor;
2287 	karg.device = pdev->device;
2288 	karg.subsystem_id = pdev->subsystem_device;
2289 	karg.subsystem_vendor = pdev->subsystem_vendor;
2290 	karg.devfn = pdev->devfn;
2291 	karg.bus = pdev->bus->number;
2292 
2293 	/* Save the SCSI host no. if
2294 	 * SCSI driver loaded
2295 	 */
2296 	if (ioc->sh != NULL)
2297 		karg.host_no = ioc->sh->host_no;
2298 	else
2299 		karg.host_no =  -1;
2300 
2301 	/* Reformat the fw_version into a string
2302 	 */
2303 	karg.fw_version[0] = ioc->facts.FWVersion.Struct.Major >= 10 ?
2304 		((ioc->facts.FWVersion.Struct.Major / 10) + '0') : '0';
2305 	karg.fw_version[1] = (ioc->facts.FWVersion.Struct.Major % 10 ) + '0';
2306 	karg.fw_version[2] = '.';
2307 	karg.fw_version[3] = ioc->facts.FWVersion.Struct.Minor >= 10 ?
2308 		((ioc->facts.FWVersion.Struct.Minor / 10) + '0') : '0';
2309 	karg.fw_version[4] = (ioc->facts.FWVersion.Struct.Minor % 10 ) + '0';
2310 	karg.fw_version[5] = '.';
2311 	karg.fw_version[6] = ioc->facts.FWVersion.Struct.Unit >= 10 ?
2312 		((ioc->facts.FWVersion.Struct.Unit / 10) + '0') : '0';
2313 	karg.fw_version[7] = (ioc->facts.FWVersion.Struct.Unit % 10 ) + '0';
2314 	karg.fw_version[8] = '.';
2315 	karg.fw_version[9] = ioc->facts.FWVersion.Struct.Dev >= 10 ?
2316 		((ioc->facts.FWVersion.Struct.Dev / 10) + '0') : '0';
2317 	karg.fw_version[10] = (ioc->facts.FWVersion.Struct.Dev % 10 ) + '0';
2318 	karg.fw_version[11] = '\0';
2319 
2320 	/* Issue a config request to get the device serial number
2321 	 */
2322 	hdr.PageVersion = 0;
2323 	hdr.PageLength = 0;
2324 	hdr.PageNumber = 0;
2325 	hdr.PageType = MPI_CONFIG_PAGETYPE_MANUFACTURING;
2326 	cfg.cfghdr.hdr = &hdr;
2327 	cfg.physAddr = -1;
2328 	cfg.pageAddr = 0;
2329 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2330 	cfg.dir = 0;	/* read */
2331 	cfg.timeout = 10;
2332 
2333 	strncpy(karg.serial_number, " ", 24);
2334 	if (mpt_config(ioc, &cfg) == 0) {
2335 		if (cfg.cfghdr.hdr->PageLength > 0) {
2336 			/* Issue the second config page request */
2337 			cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2338 
2339 			pbuf = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4, &buf_dma);
2340 			if (pbuf) {
2341 				cfg.physAddr = buf_dma;
2342 				if (mpt_config(ioc, &cfg) == 0) {
2343 					ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;
2344 					if (strlen(pdata->BoardTracerNumber) > 1) {
2345 						strncpy(karg.serial_number, 									    pdata->BoardTracerNumber, 24);
2346 						karg.serial_number[24-1]='\0';
2347 					}
2348 				}
2349 				pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, pbuf, buf_dma);
2350 				pbuf = NULL;
2351 			}
2352 		}
2353 	}
2354 	rc = mpt_GetIocState(ioc, 1);
2355 	switch (rc) {
2356 	case MPI_IOC_STATE_OPERATIONAL:
2357 		karg.ioc_status =  HP_STATUS_OK;
2358 		break;
2359 
2360 	case MPI_IOC_STATE_FAULT:
2361 		karg.ioc_status =  HP_STATUS_FAILED;
2362 		break;
2363 
2364 	case MPI_IOC_STATE_RESET:
2365 	case MPI_IOC_STATE_READY:
2366 	default:
2367 		karg.ioc_status =  HP_STATUS_OTHER;
2368 		break;
2369 	}
2370 
2371 	karg.base_io_addr = pci_resource_start(pdev, 0);
2372 
2373 	if (ioc->bus_type == FC)
2374 		karg.bus_phys_width = HP_BUS_WIDTH_UNK;
2375 	else
2376 		karg.bus_phys_width = HP_BUS_WIDTH_16;
2377 
2378 	karg.hard_resets = 0;
2379 	karg.soft_resets = 0;
2380 	karg.timeouts = 0;
2381 	if (ioc->sh != NULL) {
2382 		MPT_SCSI_HOST *hd =  (MPT_SCSI_HOST *)ioc->sh->hostdata;
2383 
2384 		if (hd && (cim_rev == 1)) {
2385 			karg.hard_resets = hd->hard_resets;
2386 			karg.soft_resets = hd->soft_resets;
2387 			karg.timeouts = hd->timeouts;
2388 		}
2389 	}
2390 
2391 	cfg.pageAddr = 0;
2392 	cfg.action = MPI_TOOLBOX_ISTWI_READ_WRITE_TOOL;
2393 	cfg.dir = MPI_TB_ISTWI_FLAGS_READ;
2394 	cfg.timeout = 10;
2395 	pbuf = pci_alloc_consistent(ioc->pcidev, 4, &buf_dma);
2396 	if (pbuf) {
2397 		cfg.physAddr = buf_dma;
2398 		if ((mpt_toolbox(ioc, &cfg)) == 0) {
2399 			karg.rsvd = *(u32 *)pbuf;
2400 		}
2401 		pci_free_consistent(ioc->pcidev, 4, pbuf, buf_dma);
2402 		pbuf = NULL;
2403 	}
2404 
2405 	/* Copy the data from kernel memory to user memory
2406 	 */
2407 	if (copy_to_user((char __user *)arg, &karg, sizeof(hp_host_info_t))) {
2408 		printk(KERN_ERR "%s@%d::mptctl_hpgethostinfo - "
2409 			"Unable to write out hp_host_info @ %p\n",
2410 				__FILE__, __LINE__, uarg);
2411 		return -EFAULT;
2412 	}
2413 
2414 	return 0;
2415 
2416 }
2417 
2418 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2419 /* Prototype Routine for the HP TARGET INFO command.
2420  *
2421  * Outputs:	None.
2422  * Return:	0 if successful
2423  *		-EFAULT if data unavailable
2424  *		-EBUSY  if previous command timout and IOC reset is not complete.
2425  *		-ENODEV if no such device/adapter
2426  *		-ETIME	if timer expires
2427  *		-ENOMEM if memory allocation error
2428  */
2429 static int
2430 mptctl_hp_targetinfo(unsigned long arg)
2431 {
2432 	hp_target_info_t __user *uarg = (void __user *) arg;
2433 	SCSIDevicePage0_t	*pg0_alloc;
2434 	SCSIDevicePage3_t	*pg3_alloc;
2435 	MPT_ADAPTER		*ioc;
2436 	MPT_SCSI_HOST 		*hd = NULL;
2437 	hp_target_info_t	karg;
2438 	int			iocnum;
2439 	int			data_sz;
2440 	dma_addr_t		page_dma;
2441 	CONFIGPARMS	 	cfg;
2442 	ConfigPageHeader_t	hdr;
2443 	int			tmp, np, rc = 0;
2444 
2445 	dctlprintk((": mptctl_hp_targetinfo called.\n"));
2446 	if (copy_from_user(&karg, uarg, sizeof(hp_target_info_t))) {
2447 		printk(KERN_ERR "%s@%d::mptctl_hp_targetinfo - "
2448 			"Unable to read in hp_host_targetinfo struct @ %p\n",
2449 				__FILE__, __LINE__, uarg);
2450 		return -EFAULT;
2451 	}
2452 
2453 	if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2454 		(ioc == NULL)) {
2455 		dctlprintk((KERN_ERR "%s::mptctl_hp_targetinfo() @%d - ioc%d not found!\n",
2456 				__FILE__, __LINE__, iocnum));
2457 		return -ENODEV;
2458 	}
2459 
2460 	/*  There is nothing to do for FCP parts.
2461 	 */
2462 	if (ioc->bus_type == FC)
2463 		return 0;
2464 
2465 	if ((ioc->spi_data.sdp0length == 0) || (ioc->sh == NULL))
2466 		return 0;
2467 
2468 	if (ioc->sh->host_no != karg.hdr.host)
2469 		return -ENODEV;
2470 
2471        /* Get the data transfer speeds
2472         */
2473 	data_sz = ioc->spi_data.sdp0length * 4;
2474 	pg0_alloc = (SCSIDevicePage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page_dma);
2475 	if (pg0_alloc) {
2476 		hdr.PageVersion = ioc->spi_data.sdp0version;
2477 		hdr.PageLength = data_sz;
2478 		hdr.PageNumber = 0;
2479 		hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2480 
2481 		cfg.cfghdr.hdr = &hdr;
2482 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2483 		cfg.dir = 0;
2484 		cfg.timeout = 0;
2485 		cfg.physAddr = page_dma;
2486 
2487 		cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2488 
2489 		if ((rc = mpt_config(ioc, &cfg)) == 0) {
2490 			np = le32_to_cpu(pg0_alloc->NegotiatedParameters);
2491 			karg.negotiated_width = np & MPI_SCSIDEVPAGE0_NP_WIDE ?
2492 					HP_BUS_WIDTH_16 : HP_BUS_WIDTH_8;
2493 
2494 			if (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) {
2495 				tmp = (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
2496 				if (tmp < 0x09)
2497 					karg.negotiated_speed = HP_DEV_SPEED_ULTRA320;
2498 				else if (tmp <= 0x09)
2499 					karg.negotiated_speed = HP_DEV_SPEED_ULTRA160;
2500 				else if (tmp <= 0x0A)
2501 					karg.negotiated_speed = HP_DEV_SPEED_ULTRA2;
2502 				else if (tmp <= 0x0C)
2503 					karg.negotiated_speed = HP_DEV_SPEED_ULTRA;
2504 				else if (tmp <= 0x25)
2505 					karg.negotiated_speed = HP_DEV_SPEED_FAST;
2506 				else
2507 					karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2508 			} else
2509 				karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2510 		}
2511 
2512 		pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg0_alloc, page_dma);
2513 	}
2514 
2515 	/* Set defaults
2516 	 */
2517 	karg.message_rejects = -1;
2518 	karg.phase_errors = -1;
2519 	karg.parity_errors = -1;
2520 	karg.select_timeouts = -1;
2521 
2522 	/* Get the target error parameters
2523 	 */
2524 	hdr.PageVersion = 0;
2525 	hdr.PageLength = 0;
2526 	hdr.PageNumber = 3;
2527 	hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2528 
2529 	cfg.cfghdr.hdr = &hdr;
2530 	cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2531 	cfg.dir = 0;
2532 	cfg.timeout = 0;
2533 	cfg.physAddr = -1;
2534 	if ((mpt_config(ioc, &cfg) == 0) && (cfg.cfghdr.hdr->PageLength > 0)) {
2535 		/* Issue the second config page request */
2536 		cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2537 		data_sz = (int) cfg.cfghdr.hdr->PageLength * 4;
2538 		pg3_alloc = (SCSIDevicePage3_t *) pci_alloc_consistent(
2539 							ioc->pcidev, data_sz, &page_dma);
2540 		if (pg3_alloc) {
2541 			cfg.physAddr = page_dma;
2542 			cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2543 			if ((rc = mpt_config(ioc, &cfg)) == 0) {
2544 				karg.message_rejects = (u32) le16_to_cpu(pg3_alloc->MsgRejectCount);
2545 				karg.phase_errors = (u32) le16_to_cpu(pg3_alloc->PhaseErrorCount);
2546 				karg.parity_errors = (u32) le16_to_cpu(pg3_alloc->ParityErrorCount);
2547 			}
2548 			pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg3_alloc, page_dma);
2549 		}
2550 	}
2551 	hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
2552 	if (hd != NULL)
2553 		karg.select_timeouts = hd->sel_timeout[karg.hdr.id];
2554 
2555 	/* Copy the data from kernel memory to user memory
2556 	 */
2557 	if (copy_to_user((char __user *)arg, &karg, sizeof(hp_target_info_t))) {
2558 		printk(KERN_ERR "%s@%d::mptctl_hp_target_info - "
2559 			"Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
2560 				__FILE__, __LINE__, uarg);
2561 		return -EFAULT;
2562 	}
2563 
2564 	return 0;
2565 }
2566 
2567 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2568 
2569 static struct file_operations mptctl_fops = {
2570 	.owner =	THIS_MODULE,
2571 	.llseek =	no_llseek,
2572 	.unlocked_ioctl = mptctl_ioctl,
2573 #ifdef CONFIG_COMPAT
2574 	.compat_ioctl = compat_mpctl_ioctl,
2575 #endif
2576 };
2577 
2578 static struct miscdevice mptctl_miscdev = {
2579 	MPT_MINOR,
2580 	MYNAM,
2581 	&mptctl_fops
2582 };
2583 
2584 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2585 
2586 #ifdef CONFIG_COMPAT
2587 
2588 static int
2589 compat_mptfwxfer_ioctl(struct file *filp, unsigned int cmd,
2590 			unsigned long arg)
2591 {
2592 	struct mpt_fw_xfer32 kfw32;
2593 	struct mpt_fw_xfer kfw;
2594 	MPT_ADAPTER *iocp = NULL;
2595 	int iocnum, iocnumX;
2596 	int nonblock = (filp->f_flags & O_NONBLOCK);
2597 	int ret;
2598 
2599 	dctlprintk((KERN_INFO MYNAM "::compat_mptfwxfer_ioctl() called\n"));
2600 
2601 	if (copy_from_user(&kfw32, (char __user *)arg, sizeof(kfw32)))
2602 		return -EFAULT;
2603 
2604 	/* Verify intended MPT adapter */
2605 	iocnumX = kfw32.iocnum & 0xFF;
2606 	if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2607 	    (iocp == NULL)) {
2608 		dctlprintk((KERN_ERR MYNAM "::compat_mptfwxfer_ioctl @%d - ioc%d not found!\n",
2609 				__LINE__, iocnumX));
2610 		return -ENODEV;
2611 	}
2612 
2613 	if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2614 		return ret;
2615 
2616 	kfw.iocnum = iocnum;
2617 	kfw.fwlen = kfw32.fwlen;
2618 	kfw.bufp = compat_ptr(kfw32.bufp);
2619 
2620 	ret = mptctl_do_fw_download(kfw.iocnum, kfw.bufp, kfw.fwlen);
2621 
2622 	up(&iocp->ioctl->sem_ioc);
2623 
2624 	return ret;
2625 }
2626 
2627 static int
2628 compat_mpt_command(struct file *filp, unsigned int cmd,
2629 			unsigned long arg)
2630 {
2631 	struct mpt_ioctl_command32 karg32;
2632 	struct mpt_ioctl_command32 __user *uarg = (struct mpt_ioctl_command32 __user *) arg;
2633 	struct mpt_ioctl_command karg;
2634 	MPT_ADAPTER *iocp = NULL;
2635 	int iocnum, iocnumX;
2636 	int nonblock = (filp->f_flags & O_NONBLOCK);
2637 	int ret;
2638 
2639 	dctlprintk((KERN_INFO MYNAM "::compat_mpt_command() called\n"));
2640 
2641 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32)))
2642 		return -EFAULT;
2643 
2644 	/* Verify intended MPT adapter */
2645 	iocnumX = karg32.hdr.iocnum & 0xFF;
2646 	if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2647 	    (iocp == NULL)) {
2648 		dctlprintk((KERN_ERR MYNAM "::compat_mpt_command @%d - ioc%d not found!\n",
2649 				__LINE__, iocnumX));
2650 		return -ENODEV;
2651 	}
2652 
2653 	if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2654 		return ret;
2655 
2656 	/* Copy data to karg */
2657 	karg.hdr.iocnum = karg32.hdr.iocnum;
2658 	karg.hdr.port = karg32.hdr.port;
2659 	karg.timeout = karg32.timeout;
2660 	karg.maxReplyBytes = karg32.maxReplyBytes;
2661 
2662 	karg.dataInSize = karg32.dataInSize;
2663 	karg.dataOutSize = karg32.dataOutSize;
2664 	karg.maxSenseBytes = karg32.maxSenseBytes;
2665 	karg.dataSgeOffset = karg32.dataSgeOffset;
2666 
2667 	karg.replyFrameBufPtr = (char __user *)(unsigned long)karg32.replyFrameBufPtr;
2668 	karg.dataInBufPtr = (char __user *)(unsigned long)karg32.dataInBufPtr;
2669 	karg.dataOutBufPtr = (char __user *)(unsigned long)karg32.dataOutBufPtr;
2670 	karg.senseDataPtr = (char __user *)(unsigned long)karg32.senseDataPtr;
2671 
2672 	/* Pass new structure to do_mpt_command
2673 	 */
2674 	ret = mptctl_do_mpt_command (karg, &uarg->MF);
2675 
2676 	up(&iocp->ioctl->sem_ioc);
2677 
2678 	return ret;
2679 }
2680 
2681 static long compat_mpctl_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2682 {
2683 	long ret;
2684 	lock_kernel();
2685 	switch (cmd) {
2686 	case MPTIOCINFO:
2687 	case MPTIOCINFO1:
2688 	case MPTIOCINFO2:
2689 	case MPTTARGETINFO:
2690 	case MPTEVENTQUERY:
2691 	case MPTEVENTENABLE:
2692 	case MPTEVENTREPORT:
2693 	case MPTHARDRESET:
2694 	case HP_GETHOSTINFO:
2695 	case HP_GETTARGETINFO:
2696 	case MPTTEST:
2697 		ret = __mptctl_ioctl(f, cmd, arg);
2698 		break;
2699 	case MPTCOMMAND32:
2700 		ret = compat_mpt_command(f, cmd, arg);
2701 		break;
2702 	case MPTFWDOWNLOAD32:
2703 		ret = compat_mptfwxfer_ioctl(f, cmd, arg);
2704 		break;
2705 	default:
2706 		ret = -ENOIOCTLCMD;
2707 		break;
2708 	}
2709 	unlock_kernel();
2710 	return ret;
2711 }
2712 
2713 #endif
2714 
2715 
2716 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2717 /*
2718  *	mptctl_probe - Installs ioctl devices per bus.
2719  *	@pdev: Pointer to pci_dev structure
2720  *
2721  *	Returns 0 for success, non-zero for failure.
2722  *
2723  */
2724 
2725 static int
2726 mptctl_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2727 {
2728 	int err;
2729 	int sz;
2730 	u8 *mem;
2731 	MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2732 
2733 	/*
2734 	 * Allocate and inite a MPT_IOCTL structure
2735 	*/
2736 	sz = sizeof (MPT_IOCTL);
2737 	mem = kmalloc(sz, GFP_KERNEL);
2738 	if (mem == NULL) {
2739 		err = -ENOMEM;
2740 		goto out_fail;
2741 	}
2742 
2743 	memset(mem, 0, sz);
2744 	ioc->ioctl = (MPT_IOCTL *) mem;
2745 	ioc->ioctl->ioc = ioc;
2746 	sema_init(&ioc->ioctl->sem_ioc, 1);
2747 	return 0;
2748 
2749 out_fail:
2750 
2751 	mptctl_remove(pdev);
2752 	return err;
2753 }
2754 
2755 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2756 /*
2757  *	mptctl_remove - Removed ioctl devices
2758  *	@pdev: Pointer to pci_dev structure
2759  *
2760  *
2761  */
2762 static void
2763 mptctl_remove(struct pci_dev *pdev)
2764 {
2765 	MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2766 
2767 	kfree ( ioc->ioctl );
2768 }
2769 
2770 static struct mpt_pci_driver mptctl_driver = {
2771   .probe		= mptctl_probe,
2772   .remove		= mptctl_remove,
2773 };
2774 
2775 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2776 static int __init mptctl_init(void)
2777 {
2778 	int err;
2779 	int where = 1;
2780 
2781 	show_mptmod_ver(my_NAME, my_VERSION);
2782 
2783 	if(mpt_device_driver_register(&mptctl_driver,
2784 	  MPTCTL_DRIVER) != 0 ) {
2785 		dprintk((KERN_INFO MYNAM
2786 		": failed to register dd callbacks\n"));
2787 	}
2788 
2789 	/* Register this device */
2790 	err = misc_register(&mptctl_miscdev);
2791 	if (err < 0) {
2792 		printk(KERN_ERR MYNAM ": Can't register misc device [minor=%d].\n", MPT_MINOR);
2793 		goto out_fail;
2794 	}
2795 	printk(KERN_INFO MYNAM ": Registered with Fusion MPT base driver\n");
2796 	printk(KERN_INFO MYNAM ": /dev/%s @ (major,minor=%d,%d)\n",
2797 			 mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2798 
2799 	/*
2800 	 *  Install our handler
2801 	 */
2802 	++where;
2803 	if ((mptctl_id = mpt_register(mptctl_reply, MPTCTL_DRIVER)) < 0) {
2804 		printk(KERN_ERR MYNAM ": ERROR: Failed to register with Fusion MPT base driver\n");
2805 		misc_deregister(&mptctl_miscdev);
2806 		err = -EBUSY;
2807 		goto out_fail;
2808 	}
2809 
2810 	if (mpt_reset_register(mptctl_id, mptctl_ioc_reset) == 0) {
2811 		dprintk((KERN_INFO MYNAM ": Registered for IOC reset notifications\n"));
2812 	} else {
2813 		/* FIXME! */
2814 	}
2815 
2816 	return 0;
2817 
2818 out_fail:
2819 
2820 	mpt_device_driver_deregister(MPTCTL_DRIVER);
2821 
2822 	return err;
2823 }
2824 
2825 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2826 static void mptctl_exit(void)
2827 {
2828 	misc_deregister(&mptctl_miscdev);
2829 	printk(KERN_INFO MYNAM ": Deregistered /dev/%s @ (major,minor=%d,%d)\n",
2830 			 mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2831 
2832 	/* De-register reset handler from base module */
2833 	mpt_reset_deregister(mptctl_id);
2834 	dprintk((KERN_INFO MYNAM ": Deregistered for IOC reset notifications\n"));
2835 
2836 	/* De-register callback handler from base module */
2837 	mpt_deregister(mptctl_id);
2838 	printk(KERN_INFO MYNAM ": Deregistered from Fusion MPT base driver\n");
2839 
2840         mpt_device_driver_deregister(MPTCTL_DRIVER);
2841 
2842 }
2843 
2844 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2845 
2846 module_init(mptctl_init);
2847 module_exit(mptctl_exit);
2848