]> err.no Git - linux-2.6/blob - drivers/message/fusion/mptctl.c
[SCSI] fusion - mptctl -firmware download fix
[linux-2.6] / drivers / message / fusion / mptctl.c
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 (!mutex_trylock(&ioc->ioctl->ioctl_mutex))
181                         rc = -EAGAIN;
182         } else {
183                 if (mutex_lock_interruptible(&ioc->ioctl->ioctl_mutex))
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_timeout(mptctl_wait,
389              ioctl->wait_done == 1,
390              HZ*5 /* 5 second timeout */);
391
392         if(ii <=0 && (ioctl->wait_done != 1 ))  {
393                 mpt_free_msg_frame(hd->ioc, mf);
394                 ioctl->wait_done = 0;
395                 retval = -1; /* return failure */
396         }
397
398 mptctl_bus_reset_done:
399
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         mutex_unlock(&iocp->ioctl->ioctl_mutex);
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(("mptctl_do_fwdl called. mptctl_id = %xh.\n", mptctl_id));
678
679         dctlprintk(("DbG: kfwdl.bufp  = %p\n", ufwbuf));
680         dctlprintk(("DbG: kfwdl.fwlen = %d\n", (int)fwlen));
681         dctlprintk(("DbG: kfwdl.ioc   = %04xh\n", ioc));
682
683         if (mpt_verify_adapter(ioc, &iocp) < 0) {
684                 dctlprintk(("ioctl_fwdl - ioc%d not found!\n",
685                                  ioc));
686                 return -ENODEV; /* (-6) No such device or address */
687         } else {
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         }
694         dlmsg = (FWDownload_t*) mf;
695         ptsge = (FWDownloadTCSGE_t *) &dlmsg->SGL;
696         sgOut = (char *) (ptsge + 1);
697
698         /*
699          * Construct f/w download request
700          */
701         dlmsg->ImageType = MPI_FW_DOWNLOAD_ITYPE_FW;
702         dlmsg->Reserved = 0;
703         dlmsg->ChainOffset = 0;
704         dlmsg->Function = MPI_FUNCTION_FW_DOWNLOAD;
705         dlmsg->Reserved1[0] = dlmsg->Reserved1[1] = dlmsg->Reserved1[2] = 0;
706         if (iocp->facts.MsgVersion >= MPI_VERSION_01_05)
707                 dlmsg->MsgFlags = MPI_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT;
708         else
709                 dlmsg->MsgFlags = 0;
710
711
712         /* Set up the Transaction SGE.
713          */
714         ptsge->Reserved = 0;
715         ptsge->ContextSize = 0;
716         ptsge->DetailsLength = 12;
717         ptsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
718         ptsge->Reserved_0100_Checksum = 0;
719         ptsge->ImageOffset = 0;
720         ptsge->ImageSize = cpu_to_le32(fwlen);
721
722         /* Add the SGL
723          */
724
725         /*
726          * Need to kmalloc area(s) for holding firmware image bytes.
727          * But we need to do it piece meal, using a proper
728          * scatter gather list (with 128kB MAX hunks).
729          *
730          * A practical limit here might be # of sg hunks that fit into
731          * a single IOC request frame; 12 or 8 (see below), so:
732          * For FC9xx: 12 x 128kB == 1.5 mB (max)
733          * For C1030:  8 x 128kB == 1   mB (max)
734          * We could support chaining, but things get ugly(ier:)
735          *
736          * Set the sge_offset to the start of the sgl (bytes).
737          */
738         sgdir = 0x04000000;             /* IOC will READ from sys mem */
739         sge_offset = sizeof(MPIHeader_t) + sizeof(FWDownloadTCSGE_t);
740         if ((sgl = kbuf_alloc_2_sgl(fwlen, sgdir, sge_offset,
741                                     &numfrags, &buflist, &sgl_dma, iocp)) == NULL)
742                 return -ENOMEM;
743
744         /*
745          * We should only need SGL with 2 simple_32bit entries (up to 256 kB)
746          * for FC9xx f/w image, but calculate max number of sge hunks
747          * we can fit into a request frame, and limit ourselves to that.
748          * (currently no chain support)
749          * maxfrags = (Request Size - FWdownload Size ) / Size of 32 bit SGE
750          *      Request         maxfrags
751          *      128             12
752          *      96              8
753          *      64              4
754          */
755         maxfrags = (iocp->req_sz - sizeof(MPIHeader_t) - sizeof(FWDownloadTCSGE_t))
756                         / (sizeof(dma_addr_t) + sizeof(u32));
757         if (numfrags > maxfrags) {
758                 ret = -EMLINK;
759                 goto fwdl_out;
760         }
761
762         dctlprintk(("DbG: sgl buffer  = %p, sgfrags = %d\n", sgl, numfrags));
763
764         /*
765          * Parse SG list, copying sgl itself,
766          * plus f/w image hunks from user space as we go...
767          */
768         ret = -EFAULT;
769         sgIn = sgl;
770         bl = buflist;
771         for (i=0; i < numfrags; i++) {
772
773                 /* Get the SGE type: 0 - TCSGE, 3 - Chain, 1 - Simple SGE
774                  * Skip everything but Simple. If simple, copy from
775                  *      user space into kernel space.
776                  * Note: we should not have anything but Simple as
777                  *      Chain SGE are illegal.
778                  */
779                 nib = (sgIn->FlagsLength & 0x30000000) >> 28;
780                 if (nib == 0 || nib == 3) {
781                         ;
782                 } else if (sgIn->Address) {
783                         mpt_add_sge(sgOut, sgIn->FlagsLength, sgIn->Address);
784                         n++;
785                         if (copy_from_user(bl->kptr, ufwbuf+fw_bytes_copied, bl->len)) {
786                                 printk(KERN_ERR "%s@%d::_ioctl_fwdl - "
787                                                 "Unable to copy f/w buffer hunk#%d @ %p\n",
788                                                 __FILE__, __LINE__, n, ufwbuf);
789                                 goto fwdl_out;
790                         }
791                         fw_bytes_copied += bl->len;
792                 }
793                 sgIn++;
794                 bl++;
795                 sgOut += (sizeof(dma_addr_t) + sizeof(u32));
796         }
797
798 #ifdef MPT_DEBUG
799         {
800                 u32 *m = (u32 *)mf;
801                 printk(KERN_INFO MYNAM ": F/W download request:\n" KERN_INFO " ");
802                 for (i=0; i < 7+numfrags*2; i++)
803                         printk(" %08x", le32_to_cpu(m[i]));
804                 printk("\n");
805         }
806 #endif
807
808         /*
809          * Finally, perform firmware download.
810          */
811         ReplyMsg = NULL;
812         mpt_put_msg_frame(mptctl_id, iocp, mf);
813
814         /* Now wait for the command to complete */
815         ret = wait_event_timeout(mptctl_wait,
816              iocp->ioctl->wait_done == 1,
817              HZ*60);
818
819         if(ret <=0 && (iocp->ioctl->wait_done != 1 )) {
820         /* Now we need to reset the board */
821                 mptctl_timeout_expired(iocp->ioctl);
822                 ret = -ENODATA;
823                 goto fwdl_out;
824         }
825
826         if (sgl)
827                 kfree_sgl(sgl, sgl_dma, buflist, iocp);
828
829         ReplyMsg = (pFWDownloadReply_t)iocp->ioctl->ReplyFrame;
830         iocstat = le16_to_cpu(ReplyMsg->IOCStatus) & MPI_IOCSTATUS_MASK;
831         if (iocstat == MPI_IOCSTATUS_SUCCESS) {
832                 printk(KERN_INFO MYNAM ": F/W update successfully sent to %s!\n", iocp->name);
833                 return 0;
834         } else if (iocstat == MPI_IOCSTATUS_INVALID_FUNCTION) {
835                 printk(KERN_WARNING MYNAM ": ?Hmmm...  %s says it doesn't support F/W download!?!\n",
836                                 iocp->name);
837                 printk(KERN_WARNING MYNAM ": (time to go bang on somebodies door)\n");
838                 return -EBADRQC;
839         } else if (iocstat == MPI_IOCSTATUS_BUSY) {
840                 printk(KERN_WARNING MYNAM ": Warning!  %s says: IOC_BUSY!\n", iocp->name);
841                 printk(KERN_WARNING MYNAM ": (try again later?)\n");
842                 return -EBUSY;
843         } else {
844                 printk(KERN_WARNING MYNAM "::ioctl_fwdl() ERROR!  %s returned [bad] status = %04xh\n",
845                                     iocp->name, iocstat);
846                 printk(KERN_WARNING MYNAM ": (bad VooDoo)\n");
847                 return -ENOMSG;
848         }
849         return 0;
850
851 fwdl_out:
852         kfree_sgl(sgl, sgl_dma, buflist, iocp);
853         return ret;
854 }
855
856 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
857 /*
858  * SGE Allocation routine
859  *
860  * Inputs:      bytes - number of bytes to be transferred
861  *              sgdir - data direction
862  *              sge_offset - offset (in bytes) from the start of the request
863  *                      frame to the first SGE
864  *              ioc - pointer to the mptadapter
865  * Outputs:     frags - number of scatter gather elements
866  *              blp - point to the buflist pointer
867  *              sglbuf_dma - pointer to the (dma) sgl
868  * Returns:     Null if failes
869  *              pointer to the (virtual) sgl if successful.
870  */
871 static MptSge_t *
872 kbuf_alloc_2_sgl(int bytes, u32 sgdir, int sge_offset, int *frags,
873                  struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc)
874 {
875         MptSge_t        *sglbuf = NULL;         /* pointer to array of SGE */
876                                                 /* and chain buffers */
877         struct buflist  *buflist = NULL;        /* kernel routine */
878         MptSge_t        *sgl;
879         int              numfrags = 0;
880         int              fragcnt = 0;
881         int              alloc_sz = min(bytes,MAX_KMALLOC_SZ);  // avoid kernel warning msg!
882         int              bytes_allocd = 0;
883         int              this_alloc;
884         dma_addr_t       pa;                                    // phys addr
885         int              i, buflist_ent;
886         int              sg_spill = MAX_FRAGS_SPILL1;
887         int              dir;
888         /* initialization */
889         *frags = 0;
890         *blp = NULL;
891
892         /* Allocate and initialize an array of kernel
893          * structures for the SG elements.
894          */
895         i = MAX_SGL_BYTES / 8;
896         buflist = kmalloc(i, GFP_USER);
897         if (buflist == NULL)
898                 return NULL;
899         memset(buflist, 0, i);
900         buflist_ent = 0;
901
902         /* Allocate a single block of memory to store the sg elements and
903          * the chain buffers.  The calling routine is responsible for
904          * copying the data in this array into the correct place in the
905          * request and chain buffers.
906          */
907         sglbuf = pci_alloc_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf_dma);
908         if (sglbuf == NULL)
909                 goto free_and_fail;
910
911         if (sgdir & 0x04000000)
912                 dir = PCI_DMA_TODEVICE;
913         else
914                 dir = PCI_DMA_FROMDEVICE;
915
916         /* At start:
917          *      sgl = sglbuf = point to beginning of sg buffer
918          *      buflist_ent = 0 = first kernel structure
919          *      sg_spill = number of SGE that can be written before the first
920          *              chain element.
921          *
922          */
923         sgl = sglbuf;
924         sg_spill = ((ioc->req_sz - sge_offset)/(sizeof(dma_addr_t) + sizeof(u32))) - 1;
925         while (bytes_allocd < bytes) {
926                 this_alloc = min(alloc_sz, bytes-bytes_allocd);
927                 buflist[buflist_ent].len = this_alloc;
928                 buflist[buflist_ent].kptr = pci_alloc_consistent(ioc->pcidev,
929                                                                  this_alloc,
930                                                                  &pa);
931                 if (buflist[buflist_ent].kptr == NULL) {
932                         alloc_sz = alloc_sz / 2;
933                         if (alloc_sz == 0) {
934                                 printk(KERN_WARNING MYNAM "-SG: No can do - "
935                                                     "not enough memory!   :-(\n");
936                                 printk(KERN_WARNING MYNAM "-SG: (freeing %d frags)\n",
937                                                     numfrags);
938                                 goto free_and_fail;
939                         }
940                         continue;
941                 } else {
942                         dma_addr_t dma_addr;
943
944                         bytes_allocd += this_alloc;
945                         sgl->FlagsLength = (0x10000000|MPT_SGE_FLAGS_ADDRESSING|sgdir|this_alloc);
946                         dma_addr = pci_map_single(ioc->pcidev, buflist[buflist_ent].kptr, this_alloc, dir);
947                         sgl->Address = dma_addr;
948
949                         fragcnt++;
950                         numfrags++;
951                         sgl++;
952                         buflist_ent++;
953                 }
954
955                 if (bytes_allocd >= bytes)
956                         break;
957
958                 /* Need to chain? */
959                 if (fragcnt == sg_spill) {
960                         printk(KERN_WARNING MYNAM "-SG: No can do - " "Chain required!   :-(\n");
961                         printk(KERN_WARNING MYNAM "(freeing %d frags)\n", numfrags);
962                         goto free_and_fail;
963                 }
964
965                 /* overflow check... */
966                 if (numfrags*8 > MAX_SGL_BYTES){
967                         /* GRRRRR... */
968                         printk(KERN_WARNING MYNAM "-SG: No can do - "
969                                             "too many SG frags!   :-(\n");
970                         printk(KERN_WARNING MYNAM "-SG: (freeing %d frags)\n",
971                                             numfrags);
972                         goto free_and_fail;
973                 }
974         }
975
976         /* Last sge fixup: set LE+eol+eob bits */
977         sgl[-1].FlagsLength |= 0xC1000000;
978
979         *frags = numfrags;
980         *blp = buflist;
981
982         dctlprintk((KERN_INFO MYNAM "-SG: kbuf_alloc_2_sgl() - "
983                            "%d SG frags generated!\n",
984                            numfrags));
985
986         dctlprintk((KERN_INFO MYNAM "-SG: kbuf_alloc_2_sgl() - "
987                            "last (big) alloc_sz=%d\n",
988                            alloc_sz));
989
990         return sglbuf;
991
992 free_and_fail:
993         if (sglbuf != NULL) {
994                 int i;
995
996                 for (i = 0; i < numfrags; i++) {
997                         dma_addr_t dma_addr;
998                         u8 *kptr;
999                         int len;
1000
1001                         if ((sglbuf[i].FlagsLength >> 24) == 0x30)
1002                                 continue;
1003
1004                         dma_addr = sglbuf[i].Address;
1005                         kptr = buflist[i].kptr;
1006                         len = buflist[i].len;
1007
1008                         pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1009                 }
1010                 pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf, *sglbuf_dma);
1011         }
1012         kfree(buflist);
1013         return NULL;
1014 }
1015
1016 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1017 /*
1018  * Routine to free the SGL elements.
1019  */
1020 static void
1021 kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma, struct buflist *buflist, MPT_ADAPTER *ioc)
1022 {
1023         MptSge_t        *sg = sgl;
1024         struct buflist  *bl = buflist;
1025         u32              nib;
1026         int              dir;
1027         int              n = 0;
1028
1029         if (sg->FlagsLength & 0x04000000)
1030                 dir = PCI_DMA_TODEVICE;
1031         else
1032                 dir = PCI_DMA_FROMDEVICE;
1033
1034         nib = (sg->FlagsLength & 0xF0000000) >> 28;
1035         while (! (nib & 0x4)) { /* eob */
1036                 /* skip ignore/chain. */
1037                 if (nib == 0 || nib == 3) {
1038                         ;
1039                 } else if (sg->Address) {
1040                         dma_addr_t dma_addr;
1041                         void *kptr;
1042                         int len;
1043
1044                         dma_addr = sg->Address;
1045                         kptr = bl->kptr;
1046                         len = bl->len;
1047                         pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1048                         pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1049                         n++;
1050                 }
1051                 sg++;
1052                 bl++;
1053                 nib = (le32_to_cpu(sg->FlagsLength) & 0xF0000000) >> 28;
1054         }
1055
1056         /* we're at eob! */
1057         if (sg->Address) {
1058                 dma_addr_t dma_addr;
1059                 void *kptr;
1060                 int len;
1061
1062                 dma_addr = sg->Address;
1063                 kptr = bl->kptr;
1064                 len = bl->len;
1065                 pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1066                 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1067                 n++;
1068         }
1069
1070         pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sgl, sgl_dma);
1071         kfree(buflist);
1072         dctlprintk((KERN_INFO MYNAM "-SG: Free'd 1 SGL buf + %d kbufs!\n", n));
1073 }
1074
1075 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1076 /*
1077  *      mptctl_getiocinfo - Query the host adapter for IOC information.
1078  *      @arg: User space argument
1079  *
1080  * Outputs:     None.
1081  * Return:      0 if successful
1082  *              -EFAULT if data unavailable
1083  *              -ENODEV  if no such device/adapter
1084  */
1085 static int
1086 mptctl_getiocinfo (unsigned long arg, unsigned int data_size)
1087 {
1088         struct mpt_ioctl_iocinfo __user *uarg = (void __user *) arg;
1089         struct mpt_ioctl_iocinfo *karg;
1090         MPT_ADAPTER             *ioc;
1091         struct pci_dev          *pdev;
1092         struct Scsi_Host        *sh;
1093         MPT_SCSI_HOST           *hd;
1094         int                     iocnum;
1095         int                     numDevices = 0;
1096         unsigned int            max_id;
1097         int                     ii;
1098         unsigned int            port;
1099         int                     cim_rev;
1100         u8                      revision;
1101
1102         dctlprintk((": mptctl_getiocinfo called.\n"));
1103         /* Add of PCI INFO results in unaligned access for
1104          * IA64 and Sparc. Reset long to int. Return no PCI
1105          * data for obsolete format.
1106          */
1107         if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev0))
1108                 cim_rev = 0;
1109         else if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev1))
1110                 cim_rev = 1;
1111         else if (data_size == sizeof(struct mpt_ioctl_iocinfo))
1112                 cim_rev = 2;
1113         else if (data_size == (sizeof(struct mpt_ioctl_iocinfo_rev0)+12))
1114                 cim_rev = 0;    /* obsolete */
1115         else
1116                 return -EFAULT;
1117
1118         karg = kmalloc(data_size, GFP_KERNEL);
1119         if (karg == NULL) {
1120                 printk(KERN_ERR "%s::mpt_ioctl_iocinfo() @%d - no memory available!\n",
1121                                 __FILE__, __LINE__);
1122                 return -ENOMEM;
1123         }
1124
1125         if (copy_from_user(karg, uarg, data_size)) {
1126                 printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
1127                         "Unable to read in mpt_ioctl_iocinfo struct @ %p\n",
1128                                 __FILE__, __LINE__, uarg);
1129                 kfree(karg);
1130                 return -EFAULT;
1131         }
1132
1133         if (((iocnum = mpt_verify_adapter(karg->hdr.iocnum, &ioc)) < 0) ||
1134             (ioc == NULL)) {
1135                 dctlprintk((KERN_ERR "%s::mptctl_getiocinfo() @%d - ioc%d not found!\n",
1136                                 __FILE__, __LINE__, iocnum));
1137                 kfree(karg);
1138                 return -ENODEV;
1139         }
1140
1141         /* Verify the data transfer size is correct. */
1142         if (karg->hdr.maxDataSize != data_size) {
1143                 printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
1144                         "Structure size mismatch. Command not completed.\n",
1145                                 __FILE__, __LINE__);
1146                 kfree(karg);
1147                 return -EFAULT;
1148         }
1149
1150         /* Fill in the data and return the structure to the calling
1151          * program
1152          */
1153         if (ioc->bus_type == SAS)
1154                 karg->adapterType = MPT_IOCTL_INTERFACE_SAS;
1155         else if (ioc->bus_type == FC)
1156                 karg->adapterType = MPT_IOCTL_INTERFACE_FC;
1157         else
1158                 karg->adapterType = MPT_IOCTL_INTERFACE_SCSI;
1159
1160         if (karg->hdr.port > 1)
1161                 return -EINVAL;
1162         port = karg->hdr.port;
1163
1164         karg->port = port;
1165         pdev = (struct pci_dev *) ioc->pcidev;
1166
1167         karg->pciId = pdev->device;
1168         pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1169         karg->hwRev = revision;
1170         karg->subSystemDevice = pdev->subsystem_device;
1171         karg->subSystemVendor = pdev->subsystem_vendor;
1172
1173         if (cim_rev == 1) {
1174                 /* Get the PCI bus, device, and function numbers for the IOC
1175                  */
1176                 karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1177                 karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1178                 karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1179         } else if (cim_rev == 2) {
1180                 /* Get the PCI bus, device, function and segment ID numbers
1181                    for the IOC */
1182                 karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1183                 karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1184                 karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1185                 karg->pciInfo.segmentID = pci_domain_nr(pdev->bus);
1186         }
1187
1188         /* Get number of devices
1189          */
1190         if ((sh = ioc->sh) != NULL) {
1191                  /* sh->max_id = maximum target ID + 1
1192                  */
1193                 max_id = sh->max_id - 1;
1194                 hd = (MPT_SCSI_HOST *) sh->hostdata;
1195
1196                 /* Check all of the target structures and
1197                  * keep a counter.
1198                  */
1199                 if (hd && hd->Targets) {
1200                         for (ii = 0; ii <= max_id; ii++) {
1201                                 if (hd->Targets[ii])
1202                                         numDevices++;
1203                         }
1204                 }
1205         }
1206         karg->numDevices = numDevices;
1207
1208         /* Set the BIOS and FW Version
1209          */
1210         karg->FWVersion = ioc->facts.FWVersion.Word;
1211         karg->BIOSVersion = ioc->biosVersion;
1212
1213         /* Set the Version Strings.
1214          */
1215         strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
1216         karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
1217
1218         karg->busChangeEvent = 0;
1219         karg->hostId = ioc->pfacts[port].PortSCSIID;
1220         karg->rsvd[0] = karg->rsvd[1] = 0;
1221
1222         /* Copy the data from kernel memory to user memory
1223          */
1224         if (copy_to_user((char __user *)arg, karg, data_size)) {
1225                 printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
1226                         "Unable to write out mpt_ioctl_iocinfo struct @ %p\n",
1227                                 __FILE__, __LINE__, uarg);
1228                 kfree(karg);
1229                 return -EFAULT;
1230         }
1231
1232         kfree(karg);
1233         return 0;
1234 }
1235
1236 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1237 /*
1238  *      mptctl_gettargetinfo - Query the host adapter for target information.
1239  *      @arg: User space argument
1240  *
1241  * Outputs:     None.
1242  * Return:      0 if successful
1243  *              -EFAULT if data unavailable
1244  *              -ENODEV  if no such device/adapter
1245  */
1246 static int
1247 mptctl_gettargetinfo (unsigned long arg)
1248 {
1249         struct mpt_ioctl_targetinfo __user *uarg = (void __user *) arg;
1250         struct mpt_ioctl_targetinfo karg;
1251         MPT_ADAPTER             *ioc;
1252         struct Scsi_Host        *sh;
1253         MPT_SCSI_HOST           *hd;
1254         VirtTarget              *vdev;
1255         char                    *pmem;
1256         int                     *pdata;
1257         IOCPage2_t              *pIoc2;
1258         IOCPage3_t              *pIoc3;
1259         int                     iocnum;
1260         int                     numDevices = 0;
1261         unsigned int            max_id;
1262         int                     id, jj, indexed_lun, lun_index;
1263         u32                     lun;
1264         int                     maxWordsLeft;
1265         int                     numBytes;
1266         u8                      port, devType, bus_id;
1267
1268         dctlprintk(("mptctl_gettargetinfo called.\n"));
1269         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_targetinfo))) {
1270                 printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
1271                         "Unable to read in mpt_ioctl_targetinfo struct @ %p\n",
1272                                 __FILE__, __LINE__, uarg);
1273                 return -EFAULT;
1274         }
1275
1276         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1277             (ioc == NULL)) {
1278                 dctlprintk((KERN_ERR "%s::mptctl_gettargetinfo() @%d - ioc%d not found!\n",
1279                                 __FILE__, __LINE__, iocnum));
1280                 return -ENODEV;
1281         }
1282
1283         /* Get the port number and set the maximum number of bytes
1284          * in the returned structure.
1285          * Ignore the port setting.
1286          */
1287         numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1288         maxWordsLeft = numBytes/sizeof(int);
1289         port = karg.hdr.port;
1290
1291         if (maxWordsLeft <= 0) {
1292                 printk(KERN_ERR "%s::mptctl_gettargetinfo() @%d - no memory available!\n",
1293                                 __FILE__, __LINE__);
1294                 return -ENOMEM;
1295         }
1296
1297         /* Fill in the data and return the structure to the calling
1298          * program
1299          */
1300
1301         /* struct mpt_ioctl_targetinfo does not contain sufficient space
1302          * for the target structures so when the IOCTL is called, there is
1303          * not sufficient stack space for the structure. Allocate memory,
1304          * populate the memory, copy back to the user, then free memory.
1305          * targetInfo format:
1306          * bits 31-24: reserved
1307          *      23-16: LUN
1308          *      15- 8: Bus Number
1309          *       7- 0: Target ID
1310          */
1311         pmem = kmalloc(numBytes, GFP_KERNEL);
1312         if (pmem == NULL) {
1313                 printk(KERN_ERR "%s::mptctl_gettargetinfo() @%d - no memory available!\n",
1314                                 __FILE__, __LINE__);
1315                 return -ENOMEM;
1316         }
1317         memset(pmem, 0, numBytes);
1318         pdata =  (int *) pmem;
1319
1320         /* Get number of devices
1321          */
1322         if ((sh = ioc->sh) != NULL) {
1323
1324                 max_id = sh->max_id - 1;
1325                 hd = (MPT_SCSI_HOST *) sh->hostdata;
1326
1327                 /* Check all of the target structures.
1328                  * Save the Id and increment the counter,
1329                  * if ptr non-null.
1330                  * sh->max_id = maximum target ID + 1
1331                  */
1332                 if (hd && hd->Targets) {
1333                         mpt_findImVolumes(ioc);
1334                         pIoc2 = ioc->raid_data.pIocPg2;
1335                         for ( id = 0; id <= max_id; ) {
1336                                 if ( pIoc2 && pIoc2->NumActiveVolumes ) {
1337                                         if ( id == pIoc2->RaidVolume[0].VolumeID ) {
1338                                                 if (maxWordsLeft <= 0) {
1339                                                         printk(KERN_ERR "mptctl_gettargetinfo - "
1340                         "buffer is full but volume is available on ioc %d\n, numDevices=%d", iocnum, numDevices);
1341                                                         goto data_space_full;
1342                                                 }
1343                                                 if ( ( pIoc2->RaidVolume[0].Flags & MPI_IOCPAGE2_FLAG_VOLUME_INACTIVE ) == 0 )
1344                                                         devType = 0x80;
1345                                                 else
1346                                                         devType = 0xC0;
1347                                                 bus_id = pIoc2->RaidVolume[0].VolumeBus;
1348                                                 numDevices++;
1349                                                 *pdata = ( (devType << 24) | (bus_id << 8) | id );
1350                                                 dctlprintk((KERN_ERR "mptctl_gettargetinfo - "
1351                 "volume ioc=%d target=%x numDevices=%d pdata=%p\n", iocnum, *pdata, numDevices, pdata));
1352                                                 pdata++;
1353                                                 --maxWordsLeft;
1354                                                 goto next_id;
1355                                         } else {
1356                                                 pIoc3 = ioc->raid_data.pIocPg3;
1357                                                 for ( jj = 0; jj < pIoc3->NumPhysDisks; jj++ ) {
1358                                                         if ( pIoc3->PhysDisk[jj].PhysDiskID == id )
1359                                                                 goto next_id;
1360                                                 }
1361                                         }
1362                                 }
1363                                 if ( (vdev = hd->Targets[id]) ) {
1364                                         for (jj = 0; jj <= MPT_LAST_LUN; jj++) {
1365                                                 lun_index = (jj >> 5);
1366                                                 indexed_lun = (jj % 32);
1367                                                 lun = (1 << indexed_lun);
1368                                                 if (vdev->luns[lun_index] & lun) {
1369                                                         if (maxWordsLeft <= 0) {
1370                                                                 printk(KERN_ERR "mptctl_gettargetinfo - "
1371                         "buffer is full but more targets are available on ioc %d numDevices=%d\n", iocnum, numDevices);
1372                                                                 goto data_space_full;
1373                                                         }
1374                                                         bus_id = vdev->bus_id;
1375                                                         numDevices++;
1376                                                         *pdata = ( (jj << 16) | (bus_id << 8) | id );
1377                                                         dctlprintk((KERN_ERR "mptctl_gettargetinfo - "
1378                 "target ioc=%d target=%x numDevices=%d pdata=%p\n", iocnum, *pdata, numDevices, pdata));
1379                                                         pdata++;
1380                                                         --maxWordsLeft;
1381                                                 }
1382                                         }
1383                                 }
1384 next_id:
1385                                 id++;
1386                         }
1387                 }
1388         }
1389 data_space_full:
1390         karg.numDevices = numDevices;
1391
1392         /* Copy part of the data from kernel memory to user memory
1393          */
1394         if (copy_to_user((char __user *)arg, &karg,
1395                                 sizeof(struct mpt_ioctl_targetinfo))) {
1396                 printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
1397                         "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
1398                                 __FILE__, __LINE__, uarg);
1399                 kfree(pmem);
1400                 return -EFAULT;
1401         }
1402
1403         /* Copy the remaining data from kernel memory to user memory
1404          */
1405         if (copy_to_user(uarg->targetInfo, pmem, numBytes)) {
1406                 printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
1407                         "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
1408                                 __FILE__, __LINE__, pdata);
1409                 kfree(pmem);
1410                 return -EFAULT;
1411         }
1412
1413         kfree(pmem);
1414
1415         return 0;
1416 }
1417
1418 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1419 /* MPT IOCTL Test function.
1420  *
1421  * Outputs:     None.
1422  * Return:      0 if successful
1423  *              -EFAULT if data unavailable
1424  *              -ENODEV  if no such device/adapter
1425  */
1426 static int
1427 mptctl_readtest (unsigned long arg)
1428 {
1429         struct mpt_ioctl_test __user *uarg = (void __user *) arg;
1430         struct mpt_ioctl_test    karg;
1431         MPT_ADAPTER *ioc;
1432         int iocnum;
1433
1434         dctlprintk(("mptctl_readtest called.\n"));
1435         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_test))) {
1436                 printk(KERN_ERR "%s@%d::mptctl_readtest - "
1437                         "Unable to read in mpt_ioctl_test struct @ %p\n",
1438                                 __FILE__, __LINE__, uarg);
1439                 return -EFAULT;
1440         }
1441
1442         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1443             (ioc == NULL)) {
1444                 dctlprintk((KERN_ERR "%s::mptctl_readtest() @%d - ioc%d not found!\n",
1445                                 __FILE__, __LINE__, iocnum));
1446                 return -ENODEV;
1447         }
1448
1449         /* Fill in the data and return the structure to the calling
1450          * program
1451          */
1452
1453 #ifdef MFCNT
1454         karg.chip_type = ioc->mfcnt;
1455 #else
1456         karg.chip_type = ioc->pcidev->device;
1457 #endif
1458         strncpy (karg.name, ioc->name, MPT_MAX_NAME);
1459         karg.name[MPT_MAX_NAME-1]='\0';
1460         strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
1461         karg.product[MPT_PRODUCT_LENGTH-1]='\0';
1462
1463         /* Copy the data from kernel memory to user memory
1464          */
1465         if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_test))) {
1466                 printk(KERN_ERR "%s@%d::mptctl_readtest - "
1467                         "Unable to write out mpt_ioctl_test struct @ %p\n",
1468                                 __FILE__, __LINE__, uarg);
1469                 return -EFAULT;
1470         }
1471
1472         return 0;
1473 }
1474
1475 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1476 /*
1477  *      mptctl_eventquery - Query the host adapter for the event types
1478  *      that are being logged.
1479  *      @arg: User space argument
1480  *
1481  * Outputs:     None.
1482  * Return:      0 if successful
1483  *              -EFAULT if data unavailable
1484  *              -ENODEV  if no such device/adapter
1485  */
1486 static int
1487 mptctl_eventquery (unsigned long arg)
1488 {
1489         struct mpt_ioctl_eventquery __user *uarg = (void __user *) arg;
1490         struct mpt_ioctl_eventquery      karg;
1491         MPT_ADAPTER *ioc;
1492         int iocnum;
1493
1494         dctlprintk(("mptctl_eventquery called.\n"));
1495         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventquery))) {
1496                 printk(KERN_ERR "%s@%d::mptctl_eventquery - "
1497                         "Unable to read in mpt_ioctl_eventquery struct @ %p\n",
1498                                 __FILE__, __LINE__, uarg);
1499                 return -EFAULT;
1500         }
1501
1502         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1503             (ioc == NULL)) {
1504                 dctlprintk((KERN_ERR "%s::mptctl_eventquery() @%d - ioc%d not found!\n",
1505                                 __FILE__, __LINE__, iocnum));
1506                 return -ENODEV;
1507         }
1508
1509         karg.eventEntries = MPTCTL_EVENT_LOG_SIZE;
1510         karg.eventTypes = ioc->eventTypes;
1511
1512         /* Copy the data from kernel memory to user memory
1513          */
1514         if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_eventquery))) {
1515                 printk(KERN_ERR "%s@%d::mptctl_eventquery - "
1516                         "Unable to write out mpt_ioctl_eventquery struct @ %p\n",
1517                                 __FILE__, __LINE__, uarg);
1518                 return -EFAULT;
1519         }
1520         return 0;
1521 }
1522
1523 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1524 static int
1525 mptctl_eventenable (unsigned long arg)
1526 {
1527         struct mpt_ioctl_eventenable __user *uarg = (void __user *) arg;
1528         struct mpt_ioctl_eventenable     karg;
1529         MPT_ADAPTER *ioc;
1530         int iocnum;
1531
1532         dctlprintk(("mptctl_eventenable called.\n"));
1533         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventenable))) {
1534                 printk(KERN_ERR "%s@%d::mptctl_eventenable - "
1535                         "Unable to read in mpt_ioctl_eventenable struct @ %p\n",
1536                                 __FILE__, __LINE__, uarg);
1537                 return -EFAULT;
1538         }
1539
1540         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1541             (ioc == NULL)) {
1542                 dctlprintk((KERN_ERR "%s::mptctl_eventenable() @%d - ioc%d not found!\n",
1543                                 __FILE__, __LINE__, iocnum));
1544                 return -ENODEV;
1545         }
1546
1547         if (ioc->events == NULL) {
1548                 /* Have not yet allocated memory - do so now.
1549                  */
1550                 int sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
1551                 ioc->events = kmalloc(sz, GFP_KERNEL);
1552                 if (ioc->events == NULL) {
1553                         printk(KERN_ERR MYNAM ": ERROR - Insufficient memory to add adapter!\n");
1554                         return -ENOMEM;
1555                 }
1556                 memset(ioc->events, 0, sz);
1557                 ioc->alloc_total += sz;
1558
1559                 ioc->eventContext = 0;
1560         }
1561
1562         /* Update the IOC event logging flag.
1563          */
1564         ioc->eventTypes = karg.eventTypes;
1565
1566         return 0;
1567 }
1568
1569 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1570 static int
1571 mptctl_eventreport (unsigned long arg)
1572 {
1573         struct mpt_ioctl_eventreport __user *uarg = (void __user *) arg;
1574         struct mpt_ioctl_eventreport     karg;
1575         MPT_ADAPTER              *ioc;
1576         int                      iocnum;
1577         int                      numBytes, maxEvents, max;
1578
1579         dctlprintk(("mptctl_eventreport called.\n"));
1580         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventreport))) {
1581                 printk(KERN_ERR "%s@%d::mptctl_eventreport - "
1582                         "Unable to read in mpt_ioctl_eventreport struct @ %p\n",
1583                                 __FILE__, __LINE__, uarg);
1584                 return -EFAULT;
1585         }
1586
1587         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1588             (ioc == NULL)) {
1589                 dctlprintk((KERN_ERR "%s::mptctl_eventreport() @%d - ioc%d not found!\n",
1590                                 __FILE__, __LINE__, iocnum));
1591                 return -ENODEV;
1592         }
1593
1594         numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1595         maxEvents = numBytes/sizeof(MPT_IOCTL_EVENTS);
1596
1597
1598         max = MPTCTL_EVENT_LOG_SIZE < maxEvents ? MPTCTL_EVENT_LOG_SIZE : maxEvents;
1599
1600         /* If fewer than 1 event is requested, there must have
1601          * been some type of error.
1602          */
1603         if ((max < 1) || !ioc->events)
1604                 return -ENODATA;
1605
1606         /* Copy the data from kernel memory to user memory
1607          */
1608         numBytes = max * sizeof(MPT_IOCTL_EVENTS);
1609         if (copy_to_user(uarg->eventData, ioc->events, numBytes)) {
1610                 printk(KERN_ERR "%s@%d::mptctl_eventreport - "
1611                         "Unable to write out mpt_ioctl_eventreport struct @ %p\n",
1612                                 __FILE__, __LINE__, ioc->events);
1613                 return -EFAULT;
1614         }
1615
1616         return 0;
1617 }
1618
1619 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1620 static int
1621 mptctl_replace_fw (unsigned long arg)
1622 {
1623         struct mpt_ioctl_replace_fw __user *uarg = (void __user *) arg;
1624         struct mpt_ioctl_replace_fw      karg;
1625         MPT_ADAPTER              *ioc;
1626         int                      iocnum;
1627         int                      newFwSize;
1628
1629         dctlprintk(("mptctl_replace_fw called.\n"));
1630         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_replace_fw))) {
1631                 printk(KERN_ERR "%s@%d::mptctl_replace_fw - "
1632                         "Unable to read in mpt_ioctl_replace_fw struct @ %p\n",
1633                                 __FILE__, __LINE__, uarg);
1634                 return -EFAULT;
1635         }
1636
1637         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1638             (ioc == NULL)) {
1639                 dctlprintk((KERN_ERR "%s::mptctl_replace_fw() @%d - ioc%d not found!\n",
1640                                 __FILE__, __LINE__, iocnum));
1641                 return -ENODEV;
1642         }
1643
1644         /* If caching FW, Free the old FW image
1645          */
1646         if (ioc->cached_fw == NULL)
1647                 return 0;
1648
1649         mpt_free_fw_memory(ioc);
1650
1651         /* Allocate memory for the new FW image
1652          */
1653         newFwSize = karg.newImageSize;
1654
1655         if (newFwSize & 0x01)
1656                 newFwSize += 1;
1657         if (newFwSize & 0x02)
1658                 newFwSize += 2;
1659
1660         mpt_alloc_fw_memory(ioc, newFwSize);
1661         if (ioc->cached_fw == NULL)
1662                 return -ENOMEM;
1663
1664         /* Copy the data from user memory to kernel space
1665          */
1666         if (copy_from_user(ioc->cached_fw, uarg->newImage, newFwSize)) {
1667                 printk(KERN_ERR "%s@%d::mptctl_replace_fw - "
1668                                 "Unable to read in mpt_ioctl_replace_fw image "
1669                                 "@ %p\n", __FILE__, __LINE__, uarg);
1670                 mpt_free_fw_memory(ioc);
1671                 return -EFAULT;
1672         }
1673
1674         /* Update IOCFactsReply
1675          */
1676         ioc->facts.FWImageSize = newFwSize;
1677         return 0;
1678 }
1679
1680 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1681 /* MPT IOCTL MPTCOMMAND function.
1682  * Cast the arg into the mpt_ioctl_mpt_command structure.
1683  *
1684  * Outputs:     None.
1685  * Return:      0 if successful
1686  *              -EBUSY  if previous command timout and IOC reset is not complete.
1687  *              -EFAULT if data unavailable
1688  *              -ENODEV if no such device/adapter
1689  *              -ETIME  if timer expires
1690  *              -ENOMEM if memory allocation error
1691  */
1692 static int
1693 mptctl_mpt_command (unsigned long arg)
1694 {
1695         struct mpt_ioctl_command __user *uarg = (void __user *) arg;
1696         struct mpt_ioctl_command  karg;
1697         MPT_ADAPTER     *ioc;
1698         int             iocnum;
1699         int             rc;
1700
1701         dctlprintk(("mptctl_command called.\n"));
1702
1703         if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_command))) {
1704                 printk(KERN_ERR "%s@%d::mptctl_mpt_command - "
1705                         "Unable to read in mpt_ioctl_command struct @ %p\n",
1706                                 __FILE__, __LINE__, uarg);
1707                 return -EFAULT;
1708         }
1709
1710         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1711             (ioc == NULL)) {
1712                 dctlprintk((KERN_ERR "%s::mptctl_mpt_command() @%d - ioc%d not found!\n",
1713                                 __FILE__, __LINE__, iocnum));
1714                 return -ENODEV;
1715         }
1716
1717         rc = mptctl_do_mpt_command (karg, &uarg->MF);
1718
1719         return rc;
1720 }
1721
1722 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1723 /* Worker routine for the IOCTL MPTCOMMAND and MPTCOMMAND32 (sparc) commands.
1724  *
1725  * Outputs:     None.
1726  * Return:      0 if successful
1727  *              -EBUSY  if previous command timout and IOC reset is not complete.
1728  *              -EFAULT if data unavailable
1729  *              -ENODEV if no such device/adapter
1730  *              -ETIME  if timer expires
1731  *              -ENOMEM if memory allocation error
1732  *              -EPERM if SCSI I/O and target is untagged
1733  */
1734 static int
1735 mptctl_do_mpt_command (struct mpt_ioctl_command karg, void __user *mfPtr)
1736 {
1737         MPT_ADAPTER     *ioc;
1738         MPT_FRAME_HDR   *mf = NULL;
1739         MPIHeader_t     *hdr;
1740         char            *psge;
1741         struct buflist  bufIn;  /* data In buffer */
1742         struct buflist  bufOut; /* data Out buffer */
1743         dma_addr_t      dma_addr_in;
1744         dma_addr_t      dma_addr_out;
1745         int             sgSize = 0;     /* Num SG elements */
1746         int             iocnum, flagsLength;
1747         int             sz, rc = 0;
1748         int             msgContext;
1749         u16             req_idx;
1750         ulong           timeout;
1751
1752         dctlprintk(("mptctl_do_mpt_command called.\n"));
1753         bufIn.kptr = bufOut.kptr = NULL;
1754
1755         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1756             (ioc == NULL)) {
1757                 dctlprintk((KERN_ERR "%s::mptctl_do_mpt_command() @%d - ioc%d not found!\n",
1758                                 __FILE__, __LINE__, iocnum));
1759                 return -ENODEV;
1760         }
1761         if (!ioc->ioctl) {
1762                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1763                         "No memory available during driver init.\n",
1764                                 __FILE__, __LINE__);
1765                 return -ENOMEM;
1766         } else if (ioc->ioctl->status & MPT_IOCTL_STATUS_DID_IOCRESET) {
1767                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1768                         "Busy with IOC Reset \n", __FILE__, __LINE__);
1769                 return -EBUSY;
1770         }
1771
1772         /* Verify that the final request frame will not be too large.
1773          */
1774         sz = karg.dataSgeOffset * 4;
1775         if (karg.dataInSize > 0)
1776                 sz += sizeof(dma_addr_t) + sizeof(u32);
1777         if (karg.dataOutSize > 0)
1778                 sz += sizeof(dma_addr_t) + sizeof(u32);
1779
1780         if (sz > ioc->req_sz) {
1781                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1782                         "Request frame too large (%d) maximum (%d)\n",
1783                                 __FILE__, __LINE__, sz, ioc->req_sz);
1784                 return -EFAULT;
1785         }
1786
1787         /* Get a free request frame and save the message context.
1788          */
1789         if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL)
1790                 return -EAGAIN;
1791
1792         hdr = (MPIHeader_t *) mf;
1793         msgContext = le32_to_cpu(hdr->MsgContext);
1794         req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
1795
1796         /* Copy the request frame
1797          * Reset the saved message context.
1798          * Request frame in user space
1799          */
1800         if (copy_from_user(mf, mfPtr, karg.dataSgeOffset * 4)) {
1801                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1802                         "Unable to read MF from mpt_ioctl_command struct @ %p\n",
1803                         __FILE__, __LINE__, mfPtr);
1804                 rc = -EFAULT;
1805                 goto done_free_mem;
1806         }
1807         hdr->MsgContext = cpu_to_le32(msgContext);
1808
1809
1810         /* Verify that this request is allowed.
1811          */
1812         switch (hdr->Function) {
1813         case MPI_FUNCTION_IOC_FACTS:
1814         case MPI_FUNCTION_PORT_FACTS:
1815                 karg.dataOutSize  = karg.dataInSize = 0;
1816                 break;
1817
1818         case MPI_FUNCTION_CONFIG:
1819         case MPI_FUNCTION_FC_COMMON_TRANSPORT_SEND:
1820         case MPI_FUNCTION_FC_EX_LINK_SRVC_SEND:
1821         case MPI_FUNCTION_FW_UPLOAD:
1822         case MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
1823         case MPI_FUNCTION_FW_DOWNLOAD:
1824         case MPI_FUNCTION_FC_PRIMITIVE_SEND:
1825         case MPI_FUNCTION_TOOLBOX:
1826         case MPI_FUNCTION_SAS_IO_UNIT_CONTROL:
1827                 break;
1828
1829         case MPI_FUNCTION_SCSI_IO_REQUEST:
1830                 if (ioc->sh) {
1831                         SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
1832                         VirtTarget      *pTarget = NULL;
1833                         MPT_SCSI_HOST   *hd = NULL;
1834                         int qtag = MPI_SCSIIO_CONTROL_UNTAGGED;
1835                         int scsidir = 0;
1836                         int target = (int) pScsiReq->TargetID;
1837                         int dataSize;
1838
1839                         if ((target < 0) || (target >= ioc->sh->max_id)) {
1840                                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1841                                         "Target ID out of bounds. \n",
1842                                         __FILE__, __LINE__);
1843                                 rc = -ENODEV;
1844                                 goto done_free_mem;
1845                         }
1846
1847                         pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
1848                         pScsiReq->MsgFlags |= mpt_msg_flags();
1849
1850
1851                         /* verify that app has not requested
1852                          *      more sense data than driver
1853                          *      can provide, if so, reset this parameter
1854                          * set the sense buffer pointer low address
1855                          * update the control field to specify Q type
1856                          */
1857                         if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1858                                 pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1859                         else
1860                                 pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1861
1862                         pScsiReq->SenseBufferLowAddr =
1863                                 cpu_to_le32(ioc->sense_buf_low_dma
1864                                    + (req_idx * MPT_SENSE_BUFFER_ALLOC));
1865
1866                         if ((hd = (MPT_SCSI_HOST *) ioc->sh->hostdata)) {
1867                                 if (hd->Targets)
1868                                         pTarget = hd->Targets[target];
1869                         }
1870
1871                         if (pTarget &&(pTarget->tflags & MPT_TARGET_FLAGS_Q_YES))
1872                                 qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1873
1874                         /* Have the IOCTL driver set the direction based
1875                          * on the dataOutSize (ordering issue with Sparc).
1876                          */
1877                         if (karg.dataOutSize > 0) {
1878                                 scsidir = MPI_SCSIIO_CONTROL_WRITE;
1879                                 dataSize = karg.dataOutSize;
1880                         } else {
1881                                 scsidir = MPI_SCSIIO_CONTROL_READ;
1882                                 dataSize = karg.dataInSize;
1883                         }
1884
1885                         pScsiReq->Control = cpu_to_le32(scsidir | qtag);
1886                         pScsiReq->DataLength = cpu_to_le32(dataSize);
1887
1888                         ioc->ioctl->reset = MPTCTL_RESET_OK;
1889                         ioc->ioctl->target = target;
1890
1891                 } else {
1892                         printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1893                                 "SCSI driver is not loaded. \n",
1894                                         __FILE__, __LINE__);
1895                         rc = -EFAULT;
1896                         goto done_free_mem;
1897                 }
1898                 break;
1899
1900         case MPI_FUNCTION_SMP_PASSTHROUGH:
1901                 /* Check mf->PassthruFlags to determine if
1902                  * transfer is ImmediateMode or not.
1903                  * Immediate mode returns data in the ReplyFrame.
1904                  * Else, we are sending request and response data
1905                  * in two SGLs at the end of the mf.
1906                  */
1907                 break;
1908
1909         case MPI_FUNCTION_SATA_PASSTHROUGH:
1910                 if (!ioc->sh) {
1911                         printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1912                                 "SCSI driver is not loaded. \n",
1913                                         __FILE__, __LINE__);
1914                         rc = -EFAULT;
1915                         goto done_free_mem;
1916                 }
1917                 break;
1918
1919         case MPI_FUNCTION_RAID_ACTION:
1920                 /* Just add a SGE
1921                  */
1922                 break;
1923
1924         case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1925                 if (ioc->sh) {
1926                         SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
1927                         int qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1928                         int scsidir = MPI_SCSIIO_CONTROL_READ;
1929                         int dataSize;
1930
1931                         pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
1932                         pScsiReq->MsgFlags |= mpt_msg_flags();
1933
1934
1935                         /* verify that app has not requested
1936                          *      more sense data than driver
1937                          *      can provide, if so, reset this parameter
1938                          * set the sense buffer pointer low address
1939                          * update the control field to specify Q type
1940                          */
1941                         if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1942                                 pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1943                         else
1944                                 pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1945
1946                         pScsiReq->SenseBufferLowAddr =
1947                                 cpu_to_le32(ioc->sense_buf_low_dma
1948                                    + (req_idx * MPT_SENSE_BUFFER_ALLOC));
1949
1950                         /* All commands to physical devices are tagged
1951                          */
1952
1953                         /* Have the IOCTL driver set the direction based
1954                          * on the dataOutSize (ordering issue with Sparc).
1955                          */
1956                         if (karg.dataOutSize > 0) {
1957                                 scsidir = MPI_SCSIIO_CONTROL_WRITE;
1958                                 dataSize = karg.dataOutSize;
1959                         } else {
1960                                 scsidir = MPI_SCSIIO_CONTROL_READ;
1961                                 dataSize = karg.dataInSize;
1962                         }
1963
1964                         pScsiReq->Control = cpu_to_le32(scsidir | qtag);
1965                         pScsiReq->DataLength = cpu_to_le32(dataSize);
1966
1967                         ioc->ioctl->reset = MPTCTL_RESET_OK;
1968                         ioc->ioctl->target = pScsiReq->TargetID;
1969                 } else {
1970                         printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1971                                 "SCSI driver is not loaded. \n",
1972                                         __FILE__, __LINE__);
1973                         rc = -EFAULT;
1974                         goto done_free_mem;
1975                 }
1976                 break;
1977
1978         case MPI_FUNCTION_SCSI_TASK_MGMT:
1979                 {
1980                         MPT_SCSI_HOST *hd = NULL;
1981                         if ((ioc->sh == NULL) || ((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL)) {
1982                                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
1983                                         "SCSI driver not loaded or SCSI host not found. \n",
1984                                         __FILE__, __LINE__);
1985                                 rc = -EFAULT;
1986                                 goto done_free_mem;
1987                         } else if (mptctl_set_tm_flags(hd) != 0) {
1988                                 rc = -EPERM;
1989                                 goto done_free_mem;
1990                         }
1991                 }
1992                 break;
1993
1994         case MPI_FUNCTION_IOC_INIT:
1995                 {
1996                         IOCInit_t       *pInit = (IOCInit_t *) mf;
1997                         u32             high_addr, sense_high;
1998
1999                         /* Verify that all entries in the IOC INIT match
2000                          * existing setup (and in LE format).
2001                          */
2002                         if (sizeof(dma_addr_t) == sizeof(u64)) {
2003                                 high_addr = cpu_to_le32((u32)((u64)ioc->req_frames_dma >> 32));
2004                                 sense_high= cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
2005                         } else {
2006                                 high_addr = 0;
2007                                 sense_high= 0;
2008                         }
2009
2010                         if ((pInit->Flags != 0) || (pInit->MaxDevices != ioc->facts.MaxDevices) ||
2011                                 (pInit->MaxBuses != ioc->facts.MaxBuses) ||
2012                                 (pInit->ReplyFrameSize != cpu_to_le16(ioc->reply_sz)) ||
2013                                 (pInit->HostMfaHighAddr != high_addr) ||
2014                                 (pInit->SenseBufferHighAddr != sense_high)) {
2015                                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2016                                         "IOC_INIT issued with 1 or more incorrect parameters. Rejected.\n",
2017                                         __FILE__, __LINE__);
2018                                 rc = -EFAULT;
2019                                 goto done_free_mem;
2020                         }
2021                 }
2022                 break;
2023         default:
2024                 /*
2025                  * MPI_FUNCTION_PORT_ENABLE
2026                  * MPI_FUNCTION_TARGET_CMD_BUFFER_POST
2027                  * MPI_FUNCTION_TARGET_ASSIST
2028                  * MPI_FUNCTION_TARGET_STATUS_SEND
2029                  * MPI_FUNCTION_TARGET_MODE_ABORT
2030                  * MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET
2031                  * MPI_FUNCTION_IO_UNIT_RESET
2032                  * MPI_FUNCTION_HANDSHAKE
2033                  * MPI_FUNCTION_REPLY_FRAME_REMOVAL
2034                  * MPI_FUNCTION_EVENT_NOTIFICATION
2035                  *  (driver handles event notification)
2036                  * MPI_FUNCTION_EVENT_ACK
2037                  */
2038
2039                 /*  What to do with these???  CHECK ME!!!
2040                         MPI_FUNCTION_FC_LINK_SRVC_BUF_POST
2041                         MPI_FUNCTION_FC_LINK_SRVC_RSP
2042                         MPI_FUNCTION_FC_ABORT
2043                         MPI_FUNCTION_LAN_SEND
2044                         MPI_FUNCTION_LAN_RECEIVE
2045                         MPI_FUNCTION_LAN_RESET
2046                 */
2047
2048                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2049                         "Illegal request (function 0x%x) \n",
2050                         __FILE__, __LINE__, hdr->Function);
2051                 rc = -EFAULT;
2052                 goto done_free_mem;
2053         }
2054
2055         /* Add the SGL ( at most one data in SGE and one data out SGE )
2056          * In the case of two SGE's - the data out (write) will always
2057          * preceede the data in (read) SGE. psgList is used to free the
2058          * allocated memory.
2059          */
2060         psge = (char *) (((int *) mf) + karg.dataSgeOffset);
2061         flagsLength = 0;
2062
2063         /* bufIn and bufOut are used for user to kernel space transfers
2064          */
2065         bufIn.kptr = bufOut.kptr = NULL;
2066         bufIn.len = bufOut.len = 0;
2067
2068         if (karg.dataOutSize > 0)
2069                 sgSize ++;
2070
2071         if (karg.dataInSize > 0)
2072                 sgSize ++;
2073
2074         if (sgSize > 0) {
2075
2076                 /* Set up the dataOut memory allocation */
2077                 if (karg.dataOutSize > 0) {
2078                         if (karg.dataInSize > 0) {
2079                                 flagsLength = ( MPI_SGE_FLAGS_SIMPLE_ELEMENT |
2080                                                 MPI_SGE_FLAGS_END_OF_BUFFER |
2081                                                 MPI_SGE_FLAGS_DIRECTION |
2082                                                 mpt_addr_size() )
2083                                                 << MPI_SGE_FLAGS_SHIFT;
2084                         } else {
2085                                 flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE;
2086                         }
2087                         flagsLength |= karg.dataOutSize;
2088                         bufOut.len = karg.dataOutSize;
2089                         bufOut.kptr = pci_alloc_consistent(
2090                                         ioc->pcidev, bufOut.len, &dma_addr_out);
2091
2092                         if (bufOut.kptr == NULL) {
2093                                 rc = -ENOMEM;
2094                                 goto done_free_mem;
2095                         } else {
2096                                 /* Set up this SGE.
2097                                  * Copy to MF and to sglbuf
2098                                  */
2099                                 mpt_add_sge(psge, flagsLength, dma_addr_out);
2100                                 psge += (sizeof(u32) + sizeof(dma_addr_t));
2101
2102                                 /* Copy user data to kernel space.
2103                                  */
2104                                 if (copy_from_user(bufOut.kptr,
2105                                                 karg.dataOutBufPtr,
2106                                                 bufOut.len)) {
2107                                         printk(KERN_ERR
2108                                                 "%s@%d::mptctl_do_mpt_command - Unable "
2109                                                 "to read user data "
2110                                                 "struct @ %p\n",
2111                                                 __FILE__, __LINE__,karg.dataOutBufPtr);
2112                                         rc =  -EFAULT;
2113                                         goto done_free_mem;
2114                                 }
2115                         }
2116                 }
2117
2118                 if (karg.dataInSize > 0) {
2119                         flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
2120                         flagsLength |= karg.dataInSize;
2121
2122                         bufIn.len = karg.dataInSize;
2123                         bufIn.kptr = pci_alloc_consistent(ioc->pcidev,
2124                                         bufIn.len, &dma_addr_in);
2125
2126                         if (bufIn.kptr == NULL) {
2127                                 rc = -ENOMEM;
2128                                 goto done_free_mem;
2129                         } else {
2130                                 /* Set up this SGE
2131                                  * Copy to MF and to sglbuf
2132                                  */
2133                                 mpt_add_sge(psge, flagsLength, dma_addr_in);
2134                         }
2135                 }
2136         } else  {
2137                 /* Add a NULL SGE
2138                  */
2139                 mpt_add_sge(psge, flagsLength, (dma_addr_t) -1);
2140         }
2141
2142         ioc->ioctl->wait_done = 0;
2143         if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT) {
2144
2145                 DBG_DUMP_TM_REQUEST_FRAME((u32 *)mf);
2146
2147                 if (mpt_send_handshake_request(mptctl_id, ioc,
2148                         sizeof(SCSITaskMgmt_t), (u32*)mf,
2149                         CAN_SLEEP) != 0) {
2150                         dfailprintk((MYIOC_s_ERR_FMT "_send_handshake FAILED!"
2151                                 " (ioc %p, mf %p) \n", ioc->name,
2152                                 ioc, mf));
2153                         mptctl_free_tm_flags(ioc);
2154                         rc = -ENODATA;
2155                         goto done_free_mem;
2156                 }
2157
2158         } else
2159                 mpt_put_msg_frame(mptctl_id, ioc, mf);
2160
2161         /* Now wait for the command to complete */
2162         timeout = (karg.timeout > 0) ? karg.timeout : MPT_IOCTL_DEFAULT_TIMEOUT;
2163         timeout = wait_event_timeout(mptctl_wait,
2164              ioc->ioctl->wait_done == 1,
2165              HZ*timeout);
2166
2167         if(timeout <=0 && (ioc->ioctl->wait_done != 1 )) {
2168         /* Now we need to reset the board */
2169
2170                 if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT)
2171                         mptctl_free_tm_flags(ioc);
2172
2173                 mptctl_timeout_expired(ioc->ioctl);
2174                 rc = -ENODATA;
2175                 goto done_free_mem;
2176         }
2177
2178         mf = NULL;
2179
2180         /* If a valid reply frame, copy to the user.
2181          * Offset 2: reply length in U32's
2182          */
2183         if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID) {
2184                 if (karg.maxReplyBytes < ioc->reply_sz) {
2185                          sz = min(karg.maxReplyBytes, 4*ioc->ioctl->ReplyFrame[2]);
2186                 } else {
2187                          sz = min(ioc->reply_sz, 4*ioc->ioctl->ReplyFrame[2]);
2188                 }
2189
2190                 if (sz > 0) {
2191                         if (copy_to_user(karg.replyFrameBufPtr,
2192                                  &ioc->ioctl->ReplyFrame, sz)){
2193                                  printk(KERN_ERR
2194                                      "%s@%d::mptctl_do_mpt_command - "
2195                                  "Unable to write out reply frame %p\n",
2196                                  __FILE__, __LINE__, karg.replyFrameBufPtr);
2197                                  rc =  -ENODATA;
2198                                  goto done_free_mem;
2199                         }
2200                 }
2201         }
2202
2203         /* If valid sense data, copy to user.
2204          */
2205         if (ioc->ioctl->status & MPT_IOCTL_STATUS_SENSE_VALID) {
2206                 sz = min(karg.maxSenseBytes, MPT_SENSE_BUFFER_SIZE);
2207                 if (sz > 0) {
2208                         if (copy_to_user(karg.senseDataPtr, ioc->ioctl->sense, sz)) {
2209                                 printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2210                                 "Unable to write sense data to user %p\n",
2211                                 __FILE__, __LINE__,
2212                                 karg.senseDataPtr);
2213                                 rc =  -ENODATA;
2214                                 goto done_free_mem;
2215                         }
2216                 }
2217         }
2218
2219         /* If the overall status is _GOOD and data in, copy data
2220          * to user.
2221          */
2222         if ((ioc->ioctl->status & MPT_IOCTL_STATUS_COMMAND_GOOD) &&
2223                                 (karg.dataInSize > 0) && (bufIn.kptr)) {
2224
2225                 if (copy_to_user(karg.dataInBufPtr,
2226                                  bufIn.kptr, karg.dataInSize)) {
2227                         printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
2228                                 "Unable to write data to user %p\n",
2229                                 __FILE__, __LINE__,
2230                                 karg.dataInBufPtr);
2231                         rc =  -ENODATA;
2232                 }
2233         }
2234
2235 done_free_mem:
2236
2237         ioc->ioctl->status &= ~(MPT_IOCTL_STATUS_COMMAND_GOOD |
2238                 MPT_IOCTL_STATUS_SENSE_VALID |
2239                 MPT_IOCTL_STATUS_RF_VALID );
2240
2241         /* Free the allocated memory.
2242          */
2243         if (bufOut.kptr != NULL) {
2244                 pci_free_consistent(ioc->pcidev,
2245                         bufOut.len, (void *) bufOut.kptr, dma_addr_out);
2246         }
2247
2248         if (bufIn.kptr != NULL) {
2249                 pci_free_consistent(ioc->pcidev,
2250                         bufIn.len, (void *) bufIn.kptr, dma_addr_in);
2251         }
2252
2253         /* mf is null if command issued successfully
2254          * otherwise, failure occured after mf acquired.
2255          */
2256         if (mf)
2257                 mpt_free_msg_frame(ioc, mf);
2258
2259         return rc;
2260 }
2261
2262 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2263 /* Prototype Routine for the HP HOST INFO command.
2264  *
2265  * Outputs:     None.
2266  * Return:      0 if successful
2267  *              -EFAULT if data unavailable
2268  *              -EBUSY  if previous command timout and IOC reset is not complete.
2269  *              -ENODEV if no such device/adapter
2270  *              -ETIME  if timer expires
2271  *              -ENOMEM if memory allocation error
2272  */
2273 static int
2274 mptctl_hp_hostinfo(unsigned long arg, unsigned int data_size)
2275 {
2276         hp_host_info_t  __user *uarg = (void __user *) arg;
2277         MPT_ADAPTER             *ioc;
2278         struct pci_dev          *pdev;
2279         char                    *pbuf=NULL;
2280         dma_addr_t              buf_dma;
2281         hp_host_info_t          karg;
2282         CONFIGPARMS             cfg;
2283         ConfigPageHeader_t      hdr;
2284         int                     iocnum;
2285         int                     rc, cim_rev;
2286         ToolboxIstwiReadWriteRequest_t  *IstwiRWRequest;
2287         MPT_FRAME_HDR           *mf = NULL;
2288         MPIHeader_t             *mpi_hdr;
2289
2290         dctlprintk((": mptctl_hp_hostinfo called.\n"));
2291         /* Reset long to int. Should affect IA64 and SPARC only
2292          */
2293         if (data_size == sizeof(hp_host_info_t))
2294                 cim_rev = 1;
2295         else if (data_size == sizeof(hp_host_info_rev0_t))
2296                 cim_rev = 0;    /* obsolete */
2297         else
2298                 return -EFAULT;
2299
2300         if (copy_from_user(&karg, uarg, sizeof(hp_host_info_t))) {
2301                 printk(KERN_ERR "%s@%d::mptctl_hp_host_info - "
2302                         "Unable to read in hp_host_info struct @ %p\n",
2303                                 __FILE__, __LINE__, uarg);
2304                 return -EFAULT;
2305         }
2306
2307         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2308             (ioc == NULL)) {
2309                 dctlprintk((KERN_ERR "%s::mptctl_hp_hostinfo() @%d - ioc%d not found!\n",
2310                                 __FILE__, __LINE__, iocnum));
2311                 return -ENODEV;
2312         }
2313
2314         /* Fill in the data and return the structure to the calling
2315          * program
2316          */
2317         pdev = (struct pci_dev *) ioc->pcidev;
2318
2319         karg.vendor = pdev->vendor;
2320         karg.device = pdev->device;
2321         karg.subsystem_id = pdev->subsystem_device;
2322         karg.subsystem_vendor = pdev->subsystem_vendor;
2323         karg.devfn = pdev->devfn;
2324         karg.bus = pdev->bus->number;
2325
2326         /* Save the SCSI host no. if
2327          * SCSI driver loaded
2328          */
2329         if (ioc->sh != NULL)
2330                 karg.host_no = ioc->sh->host_no;
2331         else
2332                 karg.host_no =  -1;
2333
2334         /* Reformat the fw_version into a string
2335          */
2336         karg.fw_version[0] = ioc->facts.FWVersion.Struct.Major >= 10 ?
2337                 ((ioc->facts.FWVersion.Struct.Major / 10) + '0') : '0';
2338         karg.fw_version[1] = (ioc->facts.FWVersion.Struct.Major % 10 ) + '0';
2339         karg.fw_version[2] = '.';
2340         karg.fw_version[3] = ioc->facts.FWVersion.Struct.Minor >= 10 ?
2341                 ((ioc->facts.FWVersion.Struct.Minor / 10) + '0') : '0';
2342         karg.fw_version[4] = (ioc->facts.FWVersion.Struct.Minor % 10 ) + '0';
2343         karg.fw_version[5] = '.';
2344         karg.fw_version[6] = ioc->facts.FWVersion.Struct.Unit >= 10 ?
2345                 ((ioc->facts.FWVersion.Struct.Unit / 10) + '0') : '0';
2346         karg.fw_version[7] = (ioc->facts.FWVersion.Struct.Unit % 10 ) + '0';
2347         karg.fw_version[8] = '.';
2348         karg.fw_version[9] = ioc->facts.FWVersion.Struct.Dev >= 10 ?
2349                 ((ioc->facts.FWVersion.Struct.Dev / 10) + '0') : '0';
2350         karg.fw_version[10] = (ioc->facts.FWVersion.Struct.Dev % 10 ) + '0';
2351         karg.fw_version[11] = '\0';
2352
2353         /* Issue a config request to get the device serial number
2354          */
2355         hdr.PageVersion = 0;
2356         hdr.PageLength = 0;
2357         hdr.PageNumber = 0;
2358         hdr.PageType = MPI_CONFIG_PAGETYPE_MANUFACTURING;
2359         cfg.cfghdr.hdr = &hdr;
2360         cfg.physAddr = -1;
2361         cfg.pageAddr = 0;
2362         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2363         cfg.dir = 0;    /* read */
2364         cfg.timeout = 10;
2365
2366         strncpy(karg.serial_number, " ", 24);
2367         if (mpt_config(ioc, &cfg) == 0) {
2368                 if (cfg.cfghdr.hdr->PageLength > 0) {
2369                         /* Issue the second config page request */
2370                         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2371
2372                         pbuf = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4, &buf_dma);
2373                         if (pbuf) {
2374                                 cfg.physAddr = buf_dma;
2375                                 if (mpt_config(ioc, &cfg) == 0) {
2376                                         ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;
2377                                         if (strlen(pdata->BoardTracerNumber) > 1) {
2378                                                 strncpy(karg.serial_number,                                                                         pdata->BoardTracerNumber, 24);
2379                                                 karg.serial_number[24-1]='\0';
2380                                         }
2381                                 }
2382                                 pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, pbuf, buf_dma);
2383                                 pbuf = NULL;
2384                         }
2385                 }
2386         }
2387         rc = mpt_GetIocState(ioc, 1);
2388         switch (rc) {
2389         case MPI_IOC_STATE_OPERATIONAL:
2390                 karg.ioc_status =  HP_STATUS_OK;
2391                 break;
2392
2393         case MPI_IOC_STATE_FAULT:
2394                 karg.ioc_status =  HP_STATUS_FAILED;
2395                 break;
2396
2397         case MPI_IOC_STATE_RESET:
2398         case MPI_IOC_STATE_READY:
2399         default:
2400                 karg.ioc_status =  HP_STATUS_OTHER;
2401                 break;
2402         }
2403
2404         karg.base_io_addr = pci_resource_start(pdev, 0);
2405
2406         if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
2407                 karg.bus_phys_width = HP_BUS_WIDTH_UNK;
2408         else
2409                 karg.bus_phys_width = HP_BUS_WIDTH_16;
2410
2411         karg.hard_resets = 0;
2412         karg.soft_resets = 0;
2413         karg.timeouts = 0;
2414         if (ioc->sh != NULL) {
2415                 MPT_SCSI_HOST *hd =  (MPT_SCSI_HOST *)ioc->sh->hostdata;
2416
2417                 if (hd && (cim_rev == 1)) {
2418                         karg.hard_resets = hd->hard_resets;
2419                         karg.soft_resets = hd->soft_resets;
2420                         karg.timeouts = hd->timeouts;
2421                 }
2422         }
2423
2424         /* 
2425          * Gather ISTWI(Industry Standard Two Wire Interface) Data
2426          */
2427         if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL) {
2428                 dfailprintk((MYIOC_s_WARN_FMT "%s, no msg frames!!\n",
2429                     ioc->name,__FUNCTION__));
2430                 goto out;
2431         }
2432
2433         IstwiRWRequest = (ToolboxIstwiReadWriteRequest_t *)mf;
2434         mpi_hdr = (MPIHeader_t *) mf;
2435         memset(IstwiRWRequest,0,sizeof(ToolboxIstwiReadWriteRequest_t));
2436         IstwiRWRequest->Function = MPI_FUNCTION_TOOLBOX;
2437         IstwiRWRequest->Tool = MPI_TOOLBOX_ISTWI_READ_WRITE_TOOL;
2438         IstwiRWRequest->MsgContext = mpi_hdr->MsgContext;
2439         IstwiRWRequest->Flags = MPI_TB_ISTWI_FLAGS_READ;
2440         IstwiRWRequest->NumAddressBytes = 0x01;
2441         IstwiRWRequest->DataLength = cpu_to_le16(0x04);
2442         if (pdev->devfn & 1)
2443                 IstwiRWRequest->DeviceAddr = 0xB2;
2444         else
2445                 IstwiRWRequest->DeviceAddr = 0xB0;
2446
2447         pbuf = pci_alloc_consistent(ioc->pcidev, 4, &buf_dma);
2448         if (!pbuf)
2449                 goto out;
2450         mpt_add_sge((char *)&IstwiRWRequest->SGL,
2451             (MPT_SGE_FLAGS_SSIMPLE_READ|4), buf_dma);
2452
2453         ioc->ioctl->wait_done = 0;
2454         mpt_put_msg_frame(mptctl_id, ioc, mf);
2455
2456         rc = wait_event_timeout(mptctl_wait,
2457              ioc->ioctl->wait_done == 1,
2458              HZ*MPT_IOCTL_DEFAULT_TIMEOUT /* 10 sec */);
2459
2460         if(rc <=0 && (ioc->ioctl->wait_done != 1 )) {
2461                 /* 
2462                  * Now we need to reset the board
2463                  */
2464                 mpt_free_msg_frame(ioc, mf);
2465                 mptctl_timeout_expired(ioc->ioctl);
2466                 goto out;
2467         }
2468
2469         /* 
2470          *ISTWI Data Definition
2471          * pbuf[0] = FW_VERSION = 0x4
2472          * pbuf[1] = Bay Count = 6 or 4 or 2, depending on
2473          *  the config, you should be seeing one out of these three values
2474          * pbuf[2] = Drive Installed Map = bit pattern depend on which
2475          *   bays have drives in them
2476          * pbuf[3] = Checksum (0x100 = (byte0 + byte2 + byte3)
2477          */
2478         if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID)
2479                 karg.rsvd = *(u32 *)pbuf;
2480
2481  out:
2482         if (pbuf)
2483                 pci_free_consistent(ioc->pcidev, 4, pbuf, buf_dma);
2484
2485         /* Copy the data from kernel memory to user memory
2486          */
2487         if (copy_to_user((char __user *)arg, &karg, sizeof(hp_host_info_t))) {
2488                 printk(KERN_ERR "%s@%d::mptctl_hpgethostinfo - "
2489                         "Unable to write out hp_host_info @ %p\n",
2490                                 __FILE__, __LINE__, uarg);
2491                 return -EFAULT;
2492         }
2493
2494         return 0;
2495
2496 }
2497
2498 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2499 /* Prototype Routine for the HP TARGET INFO command.
2500  *
2501  * Outputs:     None.
2502  * Return:      0 if successful
2503  *              -EFAULT if data unavailable
2504  *              -EBUSY  if previous command timout and IOC reset is not complete.
2505  *              -ENODEV if no such device/adapter
2506  *              -ETIME  if timer expires
2507  *              -ENOMEM if memory allocation error
2508  */
2509 static int
2510 mptctl_hp_targetinfo(unsigned long arg)
2511 {
2512         hp_target_info_t __user *uarg = (void __user *) arg;
2513         SCSIDevicePage0_t       *pg0_alloc;
2514         SCSIDevicePage3_t       *pg3_alloc;
2515         MPT_ADAPTER             *ioc;
2516         MPT_SCSI_HOST           *hd = NULL;
2517         hp_target_info_t        karg;
2518         int                     iocnum;
2519         int                     data_sz;
2520         dma_addr_t              page_dma;
2521         CONFIGPARMS             cfg;
2522         ConfigPageHeader_t      hdr;
2523         int                     tmp, np, rc = 0;
2524
2525         dctlprintk((": mptctl_hp_targetinfo called.\n"));
2526         if (copy_from_user(&karg, uarg, sizeof(hp_target_info_t))) {
2527                 printk(KERN_ERR "%s@%d::mptctl_hp_targetinfo - "
2528                         "Unable to read in hp_host_targetinfo struct @ %p\n",
2529                                 __FILE__, __LINE__, uarg);
2530                 return -EFAULT;
2531         }
2532
2533         if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2534                 (ioc == NULL)) {
2535                 dctlprintk((KERN_ERR "%s::mptctl_hp_targetinfo() @%d - ioc%d not found!\n",
2536                                 __FILE__, __LINE__, iocnum));
2537                 return -ENODEV;
2538         }
2539
2540         /*  There is nothing to do for FCP parts.
2541          */
2542         if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
2543                 return 0;
2544
2545         if ((ioc->spi_data.sdp0length == 0) || (ioc->sh == NULL))
2546                 return 0;
2547
2548         if (ioc->sh->host_no != karg.hdr.host)
2549                 return -ENODEV;
2550
2551        /* Get the data transfer speeds
2552         */
2553         data_sz = ioc->spi_data.sdp0length * 4;
2554         pg0_alloc = (SCSIDevicePage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page_dma);
2555         if (pg0_alloc) {
2556                 hdr.PageVersion = ioc->spi_data.sdp0version;
2557                 hdr.PageLength = data_sz;
2558                 hdr.PageNumber = 0;
2559                 hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2560
2561                 cfg.cfghdr.hdr = &hdr;
2562                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2563                 cfg.dir = 0;
2564                 cfg.timeout = 0;
2565                 cfg.physAddr = page_dma;
2566
2567                 cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2568
2569                 if ((rc = mpt_config(ioc, &cfg)) == 0) {
2570                         np = le32_to_cpu(pg0_alloc->NegotiatedParameters);
2571                         karg.negotiated_width = np & MPI_SCSIDEVPAGE0_NP_WIDE ?
2572                                         HP_BUS_WIDTH_16 : HP_BUS_WIDTH_8;
2573
2574                         if (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) {
2575                                 tmp = (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
2576                                 if (tmp < 0x09)
2577                                         karg.negotiated_speed = HP_DEV_SPEED_ULTRA320;
2578                                 else if (tmp <= 0x09)
2579                                         karg.negotiated_speed = HP_DEV_SPEED_ULTRA160;
2580                                 else if (tmp <= 0x0A)
2581                                         karg.negotiated_speed = HP_DEV_SPEED_ULTRA2;
2582                                 else if (tmp <= 0x0C)
2583                                         karg.negotiated_speed = HP_DEV_SPEED_ULTRA;
2584                                 else if (tmp <= 0x25)
2585                                         karg.negotiated_speed = HP_DEV_SPEED_FAST;
2586                                 else
2587                                         karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2588                         } else
2589                                 karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2590                 }
2591
2592                 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg0_alloc, page_dma);
2593         }
2594
2595         /* Set defaults
2596          */
2597         karg.message_rejects = -1;
2598         karg.phase_errors = -1;
2599         karg.parity_errors = -1;
2600         karg.select_timeouts = -1;
2601
2602         /* Get the target error parameters
2603          */
2604         hdr.PageVersion = 0;
2605         hdr.PageLength = 0;
2606         hdr.PageNumber = 3;
2607         hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2608
2609         cfg.cfghdr.hdr = &hdr;
2610         cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2611         cfg.dir = 0;
2612         cfg.timeout = 0;
2613         cfg.physAddr = -1;
2614         if ((mpt_config(ioc, &cfg) == 0) && (cfg.cfghdr.hdr->PageLength > 0)) {
2615                 /* Issue the second config page request */
2616                 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2617                 data_sz = (int) cfg.cfghdr.hdr->PageLength * 4;
2618                 pg3_alloc = (SCSIDevicePage3_t *) pci_alloc_consistent(
2619                                                         ioc->pcidev, data_sz, &page_dma);
2620                 if (pg3_alloc) {
2621                         cfg.physAddr = page_dma;
2622                         cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2623                         if ((rc = mpt_config(ioc, &cfg)) == 0) {
2624                                 karg.message_rejects = (u32) le16_to_cpu(pg3_alloc->MsgRejectCount);
2625                                 karg.phase_errors = (u32) le16_to_cpu(pg3_alloc->PhaseErrorCount);
2626                                 karg.parity_errors = (u32) le16_to_cpu(pg3_alloc->ParityErrorCount);
2627                         }
2628                         pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg3_alloc, page_dma);
2629                 }
2630         }
2631         hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
2632         if (hd != NULL)
2633                 karg.select_timeouts = hd->sel_timeout[karg.hdr.id];
2634
2635         /* Copy the data from kernel memory to user memory
2636          */
2637         if (copy_to_user((char __user *)arg, &karg, sizeof(hp_target_info_t))) {
2638                 printk(KERN_ERR "%s@%d::mptctl_hp_target_info - "
2639                         "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
2640                                 __FILE__, __LINE__, uarg);
2641                 return -EFAULT;
2642         }
2643
2644         return 0;
2645 }
2646
2647 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2648
2649 static struct file_operations mptctl_fops = {
2650         .owner =        THIS_MODULE,
2651         .llseek =       no_llseek,
2652         .unlocked_ioctl = mptctl_ioctl,
2653 #ifdef CONFIG_COMPAT
2654         .compat_ioctl = compat_mpctl_ioctl,
2655 #endif
2656 };
2657
2658 static struct miscdevice mptctl_miscdev = {
2659         MPT_MINOR,
2660         MYNAM,
2661         &mptctl_fops
2662 };
2663
2664 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2665
2666 #ifdef CONFIG_COMPAT
2667
2668 static int
2669 compat_mptfwxfer_ioctl(struct file *filp, unsigned int cmd,
2670                         unsigned long arg)
2671 {
2672         struct mpt_fw_xfer32 kfw32;
2673         struct mpt_fw_xfer kfw;
2674         MPT_ADAPTER *iocp = NULL;
2675         int iocnum, iocnumX;
2676         int nonblock = (filp->f_flags & O_NONBLOCK);
2677         int ret;
2678
2679         dctlprintk((KERN_INFO MYNAM "::compat_mptfwxfer_ioctl() called\n"));
2680
2681         if (copy_from_user(&kfw32, (char __user *)arg, sizeof(kfw32)))
2682                 return -EFAULT;
2683
2684         /* Verify intended MPT adapter */
2685         iocnumX = kfw32.iocnum & 0xFF;
2686         if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2687             (iocp == NULL)) {
2688                 dctlprintk((KERN_ERR MYNAM "::compat_mptfwxfer_ioctl @%d - ioc%d not found!\n",
2689                                 __LINE__, iocnumX));
2690                 return -ENODEV;
2691         }
2692
2693         if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2694                 return ret;
2695
2696         kfw.iocnum = iocnum;
2697         kfw.fwlen = kfw32.fwlen;
2698         kfw.bufp = compat_ptr(kfw32.bufp);
2699
2700         ret = mptctl_do_fw_download(kfw.iocnum, kfw.bufp, kfw.fwlen);
2701
2702         mutex_unlock(&iocp->ioctl->ioctl_mutex);
2703
2704         return ret;
2705 }
2706
2707 static int
2708 compat_mpt_command(struct file *filp, unsigned int cmd,
2709                         unsigned long arg)
2710 {
2711         struct mpt_ioctl_command32 karg32;
2712         struct mpt_ioctl_command32 __user *uarg = (struct mpt_ioctl_command32 __user *) arg;
2713         struct mpt_ioctl_command karg;
2714         MPT_ADAPTER *iocp = NULL;
2715         int iocnum, iocnumX;
2716         int nonblock = (filp->f_flags & O_NONBLOCK);
2717         int ret;
2718
2719         dctlprintk((KERN_INFO MYNAM "::compat_mpt_command() called\n"));
2720
2721         if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32)))
2722                 return -EFAULT;
2723
2724         /* Verify intended MPT adapter */
2725         iocnumX = karg32.hdr.iocnum & 0xFF;
2726         if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2727             (iocp == NULL)) {
2728                 dctlprintk((KERN_ERR MYNAM "::compat_mpt_command @%d - ioc%d not found!\n",
2729                                 __LINE__, iocnumX));
2730                 return -ENODEV;
2731         }
2732
2733         if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2734                 return ret;
2735
2736         /* Copy data to karg */
2737         karg.hdr.iocnum = karg32.hdr.iocnum;
2738         karg.hdr.port = karg32.hdr.port;
2739         karg.timeout = karg32.timeout;
2740         karg.maxReplyBytes = karg32.maxReplyBytes;
2741
2742         karg.dataInSize = karg32.dataInSize;
2743         karg.dataOutSize = karg32.dataOutSize;
2744         karg.maxSenseBytes = karg32.maxSenseBytes;
2745         karg.dataSgeOffset = karg32.dataSgeOffset;
2746
2747         karg.replyFrameBufPtr = (char __user *)(unsigned long)karg32.replyFrameBufPtr;
2748         karg.dataInBufPtr = (char __user *)(unsigned long)karg32.dataInBufPtr;
2749         karg.dataOutBufPtr = (char __user *)(unsigned long)karg32.dataOutBufPtr;
2750         karg.senseDataPtr = (char __user *)(unsigned long)karg32.senseDataPtr;
2751
2752         /* Pass new structure to do_mpt_command
2753          */
2754         ret = mptctl_do_mpt_command (karg, &uarg->MF);
2755
2756         mutex_unlock(&iocp->ioctl->ioctl_mutex);
2757
2758         return ret;
2759 }
2760
2761 static long compat_mpctl_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2762 {
2763         long ret;
2764         lock_kernel();
2765         switch (cmd) {
2766         case MPTIOCINFO:
2767         case MPTIOCINFO1:
2768         case MPTIOCINFO2:
2769         case MPTTARGETINFO:
2770         case MPTEVENTQUERY:
2771         case MPTEVENTENABLE:
2772         case MPTEVENTREPORT:
2773         case MPTHARDRESET:
2774         case HP_GETHOSTINFO:
2775         case HP_GETTARGETINFO:
2776         case MPTTEST:
2777                 ret = __mptctl_ioctl(f, cmd, arg);
2778                 break;
2779         case MPTCOMMAND32:
2780                 ret = compat_mpt_command(f, cmd, arg);
2781                 break;
2782         case MPTFWDOWNLOAD32:
2783                 ret = compat_mptfwxfer_ioctl(f, cmd, arg);
2784                 break;
2785         default:
2786                 ret = -ENOIOCTLCMD;
2787                 break;
2788         }
2789         unlock_kernel();
2790         return ret;
2791 }
2792
2793 #endif
2794
2795
2796 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2797 /*
2798  *      mptctl_probe - Installs ioctl devices per bus.
2799  *      @pdev: Pointer to pci_dev structure
2800  *
2801  *      Returns 0 for success, non-zero for failure.
2802  *
2803  */
2804
2805 static int
2806 mptctl_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2807 {
2808         int err;
2809         int sz;
2810         u8 *mem;
2811         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2812
2813         /*
2814          * Allocate and inite a MPT_IOCTL structure
2815         */
2816         sz = sizeof (MPT_IOCTL);
2817         mem = kmalloc(sz, GFP_KERNEL);
2818         if (mem == NULL) {
2819                 err = -ENOMEM;
2820                 goto out_fail;
2821         }
2822
2823         memset(mem, 0, sz);
2824         ioc->ioctl = (MPT_IOCTL *) mem;
2825         ioc->ioctl->ioc = ioc;
2826         mutex_init(&ioc->ioctl->ioctl_mutex);
2827         return 0;
2828
2829 out_fail:
2830
2831         mptctl_remove(pdev);
2832         return err;
2833 }
2834
2835 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2836 /*
2837  *      mptctl_remove - Removed ioctl devices
2838  *      @pdev: Pointer to pci_dev structure
2839  *
2840  *
2841  */
2842 static void
2843 mptctl_remove(struct pci_dev *pdev)
2844 {
2845         MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2846
2847         kfree ( ioc->ioctl );
2848 }
2849
2850 static struct mpt_pci_driver mptctl_driver = {
2851   .probe                = mptctl_probe,
2852   .remove               = mptctl_remove,
2853 };
2854
2855 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2856 static int __init mptctl_init(void)
2857 {
2858         int err;
2859         int where = 1;
2860
2861         show_mptmod_ver(my_NAME, my_VERSION);
2862
2863         if(mpt_device_driver_register(&mptctl_driver,
2864           MPTCTL_DRIVER) != 0 ) {
2865                 dprintk((KERN_INFO MYNAM
2866                 ": failed to register dd callbacks\n"));
2867         }
2868
2869         /* Register this device */
2870         err = misc_register(&mptctl_miscdev);
2871         if (err < 0) {
2872                 printk(KERN_ERR MYNAM ": Can't register misc device [minor=%d].\n", MPT_MINOR);
2873                 goto out_fail;
2874         }
2875         printk(KERN_INFO MYNAM ": Registered with Fusion MPT base driver\n");
2876         printk(KERN_INFO MYNAM ": /dev/%s @ (major,minor=%d,%d)\n",
2877                          mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2878
2879         /*
2880          *  Install our handler
2881          */
2882         ++where;
2883         if ((mptctl_id = mpt_register(mptctl_reply, MPTCTL_DRIVER)) < 0) {
2884                 printk(KERN_ERR MYNAM ": ERROR: Failed to register with Fusion MPT base driver\n");
2885                 misc_deregister(&mptctl_miscdev);
2886                 err = -EBUSY;
2887                 goto out_fail;
2888         }
2889
2890         if (mpt_reset_register(mptctl_id, mptctl_ioc_reset) == 0) {
2891                 dprintk((KERN_INFO MYNAM ": Registered for IOC reset notifications\n"));
2892         } else {
2893                 /* FIXME! */
2894         }
2895
2896         return 0;
2897
2898 out_fail:
2899
2900         mpt_device_driver_deregister(MPTCTL_DRIVER);
2901
2902         return err;
2903 }
2904
2905 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2906 static void mptctl_exit(void)
2907 {
2908         misc_deregister(&mptctl_miscdev);
2909         printk(KERN_INFO MYNAM ": Deregistered /dev/%s @ (major,minor=%d,%d)\n",
2910                          mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2911
2912         /* De-register reset handler from base module */
2913         mpt_reset_deregister(mptctl_id);
2914         dprintk((KERN_INFO MYNAM ": Deregistered for IOC reset notifications\n"));
2915
2916         /* De-register callback handler from base module */
2917         mpt_deregister(mptctl_id);
2918         printk(KERN_INFO MYNAM ": Deregistered from Fusion MPT base driver\n");
2919
2920         mpt_device_driver_deregister(MPTCTL_DRIVER);
2921
2922 }
2923
2924 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2925
2926 module_init(mptctl_init);
2927 module_exit(mptctl_exit);