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