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