blob: f5233f3d9eff03399c13e3e0b7138ad620056797 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/message/fusion/mptctl.c
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04003 * mpt Ioctl driver.
Prakash, Sathyaf36789e2007-08-14 16:22:54 +05304 * For use with LSI PCI chip/adapters
5 * running LSI Fusion MPT (Message Passing Technology) firmware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Prakash, Sathyacddc0ab2008-05-21 00:56:41 +05307 * Copyright (c) 1999-2008 LSI Corporation
Eric Moore16d20102007-06-13 16:31:07 -06008 * (mailto:DL-MPTFusionLinux@lsi.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#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
Prakash, Sathyacddc0ab2008-05-21 00:56:41 +053069#define COPYRIGHT "Copyright (c) 1999-2008 LSI Corporation"
Prakash, Sathyaf36789e2007-08-14 16:22:54 +053070#define MODULEAUTHOR "LSI Corporation"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#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
79MODULE_AUTHOR(MODULEAUTHOR);
80MODULE_DESCRIPTION(my_NAME);
81MODULE_LICENSE("GPL");
Eric Moore9f4203b2007-01-04 20:47:47 -070082MODULE_VERSION(my_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
85
Prakash, Sathyaf606f572007-08-14 16:12:53 +053086static u8 mptctl_id = MPT_MAX_PROTOCOL_DRIVERS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static DECLARE_WAIT_QUEUE_HEAD ( mptctl_wait );
89
90/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
91
92struct buflist {
93 u8 *kptr;
94 int len;
95};
96
97/*
98 * Function prototypes. Called from OS entry point mptctl_ioctl.
99 * arg contents specific to function.
100 */
101static int mptctl_fw_download(unsigned long arg);
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600102static int mptctl_getiocinfo(unsigned long arg, unsigned int cmd);
103static int mptctl_gettargetinfo(unsigned long arg);
104static int mptctl_readtest(unsigned long arg);
105static int mptctl_mpt_command(unsigned long arg);
106static int mptctl_eventquery(unsigned long arg);
107static int mptctl_eventenable(unsigned long arg);
108static int mptctl_eventreport(unsigned long arg);
109static int mptctl_replace_fw(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int mptctl_do_reset(unsigned long arg);
112static int mptctl_hp_hostinfo(unsigned long arg, unsigned int cmd);
113static int mptctl_hp_targetinfo(unsigned long arg);
114
115static int mptctl_probe(struct pci_dev *, const struct pci_device_id *);
116static void mptctl_remove(struct pci_dev *);
117
118#ifdef CONFIG_COMPAT
119static long compat_mpctl_ioctl(struct file *f, unsigned cmd, unsigned long arg);
120#endif
121/*
122 * Private function calls.
123 */
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600124static int mptctl_do_mpt_command(struct mpt_ioctl_command karg, void __user *mfPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int mptctl_do_fw_download(int ioc, char __user *ufwbuf, size_t fwlen);
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600126static MptSge_t *kbuf_alloc_2_sgl(int bytes, u32 dir, int sge_offset, int *frags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc);
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600128static void kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct buflist *buflist, MPT_ADAPTER *ioc);
130static void mptctl_timeout_expired (MPT_IOCTL *ioctl);
131static int mptctl_bus_reset(MPT_IOCTL *ioctl);
132static int mptctl_set_tm_flags(MPT_SCSI_HOST *hd);
133static void mptctl_free_tm_flags(MPT_ADAPTER *ioc);
134
135/*
136 * Reset Handler cleanup function
137 */
138static int mptctl_ioc_reset(MPT_ADAPTER *ioc, int reset_phase);
139
Moore, Ericea5a7a822006-02-02 17:20:01 -0700140/*
141 * Event Handler function
142 */
143static int mptctl_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
Moore, Ericc972c702006-03-14 09:14:06 -0700144static struct fasync_struct *async_queue=NULL;
Moore, Ericea5a7a822006-02-02 17:20:01 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
147/*
148 * Scatter gather list (SGL) sizes and limits...
149 */
150//#define MAX_SCSI_FRAGS 9
151#define MAX_FRAGS_SPILL1 9
152#define MAX_FRAGS_SPILL2 15
153#define FRAGS_PER_BUCKET (MAX_FRAGS_SPILL2 + 1)
154
155//#define MAX_CHAIN_FRAGS 64
156//#define MAX_CHAIN_FRAGS (15+15+15+16)
157#define MAX_CHAIN_FRAGS (4 * MAX_FRAGS_SPILL2 + 1)
158
159// Define max sg LIST bytes ( == (#frags + #chains) * 8 bytes each)
160// Works out to: 592d bytes! (9+1)*8 + 4*(15+1)*8
161// ^----------------- 80 + 512
162#define MAX_SGL_BYTES ((MAX_FRAGS_SPILL1 + 1 + (4 * FRAGS_PER_BUCKET)) * 8)
163
164/* linux only seems to ever give 128kB MAX contiguous (GFP_USER) mem bytes */
165#define MAX_KMALLOC_SZ (128*1024)
166
167#define MPT_IOCTL_DEFAULT_TIMEOUT 10 /* Default timeout value (seconds) */
168
169/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
170/**
171 * mptctl_syscall_down - Down the MPT adapter syscall semaphore.
172 * @ioc: Pointer to MPT adapter
173 * @nonblock: boolean, non-zero if O_NONBLOCK is set
174 *
175 * All of the ioctl commands can potentially sleep, which is illegal
176 * with a spinlock held, thus we perform mutual exclusion here.
177 *
178 * Returns negative errno on error, or zero for success.
179 */
180static inline int
181mptctl_syscall_down(MPT_ADAPTER *ioc, int nonblock)
182{
183 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (nonblock) {
Christoph Hellwigeeb846c2006-01-13 18:27:11 +0100186 if (!mutex_trylock(&ioc->ioctl->ioctl_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 rc = -EAGAIN;
188 } else {
Christoph Hellwigeeb846c2006-01-13 18:27:11 +0100189 if (mutex_lock_interruptible(&ioc->ioctl->ioctl_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 rc = -ERESTARTSYS;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return rc;
193}
194
195/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
196/*
197 * This is the callback for any message we have posted. The message itself
198 * will be returned to the message pool when we return from the IRQ
199 *
200 * This runs in irq context so be short and sweet.
201 */
202static int
203mptctl_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req, MPT_FRAME_HDR *reply)
204{
205 char *sense_data;
206 int sz, req_index;
207 u16 iocStatus;
208 u8 cmd;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (req)
211 cmd = req->u.hdr.Function;
212 else
213 return 1;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530214 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\tcompleting mpi function (0x%02X), req=%p, "
215 "reply=%p\n", ioc->name, req->u.hdr.Function, req, reply));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (ioc->ioctl) {
218
219 if (reply==NULL) {
220
Prakash, Sathya09120a82007-07-24 15:49:05 +0530221 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_reply() NULL Reply "
222 "Function=%x!\n", ioc->name, cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
225 ioc->ioctl->reset &= ~MPTCTL_RESET_OK;
226
227 /* We are done, issue wake up
228 */
229 ioc->ioctl->wait_done = 1;
230 wake_up (&mptctl_wait);
231 return 1;
232
233 }
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Copy the reply frame (which much exist
236 * for non-SCSI I/O) to the IOC structure.
237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 memcpy(ioc->ioctl->ReplyFrame, reply,
239 min(ioc->reply_sz, 4*reply->u.reply.MsgLength));
240 ioc->ioctl->status |= MPT_IOCTL_STATUS_RF_VALID;
241
242 /* Set the command status to GOOD if IOC Status is GOOD
243 * OR if SCSI I/O cmd and data underrun or recovered error.
244 */
Christoph Hellwig637fa992005-08-18 16:25:44 +0200245 iocStatus = le16_to_cpu(reply->u.reply.IOCStatus) & MPI_IOCSTATUS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (iocStatus == MPI_IOCSTATUS_SUCCESS)
247 ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
248
Prakash, Sathya09120a82007-07-24 15:49:05 +0530249 if (iocStatus || reply->u.reply.IOCLogInfo)
250 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\tiocstatus (0x%04X), "
251 "loginfo (0x%08X)\n", ioc->name,
252 iocStatus,
253 le32_to_cpu(reply->u.reply.IOCLogInfo)));
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if ((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) ||
256 (cmd == MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
Prakash, Sathya09120a82007-07-24 15:49:05 +0530257
258 if (reply->u.sreply.SCSIStatus || reply->u.sreply.SCSIState)
259 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT
260 "\tscsi_status (0x%02x), scsi_state (0x%02x), "
261 "tag = (0x%04x), transfer_count (0x%08x)\n", ioc->name,
262 reply->u.sreply.SCSIStatus,
263 reply->u.sreply.SCSIState,
264 le16_to_cpu(reply->u.sreply.TaskTag),
265 le32_to_cpu(reply->u.sreply.TransferCount)));
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ioc->ioctl->reset &= ~MPTCTL_RESET_OK;
268
269 if ((iocStatus == MPI_IOCSTATUS_SCSI_DATA_UNDERRUN) ||
270 (iocStatus == MPI_IOCSTATUS_SCSI_RECOVERED_ERROR)) {
271 ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
272 }
273 }
274
275 /* Copy the sense data - if present
276 */
277 if ((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) &&
278 (reply->u.sreply.SCSIState &
279 MPI_SCSI_STATE_AUTOSENSE_VALID)){
280 sz = req->u.scsireq.SenseBufferLength;
281 req_index =
282 le16_to_cpu(req->u.frame.hwhdr.msgctxu.fld.req_idx);
283 sense_data =
284 ((u8 *)ioc->sense_buf_pool +
285 (req_index * MPT_SENSE_BUFFER_ALLOC));
286 memcpy(ioc->ioctl->sense, sense_data, sz);
287 ioc->ioctl->status |= MPT_IOCTL_STATUS_SENSE_VALID;
288 }
289
290 if (cmd == MPI_FUNCTION_SCSI_TASK_MGMT)
291 mptctl_free_tm_flags(ioc);
292
293 /* We are done, issue wake up
294 */
295 ioc->ioctl->wait_done = 1;
296 wake_up (&mptctl_wait);
297 }
298 return 1;
299}
300
301/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
302/* mptctl_timeout_expired
303 *
304 * Expecting an interrupt, however timed out.
305 *
306 */
307static void mptctl_timeout_expired (MPT_IOCTL *ioctl)
308{
309 int rc = 1;
310
Prakash, Sathya09120a82007-07-24 15:49:05 +0530311 dctlprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT ": Timeout Expired! Host %d\n",
312 ioctl->ioc->name, ioctl->ioc->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (ioctl == NULL)
314 return;
315
316 ioctl->wait_done = 0;
317 if (ioctl->reset & MPTCTL_RESET_OK)
318 rc = mptctl_bus_reset(ioctl);
319
320 if (rc) {
321 /* Issue a reset for this device.
322 * The IOC is not responding.
323 */
Prakash, Sathya09120a82007-07-24 15:49:05 +0530324 dctlprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT "Calling HardReset! \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ioctl->ioc->name));
Eric Moorecd2c6192007-01-29 09:47:47 -0700326 mpt_HardResetHandler(ioctl->ioc, CAN_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 return;
329
330}
331
332/* mptctl_bus_reset
333 *
334 * Bus reset code.
335 *
336 */
337static int mptctl_bus_reset(MPT_IOCTL *ioctl)
338{
339 MPT_FRAME_HDR *mf;
340 SCSITaskMgmt_t *pScsiTm;
341 MPT_SCSI_HOST *hd;
342 int ii;
Prakash, Sathya7a195f42007-08-14 16:08:40 +0530343 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345
346 ioctl->reset &= ~MPTCTL_RESET_OK;
347
348 if (ioctl->ioc->sh == NULL)
349 return -EPERM;
350
Eric Mooree7eae9f2007-09-29 10:15:59 -0600351 hd = shost_priv(ioctl->ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (hd == NULL)
353 return -EPERM;
354
355 /* Single threading ....
356 */
357 if (mptctl_set_tm_flags(hd) != 0)
358 return -EPERM;
359
360 /* Send request
361 */
362 if ((mf = mpt_get_msg_frame(mptctl_id, ioctl->ioc)) == NULL) {
Prakash, Sathya09120a82007-07-24 15:49:05 +0530363 dtmprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt, no msg frames!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ioctl->ioc->name));
365
366 mptctl_free_tm_flags(ioctl->ioc);
367 return -ENOMEM;
368 }
369
Prakash, Sathya09120a82007-07-24 15:49:05 +0530370 dtmprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt request @ %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ioctl->ioc->name, mf));
372
373 pScsiTm = (SCSITaskMgmt_t *) mf;
Eric Moore793955f2007-01-29 09:42:20 -0700374 pScsiTm->TargetID = ioctl->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 pScsiTm->Bus = hd->port; /* 0 */
376 pScsiTm->ChainOffset = 0;
377 pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
378 pScsiTm->Reserved = 0;
379 pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS;
380 pScsiTm->Reserved1 = 0;
381 pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
382
383 for (ii= 0; ii < 8; ii++)
384 pScsiTm->LUN[ii] = 0;
385
386 for (ii=0; ii < 7; ii++)
387 pScsiTm->Reserved2[ii] = 0;
388
389 pScsiTm->TaskMsgContext = 0;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530390 dtmprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 "mptctl_bus_reset: issued.\n", ioctl->ioc->name));
392
Prakash, Sathya09120a82007-07-24 15:49:05 +0530393 DBG_DUMP_TM_REQUEST_FRAME(ioctl->ioc, (u32 *)mf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 ioctl->wait_done=0;
Prakash, Sathya7a195f42007-08-14 16:08:40 +0530396
397 if ((ioctl->ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
398 (ioctl->ioc->facts.MsgVersion >= MPI_VERSION_01_05))
399 mpt_put_msg_frame_hi_pri(mptctl_id, ioctl->ioc, mf);
400 else {
401 retval = mpt_send_handshake_request(mptctl_id, ioctl->ioc,
402 sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, CAN_SLEEP);
403 if (retval != 0) {
404 dfailprintk(ioctl->ioc, printk(MYIOC_s_ERR_FMT "_send_handshake FAILED!"
405 " (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
406 hd->ioc, mf));
407 goto mptctl_bus_reset_done;
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 /* Now wait for the command to complete */
Moore, Eric86a7dca2006-02-02 17:19:37 -0700412 ii = wait_event_timeout(mptctl_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ioctl->wait_done == 1,
414 HZ*5 /* 5 second timeout */);
415
416 if(ii <=0 && (ioctl->wait_done != 1 )) {
Moore, Eric86a7dca2006-02-02 17:19:37 -0700417 mpt_free_msg_frame(hd->ioc, mf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ioctl->wait_done = 0;
419 retval = -1; /* return failure */
420 }
421
422mptctl_bus_reset_done:
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 mptctl_free_tm_flags(ioctl->ioc);
425 return retval;
426}
427
428static int
429mptctl_set_tm_flags(MPT_SCSI_HOST *hd) {
430 unsigned long flags;
431
432 spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
433
434 if (hd->tmState == TM_STATE_NONE) {
435 hd->tmState = TM_STATE_IN_PROGRESS;
436 hd->tmPending = 1;
437 spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
438 } else {
439 spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
440 return -EBUSY;
441 }
442
443 return 0;
444}
445
446static void
447mptctl_free_tm_flags(MPT_ADAPTER *ioc)
448{
449 MPT_SCSI_HOST * hd;
450 unsigned long flags;
451
Eric Mooree7eae9f2007-09-29 10:15:59 -0600452 hd = shost_priv(ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (hd == NULL)
454 return;
455
456 spin_lock_irqsave(&ioc->FreeQlock, flags);
457
458 hd->tmState = TM_STATE_NONE;
459 hd->tmPending = 0;
460 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
461
462 return;
463}
464
465/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
466/* mptctl_ioc_reset
467 *
468 * Clean-up functionality. Used only if there has been a
469 * reload of the FW due.
470 *
471 */
472static int
473mptctl_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
474{
475 MPT_IOCTL *ioctl = ioc->ioctl;
Eric Moore29dd3602007-09-14 18:46:51 -0600476 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IOC %s_reset routed to IOCTL driver!\n", ioc->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
478 reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
479
480 if(ioctl == NULL)
481 return 1;
482
483 switch(reset_phase) {
484 case MPT_IOC_SETUP_RESET:
485 ioctl->status |= MPT_IOCTL_STATUS_DID_IOCRESET;
486 break;
487 case MPT_IOC_POST_RESET:
488 ioctl->status &= ~MPT_IOCTL_STATUS_DID_IOCRESET;
489 break;
490 case MPT_IOC_PRE_RESET:
491 default:
492 break;
493 }
494
495 return 1;
496}
497
498/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Moore, Ericea5a7a822006-02-02 17:20:01 -0700499/* ASYNC Event Notification Support */
500static int
501mptctl_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
502{
503 u8 event;
504
505 event = le32_to_cpu(pEvReply->Event) & 0xFF;
506
Prakash, Sathya09120a82007-07-24 15:49:05 +0530507 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s() called\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700508 ioc->name, __func__));
Moore, Ericea5a7a822006-02-02 17:20:01 -0700509 if(async_queue == NULL)
510 return 1;
511
512 /* Raise SIGIO for persistent events.
513 * TODO - this define is not in MPI spec yet,
514 * but they plan to set it to 0x21
515 */
516 if (event == 0x21 ) {
517 ioc->aen_event_read_flag=1;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530518 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Raised SIGIO to application\n",
519 ioc->name));
520 devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
521 "Raised SIGIO to application\n", ioc->name));
Moore, Ericea5a7a822006-02-02 17:20:01 -0700522 kill_fasync(&async_queue, SIGIO, POLL_IN);
523 return 1;
524 }
525
526 /* This flag is set after SIGIO was raised, and
527 * remains set until the application has read
528 * the event log via ioctl=MPTEVENTREPORT
529 */
530 if(ioc->aen_event_read_flag)
531 return 1;
532
533 /* Signal only for the events that are
534 * requested for by the application
535 */
536 if (ioc->events && (ioc->eventTypes & ( 1 << event))) {
537 ioc->aen_event_read_flag=1;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530538 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT
539 "Raised SIGIO to application\n", ioc->name));
540 devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
541 "Raised SIGIO to application\n", ioc->name));
Moore, Ericea5a7a822006-02-02 17:20:01 -0700542 kill_fasync(&async_queue, SIGIO, POLL_IN);
543 }
544 return 1;
545}
546
547static int
548mptctl_fasync(int fd, struct file *filep, int mode)
549{
550 MPT_ADAPTER *ioc;
Jonathan Corbetb7e3e1f2008-06-19 15:41:11 -0600551 int ret;
Moore, Ericea5a7a822006-02-02 17:20:01 -0700552
Jonathan Corbetb7e3e1f2008-06-19 15:41:11 -0600553 lock_kernel();
Moore, Ericea5a7a822006-02-02 17:20:01 -0700554 list_for_each_entry(ioc, &ioc_list, list)
555 ioc->aen_event_read_flag=0;
556
Jonathan Corbetb7e3e1f2008-06-19 15:41:11 -0600557 ret = fasync_helper(fd, filep, mode, &async_queue);
558 unlock_kernel();
559 return ret;
Moore, Ericea5a7a822006-02-02 17:20:01 -0700560}
561
562static int
563mptctl_release(struct inode *inode, struct file *filep)
564{
Moore, Ericea5a7a822006-02-02 17:20:01 -0700565 return fasync_helper(-1, filep, 0, &async_queue);
566}
567
568/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/*
570 * MPT ioctl handler
571 * cmd - specify the particular IOCTL command to be issued
572 * arg - data specific to the command. Must not be null.
573 */
574static long
575__mptctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
576{
577 mpt_ioctl_header __user *uhdr = (void __user *) arg;
578 mpt_ioctl_header khdr;
579 int iocnum;
580 unsigned iocnumX;
581 int nonblock = (file->f_flags & O_NONBLOCK);
582 int ret;
583 MPT_ADAPTER *iocp = NULL;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (copy_from_user(&khdr, uhdr, sizeof(khdr))) {
Eric Moore29dd3602007-09-14 18:46:51 -0600586 printk(KERN_ERR MYNAM "%s::mptctl_ioctl() @%d - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 "Unable to copy mpt_ioctl_header data @ %p\n",
588 __FILE__, __LINE__, uhdr);
589 return -EFAULT;
590 }
591 ret = -ENXIO; /* (-6) No such device or address */
592
593 /* Verify intended MPT adapter - set iocnum and the adapter
594 * pointer (iocp)
595 */
596 iocnumX = khdr.iocnum & 0xFF;
597 if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
598 (iocp == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -0600599 printk(KERN_DEBUG MYNAM "%s::mptctl_ioctl() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +0530600 __FILE__, __LINE__, iocnumX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return -ENODEV;
602 }
603
604 if (!iocp->active) {
Eric Moore29dd3602007-09-14 18:46:51 -0600605 printk(KERN_DEBUG MYNAM "%s::mptctl_ioctl() @%d - Controller disabled.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 __FILE__, __LINE__);
607 return -EFAULT;
608 }
609
610 /* Handle those commands that are just returning
611 * information stored in the driver.
612 * These commands should never time out and are unaffected
613 * by TM and FW reloads.
614 */
615 if ((cmd & ~IOCSIZE_MASK) == (MPTIOCINFO & ~IOCSIZE_MASK)) {
616 return mptctl_getiocinfo(arg, _IOC_SIZE(cmd));
617 } else if (cmd == MPTTARGETINFO) {
618 return mptctl_gettargetinfo(arg);
619 } else if (cmd == MPTTEST) {
620 return mptctl_readtest(arg);
621 } else if (cmd == MPTEVENTQUERY) {
622 return mptctl_eventquery(arg);
623 } else if (cmd == MPTEVENTENABLE) {
624 return mptctl_eventenable(arg);
625 } else if (cmd == MPTEVENTREPORT) {
626 return mptctl_eventreport(arg);
627 } else if (cmd == MPTFWREPLACE) {
628 return mptctl_replace_fw(arg);
629 }
630
631 /* All of these commands require an interrupt or
632 * are unknown/illegal.
633 */
634 if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
635 return ret;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (cmd == MPTFWDOWNLOAD)
638 ret = mptctl_fw_download(arg);
639 else if (cmd == MPTCOMMAND)
640 ret = mptctl_mpt_command(arg);
641 else if (cmd == MPTHARDRESET)
642 ret = mptctl_do_reset(arg);
643 else if ((cmd & ~IOCSIZE_MASK) == (HP_GETHOSTINFO & ~IOCSIZE_MASK))
644 ret = mptctl_hp_hostinfo(arg, _IOC_SIZE(cmd));
645 else if (cmd == HP_GETTARGETINFO)
646 ret = mptctl_hp_targetinfo(arg);
647 else
648 ret = -EINVAL;
649
Christoph Hellwigeeb846c2006-01-13 18:27:11 +0100650 mutex_unlock(&iocp->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 return ret;
653}
654
655static long
656mptctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
657{
658 long ret;
659 lock_kernel();
660 ret = __mptctl_ioctl(file, cmd, arg);
661 unlock_kernel();
662 return ret;
663}
664
665static int mptctl_do_reset(unsigned long arg)
666{
667 struct mpt_ioctl_diag_reset __user *urinfo = (void __user *) arg;
668 struct mpt_ioctl_diag_reset krinfo;
669 MPT_ADAPTER *iocp;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (copy_from_user(&krinfo, urinfo, sizeof(struct mpt_ioctl_diag_reset))) {
Eric Moore29dd3602007-09-14 18:46:51 -0600672 printk(KERN_ERR MYNAM "%s@%d::mptctl_do_reset - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 "Unable to copy mpt_ioctl_diag_reset struct @ %p\n",
674 __FILE__, __LINE__, urinfo);
675 return -EFAULT;
676 }
677
678 if (mpt_verify_adapter(krinfo.hdr.iocnum, &iocp) < 0) {
Eric Moore29dd3602007-09-14 18:46:51 -0600679 printk(KERN_DEBUG MYNAM "%s@%d::mptctl_do_reset - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +0530680 __FILE__, __LINE__, krinfo.hdr.iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -ENODEV; /* (-6) No such device or address */
682 }
683
Prakash, Sathya09120a82007-07-24 15:49:05 +0530684 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "mptctl_do_reset called.\n",
685 iocp->name));
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (mpt_HardResetHandler(iocp, CAN_SLEEP) != 0) {
Eric Moore29dd3602007-09-14 18:46:51 -0600688 printk (MYIOC_s_ERR_FMT "%s@%d::mptctl_do_reset - reset failed.\n",
689 iocp->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -1;
691 }
692
693 return 0;
694}
695
696/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
697/*
698 * MPT FW download function. Cast the arg into the mpt_fw_xfer structure.
699 * This structure contains: iocnum, firmware length (bytes),
700 * pointer to user space memory where the fw image is stored.
701 *
702 * Outputs: None.
703 * Return: 0 if successful
704 * -EFAULT if data unavailable
705 * -ENXIO if no such device
706 * -EAGAIN if resource problem
707 * -ENOMEM if no memory for SGE
708 * -EMLINK if too many chain buffers required
709 * -EBADRQC if adapter does not support FW download
710 * -EBUSY if adapter is busy
711 * -ENOMSG if FW upload returned bad status
712 */
713static int
714mptctl_fw_download(unsigned long arg)
715{
716 struct mpt_fw_xfer __user *ufwdl = (void __user *) arg;
717 struct mpt_fw_xfer kfwdl;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (copy_from_user(&kfwdl, ufwdl, sizeof(struct mpt_fw_xfer))) {
Eric Moore29dd3602007-09-14 18:46:51 -0600720 printk(KERN_ERR MYNAM "%s@%d::_ioctl_fwdl - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 "Unable to copy mpt_fw_xfer struct @ %p\n",
722 __FILE__, __LINE__, ufwdl);
723 return -EFAULT;
724 }
725
726 return mptctl_do_fw_download(kfwdl.iocnum, kfwdl.bufp, kfwdl.fwlen);
727}
728
729/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
730/*
731 * FW Download engine.
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 */
743static int
744mptctl_do_fw_download(int ioc, char __user *ufwbuf, size_t fwlen)
745{
746 FWDownload_t *dlmsg;
747 MPT_FRAME_HDR *mf;
748 MPT_ADAPTER *iocp;
749 FWDownloadTCSGE_t *ptsge;
750 MptSge_t *sgl, *sgIn;
751 char *sgOut;
752 struct buflist *buflist;
753 struct buflist *bl;
754 dma_addr_t sgl_dma;
755 int ret;
756 int numfrags = 0;
757 int maxfrags;
758 int n = 0;
759 u32 sgdir;
760 u32 nib;
761 int fw_bytes_copied = 0;
762 int i;
763 int sge_offset = 0;
764 u16 iocstat;
765 pFWDownloadReply_t ReplyMsg = NULL;
766
Moore, Eric946cbf02006-02-02 17:19:50 -0700767 if (mpt_verify_adapter(ioc, &iocp) < 0) {
Eric Moore29dd3602007-09-14 18:46:51 -0600768 printk(KERN_DEBUG MYNAM "ioctl_fwdl - ioc%d not found!\n",
769 ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ENODEV; /* (-6) No such device or address */
Moore, Eric946cbf02006-02-02 17:19:50 -0700771 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Moore, Eric946cbf02006-02-02 17:19:50 -0700773 /* Valid device. Get a message frame and construct the FW download message.
774 */
775 if ((mf = mpt_get_msg_frame(mptctl_id, iocp)) == NULL)
776 return -EAGAIN;
777 }
Prakash, Sathya09120a82007-07-24 15:49:05 +0530778
779 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT
780 "mptctl_do_fwdl called. mptctl_id = %xh.\n", iocp->name, mptctl_id));
781 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: kfwdl.bufp = %p\n",
782 iocp->name, ufwbuf));
783 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: kfwdl.fwlen = %d\n",
784 iocp->name, (int)fwlen));
785 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: kfwdl.ioc = %04xh\n",
786 iocp->name, ioc));
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dlmsg = (FWDownload_t*) mf;
789 ptsge = (FWDownloadTCSGE_t *) &dlmsg->SGL;
790 sgOut = (char *) (ptsge + 1);
791
792 /*
793 * Construct f/w download request
794 */
795 dlmsg->ImageType = MPI_FW_DOWNLOAD_ITYPE_FW;
796 dlmsg->Reserved = 0;
797 dlmsg->ChainOffset = 0;
798 dlmsg->Function = MPI_FUNCTION_FW_DOWNLOAD;
799 dlmsg->Reserved1[0] = dlmsg->Reserved1[1] = dlmsg->Reserved1[2] = 0;
Moore, Eric946cbf02006-02-02 17:19:50 -0700800 if (iocp->facts.MsgVersion >= MPI_VERSION_01_05)
801 dlmsg->MsgFlags = MPI_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT;
802 else
803 dlmsg->MsgFlags = 0;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /* Set up the Transaction SGE.
807 */
808 ptsge->Reserved = 0;
809 ptsge->ContextSize = 0;
810 ptsge->DetailsLength = 12;
811 ptsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
812 ptsge->Reserved_0100_Checksum = 0;
813 ptsge->ImageOffset = 0;
814 ptsge->ImageSize = cpu_to_le32(fwlen);
815
816 /* Add the SGL
817 */
818
819 /*
820 * Need to kmalloc area(s) for holding firmware image bytes.
821 * But we need to do it piece meal, using a proper
822 * scatter gather list (with 128kB MAX hunks).
823 *
824 * A practical limit here might be # of sg hunks that fit into
825 * a single IOC request frame; 12 or 8 (see below), so:
826 * For FC9xx: 12 x 128kB == 1.5 mB (max)
827 * For C1030: 8 x 128kB == 1 mB (max)
828 * We could support chaining, but things get ugly(ier:)
829 *
830 * Set the sge_offset to the start of the sgl (bytes).
831 */
832 sgdir = 0x04000000; /* IOC will READ from sys mem */
833 sge_offset = sizeof(MPIHeader_t) + sizeof(FWDownloadTCSGE_t);
834 if ((sgl = kbuf_alloc_2_sgl(fwlen, sgdir, sge_offset,
835 &numfrags, &buflist, &sgl_dma, iocp)) == NULL)
836 return -ENOMEM;
837
838 /*
839 * We should only need SGL with 2 simple_32bit entries (up to 256 kB)
840 * for FC9xx f/w image, but calculate max number of sge hunks
841 * we can fit into a request frame, and limit ourselves to that.
842 * (currently no chain support)
843 * maxfrags = (Request Size - FWdownload Size ) / Size of 32 bit SGE
844 * Request maxfrags
845 * 128 12
846 * 96 8
847 * 64 4
848 */
849 maxfrags = (iocp->req_sz - sizeof(MPIHeader_t) - sizeof(FWDownloadTCSGE_t))
850 / (sizeof(dma_addr_t) + sizeof(u32));
851 if (numfrags > maxfrags) {
852 ret = -EMLINK;
853 goto fwdl_out;
854 }
855
Prakash, Sathya09120a82007-07-24 15:49:05 +0530856 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: sgl buffer = %p, sgfrags = %d\n",
857 iocp->name, sgl, numfrags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /*
860 * Parse SG list, copying sgl itself,
861 * plus f/w image hunks from user space as we go...
862 */
863 ret = -EFAULT;
864 sgIn = sgl;
865 bl = buflist;
866 for (i=0; i < numfrags; i++) {
867
868 /* Get the SGE type: 0 - TCSGE, 3 - Chain, 1 - Simple SGE
869 * Skip everything but Simple. If simple, copy from
870 * user space into kernel space.
871 * Note: we should not have anything but Simple as
872 * Chain SGE are illegal.
873 */
874 nib = (sgIn->FlagsLength & 0x30000000) >> 28;
875 if (nib == 0 || nib == 3) {
876 ;
877 } else if (sgIn->Address) {
878 mpt_add_sge(sgOut, sgIn->FlagsLength, sgIn->Address);
879 n++;
880 if (copy_from_user(bl->kptr, ufwbuf+fw_bytes_copied, bl->len)) {
Eric Moore29dd3602007-09-14 18:46:51 -0600881 printk(MYIOC_s_ERR_FMT "%s@%d::_ioctl_fwdl - "
882 "Unable to copy f/w buffer hunk#%d @ %p\n",
883 iocp->name, __FILE__, __LINE__, n, ufwbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 goto fwdl_out;
885 }
886 fw_bytes_copied += bl->len;
887 }
888 sgIn++;
889 bl++;
890 sgOut += (sizeof(dma_addr_t) + sizeof(u32));
891 }
892
Prakash, Sathya09120a82007-07-24 15:49:05 +0530893 DBG_DUMP_FW_DOWNLOAD(iocp, (u32 *)mf, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /*
896 * Finally, perform firmware download.
897 */
Moore, Eric946cbf02006-02-02 17:19:50 -0700898 ReplyMsg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 mpt_put_msg_frame(mptctl_id, iocp, mf);
900
901 /* Now wait for the command to complete */
Moore, Eric86a7dca2006-02-02 17:19:37 -0700902 ret = wait_event_timeout(mptctl_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 iocp->ioctl->wait_done == 1,
904 HZ*60);
905
906 if(ret <=0 && (iocp->ioctl->wait_done != 1 )) {
907 /* Now we need to reset the board */
908 mptctl_timeout_expired(iocp->ioctl);
909 ret = -ENODATA;
910 goto fwdl_out;
911 }
912
913 if (sgl)
914 kfree_sgl(sgl, sgl_dma, buflist, iocp);
915
916 ReplyMsg = (pFWDownloadReply_t)iocp->ioctl->ReplyFrame;
917 iocstat = le16_to_cpu(ReplyMsg->IOCStatus) & MPI_IOCSTATUS_MASK;
918 if (iocstat == MPI_IOCSTATUS_SUCCESS) {
Eric Moore29dd3602007-09-14 18:46:51 -0600919 printk(MYIOC_s_INFO_FMT "F/W update successfull!\n", iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 } else if (iocstat == MPI_IOCSTATUS_INVALID_FUNCTION) {
Eric Moore29dd3602007-09-14 18:46:51 -0600922 printk(MYIOC_s_WARN_FMT "Hmmm... F/W download not supported!?!\n",
923 iocp->name);
924 printk(MYIOC_s_WARN_FMT "(time to go bang on somebodies door)\n",
925 iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EBADRQC;
927 } else if (iocstat == MPI_IOCSTATUS_BUSY) {
Eric Moore29dd3602007-09-14 18:46:51 -0600928 printk(MYIOC_s_WARN_FMT "IOC_BUSY!\n", iocp->name);
929 printk(MYIOC_s_WARN_FMT "(try again later?)\n", iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return -EBUSY;
931 } else {
Eric Moore29dd3602007-09-14 18:46:51 -0600932 printk(MYIOC_s_WARN_FMT "ioctl_fwdl() returned [bad] status = %04xh\n",
933 iocp->name, iocstat);
934 printk(MYIOC_s_WARN_FMT "(bad VooDoo)\n", iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return -ENOMSG;
936 }
937 return 0;
938
939fwdl_out:
940 kfree_sgl(sgl, sgl_dma, buflist, iocp);
941 return ret;
942}
943
944/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
945/*
946 * SGE Allocation routine
947 *
948 * Inputs: bytes - number of bytes to be transferred
949 * sgdir - data direction
950 * sge_offset - offset (in bytes) from the start of the request
951 * frame to the first SGE
952 * ioc - pointer to the mptadapter
953 * Outputs: frags - number of scatter gather elements
954 * blp - point to the buflist pointer
955 * sglbuf_dma - pointer to the (dma) sgl
956 * Returns: Null if failes
957 * pointer to the (virtual) sgl if successful.
958 */
959static MptSge_t *
960kbuf_alloc_2_sgl(int bytes, u32 sgdir, int sge_offset, int *frags,
961 struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc)
962{
963 MptSge_t *sglbuf = NULL; /* pointer to array of SGE */
964 /* and chain buffers */
965 struct buflist *buflist = NULL; /* kernel routine */
966 MptSge_t *sgl;
967 int numfrags = 0;
968 int fragcnt = 0;
969 int alloc_sz = min(bytes,MAX_KMALLOC_SZ); // avoid kernel warning msg!
970 int bytes_allocd = 0;
971 int this_alloc;
972 dma_addr_t pa; // phys addr
973 int i, buflist_ent;
974 int sg_spill = MAX_FRAGS_SPILL1;
975 int dir;
976 /* initialization */
977 *frags = 0;
978 *blp = NULL;
979
980 /* Allocate and initialize an array of kernel
981 * structures for the SG elements.
982 */
983 i = MAX_SGL_BYTES / 8;
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -0700984 buflist = kzalloc(i, GFP_USER);
985 if (!buflist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 buflist_ent = 0;
988
989 /* Allocate a single block of memory to store the sg elements and
990 * the chain buffers. The calling routine is responsible for
991 * copying the data in this array into the correct place in the
992 * request and chain buffers.
993 */
994 sglbuf = pci_alloc_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf_dma);
995 if (sglbuf == NULL)
996 goto free_and_fail;
997
998 if (sgdir & 0x04000000)
999 dir = PCI_DMA_TODEVICE;
1000 else
1001 dir = PCI_DMA_FROMDEVICE;
1002
1003 /* At start:
1004 * sgl = sglbuf = point to beginning of sg buffer
1005 * buflist_ent = 0 = first kernel structure
1006 * sg_spill = number of SGE that can be written before the first
1007 * chain element.
1008 *
1009 */
1010 sgl = sglbuf;
1011 sg_spill = ((ioc->req_sz - sge_offset)/(sizeof(dma_addr_t) + sizeof(u32))) - 1;
1012 while (bytes_allocd < bytes) {
1013 this_alloc = min(alloc_sz, bytes-bytes_allocd);
1014 buflist[buflist_ent].len = this_alloc;
1015 buflist[buflist_ent].kptr = pci_alloc_consistent(ioc->pcidev,
1016 this_alloc,
1017 &pa);
1018 if (buflist[buflist_ent].kptr == NULL) {
1019 alloc_sz = alloc_sz / 2;
1020 if (alloc_sz == 0) {
Eric Moore29dd3602007-09-14 18:46:51 -06001021 printk(MYIOC_s_WARN_FMT "-SG: No can do - "
1022 "not enough memory! :-(\n", ioc->name);
1023 printk(MYIOC_s_WARN_FMT "-SG: (freeing %d frags)\n",
1024 ioc->name, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto free_and_fail;
1026 }
1027 continue;
1028 } else {
1029 dma_addr_t dma_addr;
1030
1031 bytes_allocd += this_alloc;
1032 sgl->FlagsLength = (0x10000000|MPT_SGE_FLAGS_ADDRESSING|sgdir|this_alloc);
1033 dma_addr = pci_map_single(ioc->pcidev, buflist[buflist_ent].kptr, this_alloc, dir);
1034 sgl->Address = dma_addr;
1035
1036 fragcnt++;
1037 numfrags++;
1038 sgl++;
1039 buflist_ent++;
1040 }
1041
1042 if (bytes_allocd >= bytes)
1043 break;
1044
1045 /* Need to chain? */
1046 if (fragcnt == sg_spill) {
Eric Moore29dd3602007-09-14 18:46:51 -06001047 printk(MYIOC_s_WARN_FMT
1048 "-SG: No can do - " "Chain required! :-(\n", ioc->name);
1049 printk(MYIOC_s_WARN_FMT "(freeing %d frags)\n", ioc->name, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto free_and_fail;
1051 }
1052
1053 /* overflow check... */
1054 if (numfrags*8 > MAX_SGL_BYTES){
1055 /* GRRRRR... */
Eric Moore29dd3602007-09-14 18:46:51 -06001056 printk(MYIOC_s_WARN_FMT "-SG: No can do - "
1057 "too many SG frags! :-(\n", ioc->name);
1058 printk(MYIOC_s_WARN_FMT "-SG: (freeing %d frags)\n",
1059 ioc->name, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto free_and_fail;
1061 }
1062 }
1063
1064 /* Last sge fixup: set LE+eol+eob bits */
1065 sgl[-1].FlagsLength |= 0xC1000000;
1066
1067 *frags = numfrags;
1068 *blp = buflist;
1069
Prakash, Sathya09120a82007-07-24 15:49:05 +05301070 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "-SG: kbuf_alloc_2_sgl() - "
1071 "%d SG frags generated!\n", ioc->name, numfrags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Prakash, Sathya09120a82007-07-24 15:49:05 +05301073 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "-SG: kbuf_alloc_2_sgl() - "
1074 "last (big) alloc_sz=%d\n", ioc->name, alloc_sz));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return sglbuf;
1077
1078free_and_fail:
1079 if (sglbuf != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 for (i = 0; i < numfrags; i++) {
1081 dma_addr_t dma_addr;
1082 u8 *kptr;
1083 int len;
1084
1085 if ((sglbuf[i].FlagsLength >> 24) == 0x30)
1086 continue;
1087
1088 dma_addr = sglbuf[i].Address;
1089 kptr = buflist[i].kptr;
1090 len = buflist[i].len;
1091
1092 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1093 }
1094 pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf, *sglbuf_dma);
1095 }
1096 kfree(buflist);
1097 return NULL;
1098}
1099
1100/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1101/*
1102 * Routine to free the SGL elements.
1103 */
1104static void
1105kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma, struct buflist *buflist, MPT_ADAPTER *ioc)
1106{
1107 MptSge_t *sg = sgl;
1108 struct buflist *bl = buflist;
1109 u32 nib;
1110 int dir;
1111 int n = 0;
1112
1113 if (sg->FlagsLength & 0x04000000)
1114 dir = PCI_DMA_TODEVICE;
1115 else
1116 dir = PCI_DMA_FROMDEVICE;
1117
1118 nib = (sg->FlagsLength & 0xF0000000) >> 28;
1119 while (! (nib & 0x4)) { /* eob */
1120 /* skip ignore/chain. */
1121 if (nib == 0 || nib == 3) {
1122 ;
1123 } else if (sg->Address) {
1124 dma_addr_t dma_addr;
1125 void *kptr;
1126 int len;
1127
1128 dma_addr = sg->Address;
1129 kptr = bl->kptr;
1130 len = bl->len;
1131 pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1132 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1133 n++;
1134 }
1135 sg++;
1136 bl++;
1137 nib = (le32_to_cpu(sg->FlagsLength) & 0xF0000000) >> 28;
1138 }
1139
1140 /* we're at eob! */
1141 if (sg->Address) {
1142 dma_addr_t dma_addr;
1143 void *kptr;
1144 int len;
1145
1146 dma_addr = sg->Address;
1147 kptr = bl->kptr;
1148 len = bl->len;
1149 pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1150 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1151 n++;
1152 }
1153
1154 pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sgl, sgl_dma);
1155 kfree(buflist);
Prakash, Sathya09120a82007-07-24 15:49:05 +05301156 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "-SG: Free'd 1 SGL buf + %d kbufs!\n",
1157 ioc->name, n));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1161/*
1162 * mptctl_getiocinfo - Query the host adapter for IOC information.
1163 * @arg: User space argument
1164 *
1165 * Outputs: None.
1166 * Return: 0 if successful
1167 * -EFAULT if data unavailable
1168 * -ENODEV if no such device/adapter
1169 */
1170static int
1171mptctl_getiocinfo (unsigned long arg, unsigned int data_size)
1172{
1173 struct mpt_ioctl_iocinfo __user *uarg = (void __user *) arg;
1174 struct mpt_ioctl_iocinfo *karg;
1175 MPT_ADAPTER *ioc;
1176 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 int iocnum;
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04001178 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int cim_rev;
1180 u8 revision;
Eric Moore793955f2007-01-29 09:42:20 -07001181 struct scsi_device *sdev;
Eric Moorea69de502007-09-14 18:48:19 -06001182 VirtDevice *vdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* Add of PCI INFO results in unaligned access for
1185 * IA64 and Sparc. Reset long to int. Return no PCI
1186 * data for obsolete format.
1187 */
1188 if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev0))
1189 cim_rev = 0;
1190 else if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev1))
1191 cim_rev = 1;
1192 else if (data_size == sizeof(struct mpt_ioctl_iocinfo))
1193 cim_rev = 2;
1194 else if (data_size == (sizeof(struct mpt_ioctl_iocinfo_rev0)+12))
1195 cim_rev = 0; /* obsolete */
1196 else
1197 return -EFAULT;
1198
1199 karg = kmalloc(data_size, GFP_KERNEL);
1200 if (karg == NULL) {
Eric Moore29dd3602007-09-14 18:46:51 -06001201 printk(KERN_ERR MYNAM "%s::mpt_ioctl_iocinfo() @%d - no memory available!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 __FILE__, __LINE__);
1203 return -ENOMEM;
1204 }
1205
1206 if (copy_from_user(karg, uarg, data_size)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001207 printk(KERN_ERR MYNAM "%s@%d::mptctl_getiocinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 "Unable to read in mpt_ioctl_iocinfo struct @ %p\n",
1209 __FILE__, __LINE__, uarg);
1210 kfree(karg);
1211 return -EFAULT;
1212 }
1213
1214 if (((iocnum = mpt_verify_adapter(karg->hdr.iocnum, &ioc)) < 0) ||
1215 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001216 printk(KERN_DEBUG MYNAM "%s::mptctl_getiocinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301217 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 kfree(karg);
1219 return -ENODEV;
1220 }
1221
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04001222 /* Verify the data transfer size is correct. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (karg->hdr.maxDataSize != data_size) {
Eric Moore29dd3602007-09-14 18:46:51 -06001224 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_getiocinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 "Structure size mismatch. Command not completed.\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001226 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 kfree(karg);
1228 return -EFAULT;
1229 }
1230
Prakash, Sathya09120a82007-07-24 15:49:05 +05301231 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_getiocinfo called.\n",
1232 ioc->name));
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /* Fill in the data and return the structure to the calling
1235 * program
1236 */
Moore, Eric9cc1cfb2006-02-02 17:19:33 -07001237 if (ioc->bus_type == SAS)
1238 karg->adapterType = MPT_IOCTL_INTERFACE_SAS;
1239 else if (ioc->bus_type == FC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 karg->adapterType = MPT_IOCTL_INTERFACE_FC;
1241 else
1242 karg->adapterType = MPT_IOCTL_INTERFACE_SCSI;
1243
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04001244 if (karg->hdr.port > 1)
1245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 port = karg->hdr.port;
1247
1248 karg->port = port;
1249 pdev = (struct pci_dev *) ioc->pcidev;
1250
1251 karg->pciId = pdev->device;
1252 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1253 karg->hwRev = revision;
1254 karg->subSystemDevice = pdev->subsystem_device;
1255 karg->subSystemVendor = pdev->subsystem_vendor;
1256
1257 if (cim_rev == 1) {
1258 /* Get the PCI bus, device, and function numbers for the IOC
1259 */
1260 karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1261 karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1262 karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1263 } else if (cim_rev == 2) {
Moore, Eric86a7dca2006-02-02 17:19:37 -07001264 /* Get the PCI bus, device, function and segment ID numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 for the IOC */
1266 karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1267 karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1268 karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 karg->pciInfo.segmentID = pci_domain_nr(pdev->bus);
1270 }
1271
1272 /* Get number of devices
1273 */
Eric Moore793955f2007-01-29 09:42:20 -07001274 karg->numDevices = 0;
1275 if (ioc->sh) {
1276 shost_for_each_device(sdev, ioc->sh) {
Eric Moorea69de502007-09-14 18:48:19 -06001277 vdevice = sdev->hostdata;
1278 if (vdevice->vtarget->tflags &
Eric Moore793955f2007-01-29 09:42:20 -07001279 MPT_TARGET_FLAGS_RAID_COMPONENT)
1280 continue;
1281 karg->numDevices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /* Set the BIOS and FW Version
1286 */
1287 karg->FWVersion = ioc->facts.FWVersion.Word;
1288 karg->BIOSVersion = ioc->biosVersion;
1289
1290 /* Set the Version Strings.
1291 */
1292 strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
1293 karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
1294
1295 karg->busChangeEvent = 0;
1296 karg->hostId = ioc->pfacts[port].PortSCSIID;
1297 karg->rsvd[0] = karg->rsvd[1] = 0;
1298
1299 /* Copy the data from kernel memory to user memory
1300 */
1301 if (copy_to_user((char __user *)arg, karg, data_size)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001302 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_getiocinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 "Unable to write out mpt_ioctl_iocinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001304 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 kfree(karg);
1306 return -EFAULT;
1307 }
1308
1309 kfree(karg);
1310 return 0;
1311}
1312
1313/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1314/*
1315 * mptctl_gettargetinfo - Query the host adapter for target information.
1316 * @arg: User space argument
1317 *
1318 * Outputs: None.
1319 * Return: 0 if successful
1320 * -EFAULT if data unavailable
1321 * -ENODEV if no such device/adapter
1322 */
1323static int
1324mptctl_gettargetinfo (unsigned long arg)
1325{
1326 struct mpt_ioctl_targetinfo __user *uarg = (void __user *) arg;
1327 struct mpt_ioctl_targetinfo karg;
1328 MPT_ADAPTER *ioc;
Eric Moorea69de502007-09-14 18:48:19 -06001329 VirtDevice *vdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 char *pmem;
1331 int *pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int iocnum;
1333 int numDevices = 0;
Eric Moore793955f2007-01-29 09:42:20 -07001334 int lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 int maxWordsLeft;
1336 int numBytes;
Eric Moore793955f2007-01-29 09:42:20 -07001337 u8 port;
1338 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_targetinfo))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001341 printk(KERN_ERR MYNAM "%s@%d::mptctl_gettargetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 "Unable to read in mpt_ioctl_targetinfo struct @ %p\n",
1343 __FILE__, __LINE__, uarg);
1344 return -EFAULT;
1345 }
1346
1347 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1348 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001349 printk(KERN_DEBUG MYNAM "%s::mptctl_gettargetinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301350 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -ENODEV;
1352 }
1353
Prakash, Sathya09120a82007-07-24 15:49:05 +05301354 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_gettargetinfo called.\n",
1355 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* Get the port number and set the maximum number of bytes
1357 * in the returned structure.
1358 * Ignore the port setting.
1359 */
1360 numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1361 maxWordsLeft = numBytes/sizeof(int);
1362 port = karg.hdr.port;
1363
1364 if (maxWordsLeft <= 0) {
Eric Moore29dd3602007-09-14 18:46:51 -06001365 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo() - no memory available!\n",
1366 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -ENOMEM;
1368 }
1369
1370 /* Fill in the data and return the structure to the calling
1371 * program
1372 */
1373
1374 /* struct mpt_ioctl_targetinfo does not contain sufficient space
1375 * for the target structures so when the IOCTL is called, there is
1376 * not sufficient stack space for the structure. Allocate memory,
1377 * populate the memory, copy back to the user, then free memory.
1378 * targetInfo format:
1379 * bits 31-24: reserved
1380 * 23-16: LUN
1381 * 15- 8: Bus Number
1382 * 7- 0: Target ID
1383 */
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07001384 pmem = kzalloc(numBytes, GFP_KERNEL);
1385 if (!pmem) {
Eric Moore29dd3602007-09-14 18:46:51 -06001386 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo() - no memory available!\n",
1387 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -ENOMEM;
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 pdata = (int *) pmem;
1391
1392 /* Get number of devices
1393 */
Eric Moore793955f2007-01-29 09:42:20 -07001394 if (ioc->sh){
1395 shost_for_each_device(sdev, ioc->sh) {
1396 if (!maxWordsLeft)
1397 continue;
Eric Moorea69de502007-09-14 18:48:19 -06001398 vdevice = sdev->hostdata;
1399 if (vdevice->vtarget->tflags &
Eric Moore793955f2007-01-29 09:42:20 -07001400 MPT_TARGET_FLAGS_RAID_COMPONENT)
1401 continue;
Eric Moorea69de502007-09-14 18:48:19 -06001402 lun = (vdevice->vtarget->raidVolume) ? 0x80 : vdevice->lun;
1403 *pdata = (((u8)lun << 16) + (vdevice->vtarget->channel << 8) +
1404 (vdevice->vtarget->id ));
Eric Moore793955f2007-01-29 09:42:20 -07001405 pdata++;
1406 numDevices++;
1407 --maxWordsLeft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 karg.numDevices = numDevices;
1411
1412 /* Copy part of the data from kernel memory to user memory
1413 */
1414 if (copy_to_user((char __user *)arg, &karg,
1415 sizeof(struct mpt_ioctl_targetinfo))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001416 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001418 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 kfree(pmem);
1420 return -EFAULT;
1421 }
1422
1423 /* Copy the remaining data from kernel memory to user memory
1424 */
1425 if (copy_to_user(uarg->targetInfo, pmem, numBytes)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001426 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001428 ioc->name, __FILE__, __LINE__, pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 kfree(pmem);
1430 return -EFAULT;
1431 }
1432
1433 kfree(pmem);
1434
1435 return 0;
1436}
1437
1438/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1439/* MPT IOCTL Test function.
1440 *
1441 * Outputs: None.
1442 * Return: 0 if successful
1443 * -EFAULT if data unavailable
1444 * -ENODEV if no such device/adapter
1445 */
1446static int
1447mptctl_readtest (unsigned long arg)
1448{
1449 struct mpt_ioctl_test __user *uarg = (void __user *) arg;
1450 struct mpt_ioctl_test karg;
1451 MPT_ADAPTER *ioc;
1452 int iocnum;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_test))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001455 printk(KERN_ERR MYNAM "%s@%d::mptctl_readtest - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 "Unable to read in mpt_ioctl_test struct @ %p\n",
1457 __FILE__, __LINE__, uarg);
1458 return -EFAULT;
1459 }
1460
1461 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1462 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001463 printk(KERN_DEBUG MYNAM "%s::mptctl_readtest() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301464 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -ENODEV;
1466 }
1467
Prakash, Sathya09120a82007-07-24 15:49:05 +05301468 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_readtest called.\n",
1469 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Fill in the data and return the structure to the calling
1471 * program
1472 */
1473
1474#ifdef MFCNT
1475 karg.chip_type = ioc->mfcnt;
1476#else
1477 karg.chip_type = ioc->pcidev->device;
1478#endif
1479 strncpy (karg.name, ioc->name, MPT_MAX_NAME);
1480 karg.name[MPT_MAX_NAME-1]='\0';
1481 strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
1482 karg.product[MPT_PRODUCT_LENGTH-1]='\0';
1483
1484 /* Copy the data from kernel memory to user memory
1485 */
1486 if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_test))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001487 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_readtest - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 "Unable to write out mpt_ioctl_test struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001489 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -EFAULT;
1491 }
1492
1493 return 0;
1494}
1495
1496/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1497/*
1498 * mptctl_eventquery - Query the host adapter for the event types
1499 * that are being logged.
1500 * @arg: User space argument
1501 *
1502 * Outputs: None.
1503 * Return: 0 if successful
1504 * -EFAULT if data unavailable
1505 * -ENODEV if no such device/adapter
1506 */
1507static int
1508mptctl_eventquery (unsigned long arg)
1509{
1510 struct mpt_ioctl_eventquery __user *uarg = (void __user *) arg;
1511 struct mpt_ioctl_eventquery karg;
1512 MPT_ADAPTER *ioc;
1513 int iocnum;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventquery))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001516 printk(KERN_ERR MYNAM "%s@%d::mptctl_eventquery - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 "Unable to read in mpt_ioctl_eventquery struct @ %p\n",
1518 __FILE__, __LINE__, uarg);
1519 return -EFAULT;
1520 }
1521
1522 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1523 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001524 printk(KERN_DEBUG MYNAM "%s::mptctl_eventquery() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301525 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -ENODEV;
1527 }
1528
Prakash, Sathya09120a82007-07-24 15:49:05 +05301529 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_eventquery called.\n",
1530 ioc->name));
Moore, Eric5b5ef4f2006-02-02 17:19:40 -07001531 karg.eventEntries = MPTCTL_EVENT_LOG_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 karg.eventTypes = ioc->eventTypes;
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_eventquery))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001537 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_eventquery - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 "Unable to write out mpt_ioctl_eventquery struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001539 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -EFAULT;
1541 }
1542 return 0;
1543}
1544
1545/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1546static int
1547mptctl_eventenable (unsigned long arg)
1548{
1549 struct mpt_ioctl_eventenable __user *uarg = (void __user *) arg;
1550 struct mpt_ioctl_eventenable karg;
1551 MPT_ADAPTER *ioc;
1552 int iocnum;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventenable))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001555 printk(KERN_ERR MYNAM "%s@%d::mptctl_eventenable - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 "Unable to read in mpt_ioctl_eventenable struct @ %p\n",
1557 __FILE__, __LINE__, uarg);
1558 return -EFAULT;
1559 }
1560
1561 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1562 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001563 printk(KERN_DEBUG MYNAM "%s::mptctl_eventenable() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301564 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return -ENODEV;
1566 }
1567
Prakash, Sathya09120a82007-07-24 15:49:05 +05301568 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_eventenable called.\n",
1569 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (ioc->events == NULL) {
1571 /* Have not yet allocated memory - do so now.
1572 */
1573 int sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07001574 ioc->events = kzalloc(sz, GFP_KERNEL);
1575 if (!ioc->events) {
Eric Moore29dd3602007-09-14 18:46:51 -06001576 printk(MYIOC_s_ERR_FMT
1577 ": ERROR - Insufficient memory to add adapter!\n",
1578 ioc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return -ENOMEM;
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 ioc->alloc_total += sz;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ioc->eventContext = 0;
1584 }
1585
1586 /* Update the IOC event logging flag.
1587 */
1588 ioc->eventTypes = karg.eventTypes;
1589
1590 return 0;
1591}
1592
1593/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1594static int
1595mptctl_eventreport (unsigned long arg)
1596{
1597 struct mpt_ioctl_eventreport __user *uarg = (void __user *) arg;
1598 struct mpt_ioctl_eventreport karg;
1599 MPT_ADAPTER *ioc;
1600 int iocnum;
1601 int numBytes, maxEvents, max;
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventreport))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001604 printk(KERN_ERR MYNAM "%s@%d::mptctl_eventreport - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 "Unable to read in mpt_ioctl_eventreport struct @ %p\n",
1606 __FILE__, __LINE__, uarg);
1607 return -EFAULT;
1608 }
1609
1610 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1611 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001612 printk(KERN_DEBUG MYNAM "%s::mptctl_eventreport() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301613 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return -ENODEV;
1615 }
Prakash, Sathya09120a82007-07-24 15:49:05 +05301616 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_eventreport called.\n",
1617 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1620 maxEvents = numBytes/sizeof(MPT_IOCTL_EVENTS);
1621
1622
Moore, Eric5b5ef4f2006-02-02 17:19:40 -07001623 max = MPTCTL_EVENT_LOG_SIZE < maxEvents ? MPTCTL_EVENT_LOG_SIZE : maxEvents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 /* If fewer than 1 event is requested, there must have
1626 * been some type of error.
1627 */
1628 if ((max < 1) || !ioc->events)
1629 return -ENODATA;
1630
Moore, Ericea5a7a822006-02-02 17:20:01 -07001631 /* reset this flag so SIGIO can restart */
1632 ioc->aen_event_read_flag=0;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Copy the data from kernel memory to user memory
1635 */
1636 numBytes = max * sizeof(MPT_IOCTL_EVENTS);
1637 if (copy_to_user(uarg->eventData, ioc->events, numBytes)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001638 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_eventreport - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 "Unable to write out mpt_ioctl_eventreport struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001640 ioc->name, __FILE__, __LINE__, ioc->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return -EFAULT;
1642 }
1643
1644 return 0;
1645}
1646
1647/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1648static int
1649mptctl_replace_fw (unsigned long arg)
1650{
1651 struct mpt_ioctl_replace_fw __user *uarg = (void __user *) arg;
1652 struct mpt_ioctl_replace_fw karg;
1653 MPT_ADAPTER *ioc;
1654 int iocnum;
1655 int newFwSize;
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_replace_fw))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001658 printk(KERN_ERR MYNAM "%s@%d::mptctl_replace_fw - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 "Unable to read in mpt_ioctl_replace_fw struct @ %p\n",
1660 __FILE__, __LINE__, uarg);
1661 return -EFAULT;
1662 }
1663
1664 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1665 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001666 printk(KERN_DEBUG MYNAM "%s::mptctl_replace_fw() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301667 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return -ENODEV;
1669 }
1670
Prakash, Sathya09120a82007-07-24 15:49:05 +05301671 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_replace_fw called.\n",
1672 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* If caching FW, Free the old FW image
1674 */
1675 if (ioc->cached_fw == NULL)
1676 return 0;
1677
1678 mpt_free_fw_memory(ioc);
1679
1680 /* Allocate memory for the new FW image
1681 */
1682 newFwSize = karg.newImageSize;
1683
1684 if (newFwSize & 0x01)
1685 newFwSize += 1;
1686 if (newFwSize & 0x02)
1687 newFwSize += 2;
1688
1689 mpt_alloc_fw_memory(ioc, newFwSize);
1690 if (ioc->cached_fw == NULL)
1691 return -ENOMEM;
1692
1693 /* Copy the data from user memory to kernel space
1694 */
1695 if (copy_from_user(ioc->cached_fw, uarg->newImage, newFwSize)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001696 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_replace_fw - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 "Unable to read in mpt_ioctl_replace_fw image "
Eric Moore29dd3602007-09-14 18:46:51 -06001698 "@ %p\n", ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 mpt_free_fw_memory(ioc);
1700 return -EFAULT;
1701 }
1702
1703 /* Update IOCFactsReply
1704 */
1705 ioc->facts.FWImageSize = newFwSize;
1706 return 0;
1707}
1708
1709/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1710/* MPT IOCTL MPTCOMMAND function.
1711 * Cast the arg into the mpt_ioctl_mpt_command structure.
1712 *
1713 * Outputs: None.
1714 * Return: 0 if successful
Joe Perchesfc1323b2008-02-03 17:21:01 +02001715 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 * -EFAULT if data unavailable
1717 * -ENODEV if no such device/adapter
1718 * -ETIME if timer expires
1719 * -ENOMEM if memory allocation error
1720 */
1721static int
1722mptctl_mpt_command (unsigned long arg)
1723{
1724 struct mpt_ioctl_command __user *uarg = (void __user *) arg;
1725 struct mpt_ioctl_command karg;
1726 MPT_ADAPTER *ioc;
1727 int iocnum;
1728 int rc;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_command))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001732 printk(KERN_ERR MYNAM "%s@%d::mptctl_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 "Unable to read in mpt_ioctl_command struct @ %p\n",
1734 __FILE__, __LINE__, uarg);
1735 return -EFAULT;
1736 }
1737
1738 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1739 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001740 printk(KERN_DEBUG MYNAM "%s::mptctl_mpt_command() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301741 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return -ENODEV;
1743 }
1744
1745 rc = mptctl_do_mpt_command (karg, &uarg->MF);
1746
1747 return rc;
1748}
1749
1750/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1751/* Worker routine for the IOCTL MPTCOMMAND and MPTCOMMAND32 (sparc) commands.
1752 *
1753 * Outputs: None.
1754 * Return: 0 if successful
Joe Perchesfc1323b2008-02-03 17:21:01 +02001755 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * -EFAULT if data unavailable
1757 * -ENODEV if no such device/adapter
1758 * -ETIME if timer expires
1759 * -ENOMEM if memory allocation error
1760 * -EPERM if SCSI I/O and target is untagged
1761 */
1762static int
1763mptctl_do_mpt_command (struct mpt_ioctl_command karg, void __user *mfPtr)
1764{
1765 MPT_ADAPTER *ioc;
1766 MPT_FRAME_HDR *mf = NULL;
1767 MPIHeader_t *hdr;
1768 char *psge;
1769 struct buflist bufIn; /* data In buffer */
1770 struct buflist bufOut; /* data Out buffer */
1771 dma_addr_t dma_addr_in;
1772 dma_addr_t dma_addr_out;
1773 int sgSize = 0; /* Num SG elements */
1774 int iocnum, flagsLength;
1775 int sz, rc = 0;
1776 int msgContext;
1777 u16 req_idx;
1778 ulong timeout;
Eric Moore793955f2007-01-29 09:42:20 -07001779 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Eric Mooreab371282007-09-29 10:22:54 -06001781 /* bufIn and bufOut are used for user to kernel space transfers
1782 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 bufIn.kptr = bufOut.kptr = NULL;
Eric Mooreab371282007-09-29 10:22:54 -06001784 bufIn.len = bufOut.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1787 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001788 printk(KERN_DEBUG MYNAM "%s::mptctl_do_mpt_command() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301789 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return -ENODEV;
1791 }
1792 if (!ioc->ioctl) {
Eric Moore29dd3602007-09-14 18:46:51 -06001793 printk(KERN_ERR MYNAM "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 "No memory available during driver init.\n",
1795 __FILE__, __LINE__);
1796 return -ENOMEM;
1797 } else if (ioc->ioctl->status & MPT_IOCTL_STATUS_DID_IOCRESET) {
Eric Moore29dd3602007-09-14 18:46:51 -06001798 printk(KERN_ERR MYNAM "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 "Busy with IOC Reset \n", __FILE__, __LINE__);
1800 return -EBUSY;
1801 }
1802
1803 /* Verify that the final request frame will not be too large.
1804 */
1805 sz = karg.dataSgeOffset * 4;
1806 if (karg.dataInSize > 0)
1807 sz += sizeof(dma_addr_t) + sizeof(u32);
1808 if (karg.dataOutSize > 0)
1809 sz += sizeof(dma_addr_t) + sizeof(u32);
1810
1811 if (sz > ioc->req_sz) {
Eric Moore29dd3602007-09-14 18:46:51 -06001812 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 "Request frame too large (%d) maximum (%d)\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001814 ioc->name, __FILE__, __LINE__, sz, ioc->req_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return -EFAULT;
1816 }
1817
1818 /* Get a free request frame and save the message context.
1819 */
1820 if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL)
1821 return -EAGAIN;
1822
1823 hdr = (MPIHeader_t *) mf;
1824 msgContext = le32_to_cpu(hdr->MsgContext);
1825 req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
1826
1827 /* Copy the request frame
1828 * Reset the saved message context.
1829 * Request frame in user space
1830 */
1831 if (copy_from_user(mf, mfPtr, karg.dataSgeOffset * 4)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001832 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 "Unable to read MF from mpt_ioctl_command struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001834 ioc->name, __FILE__, __LINE__, mfPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rc = -EFAULT;
1836 goto done_free_mem;
1837 }
1838 hdr->MsgContext = cpu_to_le32(msgContext);
1839
1840
1841 /* Verify that this request is allowed.
1842 */
Prakash, Sathya09120a82007-07-24 15:49:05 +05301843 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sending mpi function (0x%02X), req=%p\n",
1844 ioc->name, hdr->Function, mf));
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 switch (hdr->Function) {
1847 case MPI_FUNCTION_IOC_FACTS:
1848 case MPI_FUNCTION_PORT_FACTS:
1849 karg.dataOutSize = karg.dataInSize = 0;
1850 break;
1851
1852 case MPI_FUNCTION_CONFIG:
Prakash, Sathya09120a82007-07-24 15:49:05 +05301853 {
1854 Config_t *config_frame;
1855 config_frame = (Config_t *)mf;
1856 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\ttype=0x%02x ext_type=0x%02x "
1857 "number=0x%02x action=0x%02x\n", ioc->name,
1858 config_frame->Header.PageType,
1859 config_frame->ExtPageType,
1860 config_frame->Header.PageNumber,
1861 config_frame->Action));
1862 break;
1863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 case MPI_FUNCTION_FC_COMMON_TRANSPORT_SEND:
1866 case MPI_FUNCTION_FC_EX_LINK_SRVC_SEND:
1867 case MPI_FUNCTION_FW_UPLOAD:
1868 case MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
1869 case MPI_FUNCTION_FW_DOWNLOAD:
1870 case MPI_FUNCTION_FC_PRIMITIVE_SEND:
Moore, Eric096f7a22006-02-02 17:19:30 -07001871 case MPI_FUNCTION_TOOLBOX:
1872 case MPI_FUNCTION_SAS_IO_UNIT_CONTROL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874
1875 case MPI_FUNCTION_SCSI_IO_REQUEST:
1876 if (ioc->sh) {
1877 SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 int qtag = MPI_SCSIIO_CONTROL_UNTAGGED;
1879 int scsidir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int dataSize;
Eric Moore793955f2007-01-29 09:42:20 -07001881 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Eric Moore793955f2007-01-29 09:42:20 -07001883 id = (ioc->devices_per_bus == 0) ? 256 : ioc->devices_per_bus;
1884 if (pScsiReq->TargetID > id) {
Eric Moore29dd3602007-09-14 18:46:51 -06001885 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 "Target ID out of bounds. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001887 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 rc = -ENODEV;
1889 goto done_free_mem;
1890 }
1891
Eric Moore793955f2007-01-29 09:42:20 -07001892 if (pScsiReq->Bus >= ioc->number_of_buses) {
Eric Moore29dd3602007-09-14 18:46:51 -06001893 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Eric Moore793955f2007-01-29 09:42:20 -07001894 "Target Bus out of bounds. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001895 ioc->name, __FILE__, __LINE__);
Eric Moore793955f2007-01-29 09:42:20 -07001896 rc = -ENODEV;
1897 goto done_free_mem;
1898 }
1899
Moore, Eric5f07e242006-02-02 17:19:44 -07001900 pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
1901 pScsiReq->MsgFlags |= mpt_msg_flags();
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* verify that app has not requested
1905 * more sense data than driver
1906 * can provide, if so, reset this parameter
1907 * set the sense buffer pointer low address
1908 * update the control field to specify Q type
1909 */
1910 if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1911 pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1912 else
1913 pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1914
1915 pScsiReq->SenseBufferLowAddr =
1916 cpu_to_le32(ioc->sense_buf_low_dma
1917 + (req_idx * MPT_SENSE_BUFFER_ALLOC));
1918
Eric Moore793955f2007-01-29 09:42:20 -07001919 shost_for_each_device(sdev, ioc->sh) {
1920 struct scsi_target *starget = scsi_target(sdev);
1921 VirtTarget *vtarget = starget->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Eric Moore793955f2007-01-29 09:42:20 -07001923 if ((pScsiReq->TargetID == vtarget->id) &&
1924 (pScsiReq->Bus == vtarget->channel) &&
1925 (vtarget->tflags & MPT_TARGET_FLAGS_Q_YES))
1926 qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 /* Have the IOCTL driver set the direction based
1930 * on the dataOutSize (ordering issue with Sparc).
1931 */
1932 if (karg.dataOutSize > 0) {
1933 scsidir = MPI_SCSIIO_CONTROL_WRITE;
1934 dataSize = karg.dataOutSize;
1935 } else {
1936 scsidir = MPI_SCSIIO_CONTROL_READ;
1937 dataSize = karg.dataInSize;
1938 }
1939
1940 pScsiReq->Control = cpu_to_le32(scsidir | qtag);
1941 pScsiReq->DataLength = cpu_to_le32(dataSize);
1942
1943 ioc->ioctl->reset = MPTCTL_RESET_OK;
Eric Moore793955f2007-01-29 09:42:20 -07001944 ioc->ioctl->id = pScsiReq->TargetID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 } else {
Eric Moore29dd3602007-09-14 18:46:51 -06001947 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 "SCSI driver is not loaded. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001949 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 rc = -EFAULT;
1951 goto done_free_mem;
1952 }
1953 break;
1954
Moore, Eric096f7a22006-02-02 17:19:30 -07001955 case MPI_FUNCTION_SMP_PASSTHROUGH:
1956 /* Check mf->PassthruFlags to determine if
1957 * transfer is ImmediateMode or not.
1958 * Immediate mode returns data in the ReplyFrame.
1959 * Else, we are sending request and response data
1960 * in two SGLs at the end of the mf.
1961 */
1962 break;
1963
1964 case MPI_FUNCTION_SATA_PASSTHROUGH:
1965 if (!ioc->sh) {
Eric Moore29dd3602007-09-14 18:46:51 -06001966 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Moore, Eric096f7a22006-02-02 17:19:30 -07001967 "SCSI driver is not loaded. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001968 ioc->name, __FILE__, __LINE__);
Moore, Eric096f7a22006-02-02 17:19:30 -07001969 rc = -EFAULT;
1970 goto done_free_mem;
1971 }
1972 break;
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 case MPI_FUNCTION_RAID_ACTION:
1975 /* Just add a SGE
1976 */
1977 break;
1978
1979 case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1980 if (ioc->sh) {
1981 SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
1982 int qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1983 int scsidir = MPI_SCSIIO_CONTROL_READ;
1984 int dataSize;
1985
Moore, Eric5f07e242006-02-02 17:19:44 -07001986 pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
1987 pScsiReq->MsgFlags |= mpt_msg_flags();
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /* verify that app has not requested
1991 * more sense data than driver
1992 * can provide, if so, reset this parameter
1993 * set the sense buffer pointer low address
1994 * update the control field to specify Q type
1995 */
1996 if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1997 pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1998 else
1999 pScsiReq->SenseBufferLength = karg.maxSenseBytes;
2000
2001 pScsiReq->SenseBufferLowAddr =
2002 cpu_to_le32(ioc->sense_buf_low_dma
2003 + (req_idx * MPT_SENSE_BUFFER_ALLOC));
2004
2005 /* All commands to physical devices are tagged
2006 */
2007
2008 /* Have the IOCTL driver set the direction based
2009 * on the dataOutSize (ordering issue with Sparc).
2010 */
2011 if (karg.dataOutSize > 0) {
2012 scsidir = MPI_SCSIIO_CONTROL_WRITE;
2013 dataSize = karg.dataOutSize;
2014 } else {
2015 scsidir = MPI_SCSIIO_CONTROL_READ;
2016 dataSize = karg.dataInSize;
2017 }
2018
2019 pScsiReq->Control = cpu_to_le32(scsidir | qtag);
2020 pScsiReq->DataLength = cpu_to_le32(dataSize);
2021
2022 ioc->ioctl->reset = MPTCTL_RESET_OK;
Eric Moore793955f2007-01-29 09:42:20 -07002023 ioc->ioctl->id = pScsiReq->TargetID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 } else {
Eric Moore29dd3602007-09-14 18:46:51 -06002025 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 "SCSI driver is not loaded. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06002027 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 rc = -EFAULT;
2029 goto done_free_mem;
2030 }
2031 break;
2032
2033 case MPI_FUNCTION_SCSI_TASK_MGMT:
2034 {
2035 MPT_SCSI_HOST *hd = NULL;
Eric Mooree7eae9f2007-09-29 10:15:59 -06002036 if ((ioc->sh == NULL) || ((hd = shost_priv(ioc->sh)) == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002037 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 "SCSI driver not loaded or SCSI host not found. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06002039 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 rc = -EFAULT;
2041 goto done_free_mem;
2042 } else if (mptctl_set_tm_flags(hd) != 0) {
2043 rc = -EPERM;
2044 goto done_free_mem;
2045 }
2046 }
2047 break;
2048
2049 case MPI_FUNCTION_IOC_INIT:
2050 {
2051 IOCInit_t *pInit = (IOCInit_t *) mf;
2052 u32 high_addr, sense_high;
2053
2054 /* Verify that all entries in the IOC INIT match
2055 * existing setup (and in LE format).
2056 */
2057 if (sizeof(dma_addr_t) == sizeof(u64)) {
2058 high_addr = cpu_to_le32((u32)((u64)ioc->req_frames_dma >> 32));
2059 sense_high= cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
2060 } else {
2061 high_addr = 0;
2062 sense_high= 0;
2063 }
2064
2065 if ((pInit->Flags != 0) || (pInit->MaxDevices != ioc->facts.MaxDevices) ||
2066 (pInit->MaxBuses != ioc->facts.MaxBuses) ||
2067 (pInit->ReplyFrameSize != cpu_to_le16(ioc->reply_sz)) ||
2068 (pInit->HostMfaHighAddr != high_addr) ||
2069 (pInit->SenseBufferHighAddr != sense_high)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002070 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 "IOC_INIT issued with 1 or more incorrect parameters. Rejected.\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002072 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 rc = -EFAULT;
2074 goto done_free_mem;
2075 }
2076 }
2077 break;
2078 default:
2079 /*
2080 * MPI_FUNCTION_PORT_ENABLE
2081 * MPI_FUNCTION_TARGET_CMD_BUFFER_POST
2082 * MPI_FUNCTION_TARGET_ASSIST
2083 * MPI_FUNCTION_TARGET_STATUS_SEND
2084 * MPI_FUNCTION_TARGET_MODE_ABORT
2085 * MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET
2086 * MPI_FUNCTION_IO_UNIT_RESET
2087 * MPI_FUNCTION_HANDSHAKE
2088 * MPI_FUNCTION_REPLY_FRAME_REMOVAL
2089 * MPI_FUNCTION_EVENT_NOTIFICATION
2090 * (driver handles event notification)
2091 * MPI_FUNCTION_EVENT_ACK
2092 */
2093
2094 /* What to do with these??? CHECK ME!!!
2095 MPI_FUNCTION_FC_LINK_SRVC_BUF_POST
2096 MPI_FUNCTION_FC_LINK_SRVC_RSP
2097 MPI_FUNCTION_FC_ABORT
2098 MPI_FUNCTION_LAN_SEND
2099 MPI_FUNCTION_LAN_RECEIVE
2100 MPI_FUNCTION_LAN_RESET
2101 */
2102
Eric Moore29dd3602007-09-14 18:46:51 -06002103 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 "Illegal request (function 0x%x) \n",
Eric Moore29dd3602007-09-14 18:46:51 -06002105 ioc->name, __FILE__, __LINE__, hdr->Function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 rc = -EFAULT;
2107 goto done_free_mem;
2108 }
2109
2110 /* Add the SGL ( at most one data in SGE and one data out SGE )
2111 * In the case of two SGE's - the data out (write) will always
2112 * preceede the data in (read) SGE. psgList is used to free the
2113 * allocated memory.
2114 */
2115 psge = (char *) (((int *) mf) + karg.dataSgeOffset);
2116 flagsLength = 0;
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (karg.dataOutSize > 0)
2119 sgSize ++;
2120
2121 if (karg.dataInSize > 0)
2122 sgSize ++;
2123
2124 if (sgSize > 0) {
2125
2126 /* Set up the dataOut memory allocation */
2127 if (karg.dataOutSize > 0) {
2128 if (karg.dataInSize > 0) {
2129 flagsLength = ( MPI_SGE_FLAGS_SIMPLE_ELEMENT |
2130 MPI_SGE_FLAGS_END_OF_BUFFER |
2131 MPI_SGE_FLAGS_DIRECTION |
2132 mpt_addr_size() )
2133 << MPI_SGE_FLAGS_SHIFT;
2134 } else {
2135 flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE;
2136 }
2137 flagsLength |= karg.dataOutSize;
2138 bufOut.len = karg.dataOutSize;
2139 bufOut.kptr = pci_alloc_consistent(
2140 ioc->pcidev, bufOut.len, &dma_addr_out);
2141
2142 if (bufOut.kptr == NULL) {
2143 rc = -ENOMEM;
2144 goto done_free_mem;
2145 } else {
2146 /* Set up this SGE.
2147 * Copy to MF and to sglbuf
2148 */
2149 mpt_add_sge(psge, flagsLength, dma_addr_out);
2150 psge += (sizeof(u32) + sizeof(dma_addr_t));
2151
2152 /* Copy user data to kernel space.
2153 */
2154 if (copy_from_user(bufOut.kptr,
2155 karg.dataOutBufPtr,
2156 bufOut.len)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002157 printk(MYIOC_s_ERR_FMT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 "%s@%d::mptctl_do_mpt_command - Unable "
2159 "to read user data "
2160 "struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002161 ioc->name, __FILE__, __LINE__,karg.dataOutBufPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 rc = -EFAULT;
2163 goto done_free_mem;
2164 }
2165 }
2166 }
2167
2168 if (karg.dataInSize > 0) {
2169 flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
2170 flagsLength |= karg.dataInSize;
2171
2172 bufIn.len = karg.dataInSize;
2173 bufIn.kptr = pci_alloc_consistent(ioc->pcidev,
2174 bufIn.len, &dma_addr_in);
2175
2176 if (bufIn.kptr == NULL) {
2177 rc = -ENOMEM;
2178 goto done_free_mem;
2179 } else {
2180 /* Set up this SGE
2181 * Copy to MF and to sglbuf
2182 */
2183 mpt_add_sge(psge, flagsLength, dma_addr_in);
2184 }
2185 }
2186 } else {
2187 /* Add a NULL SGE
2188 */
2189 mpt_add_sge(psge, flagsLength, (dma_addr_t) -1);
2190 }
2191
2192 ioc->ioctl->wait_done = 0;
2193 if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT) {
2194
Prakash, Sathya09120a82007-07-24 15:49:05 +05302195 DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Prakash, Sathya7a195f42007-08-14 16:08:40 +05302197 if ((ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
2198 (ioc->facts.MsgVersion >= MPI_VERSION_01_05))
2199 mpt_put_msg_frame_hi_pri(mptctl_id, ioc, mf);
2200 else {
2201 rc =mpt_send_handshake_request(mptctl_id, ioc,
2202 sizeof(SCSITaskMgmt_t), (u32*)mf, CAN_SLEEP);
2203 if (rc != 0) {
2204 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2205 "_send_handshake FAILED! (ioc %p, mf %p)\n",
2206 ioc->name, ioc, mf));
2207 mptctl_free_tm_flags(ioc);
2208 rc = -ENODATA;
2209 goto done_free_mem;
2210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212
2213 } else
2214 mpt_put_msg_frame(mptctl_id, ioc, mf);
2215
2216 /* Now wait for the command to complete */
2217 timeout = (karg.timeout > 0) ? karg.timeout : MPT_IOCTL_DEFAULT_TIMEOUT;
Moore, Eric86a7dca2006-02-02 17:19:37 -07002218 timeout = wait_event_timeout(mptctl_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 ioc->ioctl->wait_done == 1,
2220 HZ*timeout);
2221
2222 if(timeout <=0 && (ioc->ioctl->wait_done != 1 )) {
2223 /* Now we need to reset the board */
2224
2225 if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT)
2226 mptctl_free_tm_flags(ioc);
2227
2228 mptctl_timeout_expired(ioc->ioctl);
2229 rc = -ENODATA;
2230 goto done_free_mem;
2231 }
2232
2233 mf = NULL;
2234
2235 /* If a valid reply frame, copy to the user.
2236 * Offset 2: reply length in U32's
2237 */
2238 if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID) {
2239 if (karg.maxReplyBytes < ioc->reply_sz) {
2240 sz = min(karg.maxReplyBytes, 4*ioc->ioctl->ReplyFrame[2]);
2241 } else {
2242 sz = min(ioc->reply_sz, 4*ioc->ioctl->ReplyFrame[2]);
2243 }
2244
2245 if (sz > 0) {
2246 if (copy_to_user(karg.replyFrameBufPtr,
2247 &ioc->ioctl->ReplyFrame, sz)){
Eric Moore29dd3602007-09-14 18:46:51 -06002248 printk(MYIOC_s_ERR_FMT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 "%s@%d::mptctl_do_mpt_command - "
2250 "Unable to write out reply frame %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002251 ioc->name, __FILE__, __LINE__, karg.replyFrameBufPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 rc = -ENODATA;
2253 goto done_free_mem;
2254 }
2255 }
2256 }
2257
2258 /* If valid sense data, copy to user.
2259 */
2260 if (ioc->ioctl->status & MPT_IOCTL_STATUS_SENSE_VALID) {
2261 sz = min(karg.maxSenseBytes, MPT_SENSE_BUFFER_SIZE);
2262 if (sz > 0) {
2263 if (copy_to_user(karg.senseDataPtr, ioc->ioctl->sense, sz)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002264 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 "Unable to write sense data to user %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002266 ioc->name, __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 karg.senseDataPtr);
2268 rc = -ENODATA;
2269 goto done_free_mem;
2270 }
2271 }
2272 }
2273
2274 /* If the overall status is _GOOD and data in, copy data
2275 * to user.
2276 */
2277 if ((ioc->ioctl->status & MPT_IOCTL_STATUS_COMMAND_GOOD) &&
2278 (karg.dataInSize > 0) && (bufIn.kptr)) {
2279
2280 if (copy_to_user(karg.dataInBufPtr,
2281 bufIn.kptr, karg.dataInSize)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002282 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 "Unable to write data to user %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002284 ioc->name, __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 karg.dataInBufPtr);
2286 rc = -ENODATA;
2287 }
2288 }
2289
2290done_free_mem:
2291
2292 ioc->ioctl->status &= ~(MPT_IOCTL_STATUS_COMMAND_GOOD |
2293 MPT_IOCTL_STATUS_SENSE_VALID |
2294 MPT_IOCTL_STATUS_RF_VALID );
2295
2296 /* Free the allocated memory.
2297 */
2298 if (bufOut.kptr != NULL) {
2299 pci_free_consistent(ioc->pcidev,
2300 bufOut.len, (void *) bufOut.kptr, dma_addr_out);
2301 }
2302
2303 if (bufIn.kptr != NULL) {
2304 pci_free_consistent(ioc->pcidev,
2305 bufIn.len, (void *) bufIn.kptr, dma_addr_in);
2306 }
2307
2308 /* mf is null if command issued successfully
2309 * otherwise, failure occured after mf acquired.
2310 */
2311 if (mf)
2312 mpt_free_msg_frame(ioc, mf);
2313
2314 return rc;
2315}
2316
2317/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Eric Mooreba856d32006-07-11 17:34:01 -06002318/* Prototype Routine for the HOST INFO command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 *
2320 * Outputs: None.
2321 * Return: 0 if successful
2322 * -EFAULT if data unavailable
Joe Perchesfc1323b2008-02-03 17:21:01 +02002323 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 * -ENODEV if no such device/adapter
2325 * -ETIME if timer expires
2326 * -ENOMEM if memory allocation error
2327 */
2328static int
2329mptctl_hp_hostinfo(unsigned long arg, unsigned int data_size)
2330{
2331 hp_host_info_t __user *uarg = (void __user *) arg;
2332 MPT_ADAPTER *ioc;
2333 struct pci_dev *pdev;
Moore, Eric592f9c22006-02-02 17:19:47 -07002334 char *pbuf=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 dma_addr_t buf_dma;
2336 hp_host_info_t karg;
2337 CONFIGPARMS cfg;
2338 ConfigPageHeader_t hdr;
2339 int iocnum;
2340 int rc, cim_rev;
Moore, Eric592f9c22006-02-02 17:19:47 -07002341 ToolboxIstwiReadWriteRequest_t *IstwiRWRequest;
2342 MPT_FRAME_HDR *mf = NULL;
2343 MPIHeader_t *mpi_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* Reset long to int. Should affect IA64 and SPARC only
2346 */
2347 if (data_size == sizeof(hp_host_info_t))
2348 cim_rev = 1;
2349 else if (data_size == sizeof(hp_host_info_rev0_t))
2350 cim_rev = 0; /* obsolete */
2351 else
2352 return -EFAULT;
2353
2354 if (copy_from_user(&karg, uarg, sizeof(hp_host_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002355 printk(KERN_ERR MYNAM "%s@%d::mptctl_hp_host_info - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 "Unable to read in hp_host_info struct @ %p\n",
2357 __FILE__, __LINE__, uarg);
2358 return -EFAULT;
2359 }
2360
2361 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2362 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002363 printk(KERN_DEBUG MYNAM "%s::mptctl_hp_hostinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05302364 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return -ENODEV;
2366 }
Prakash, Sathya09120a82007-07-24 15:49:05 +05302367 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT ": mptctl_hp_hostinfo called.\n",
2368 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 /* Fill in the data and return the structure to the calling
2371 * program
2372 */
2373 pdev = (struct pci_dev *) ioc->pcidev;
2374
2375 karg.vendor = pdev->vendor;
2376 karg.device = pdev->device;
2377 karg.subsystem_id = pdev->subsystem_device;
2378 karg.subsystem_vendor = pdev->subsystem_vendor;
2379 karg.devfn = pdev->devfn;
2380 karg.bus = pdev->bus->number;
2381
2382 /* Save the SCSI host no. if
2383 * SCSI driver loaded
2384 */
2385 if (ioc->sh != NULL)
2386 karg.host_no = ioc->sh->host_no;
2387 else
2388 karg.host_no = -1;
2389
2390 /* Reformat the fw_version into a string
2391 */
2392 karg.fw_version[0] = ioc->facts.FWVersion.Struct.Major >= 10 ?
2393 ((ioc->facts.FWVersion.Struct.Major / 10) + '0') : '0';
2394 karg.fw_version[1] = (ioc->facts.FWVersion.Struct.Major % 10 ) + '0';
2395 karg.fw_version[2] = '.';
2396 karg.fw_version[3] = ioc->facts.FWVersion.Struct.Minor >= 10 ?
2397 ((ioc->facts.FWVersion.Struct.Minor / 10) + '0') : '0';
2398 karg.fw_version[4] = (ioc->facts.FWVersion.Struct.Minor % 10 ) + '0';
2399 karg.fw_version[5] = '.';
2400 karg.fw_version[6] = ioc->facts.FWVersion.Struct.Unit >= 10 ?
2401 ((ioc->facts.FWVersion.Struct.Unit / 10) + '0') : '0';
2402 karg.fw_version[7] = (ioc->facts.FWVersion.Struct.Unit % 10 ) + '0';
2403 karg.fw_version[8] = '.';
2404 karg.fw_version[9] = ioc->facts.FWVersion.Struct.Dev >= 10 ?
2405 ((ioc->facts.FWVersion.Struct.Dev / 10) + '0') : '0';
2406 karg.fw_version[10] = (ioc->facts.FWVersion.Struct.Dev % 10 ) + '0';
2407 karg.fw_version[11] = '\0';
2408
2409 /* Issue a config request to get the device serial number
2410 */
2411 hdr.PageVersion = 0;
2412 hdr.PageLength = 0;
2413 hdr.PageNumber = 0;
2414 hdr.PageType = MPI_CONFIG_PAGETYPE_MANUFACTURING;
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002415 cfg.cfghdr.hdr = &hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 cfg.physAddr = -1;
2417 cfg.pageAddr = 0;
2418 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2419 cfg.dir = 0; /* read */
2420 cfg.timeout = 10;
2421
2422 strncpy(karg.serial_number, " ", 24);
2423 if (mpt_config(ioc, &cfg) == 0) {
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002424 if (cfg.cfghdr.hdr->PageLength > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* Issue the second config page request */
2426 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2427
2428 pbuf = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4, &buf_dma);
2429 if (pbuf) {
2430 cfg.physAddr = buf_dma;
2431 if (mpt_config(ioc, &cfg) == 0) {
2432 ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;
2433 if (strlen(pdata->BoardTracerNumber) > 1) {
2434 strncpy(karg.serial_number, pdata->BoardTracerNumber, 24);
2435 karg.serial_number[24-1]='\0';
2436 }
2437 }
2438 pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, pbuf, buf_dma);
2439 pbuf = NULL;
2440 }
2441 }
2442 }
2443 rc = mpt_GetIocState(ioc, 1);
2444 switch (rc) {
2445 case MPI_IOC_STATE_OPERATIONAL:
2446 karg.ioc_status = HP_STATUS_OK;
2447 break;
2448
2449 case MPI_IOC_STATE_FAULT:
2450 karg.ioc_status = HP_STATUS_FAILED;
2451 break;
2452
2453 case MPI_IOC_STATE_RESET:
2454 case MPI_IOC_STATE_READY:
2455 default:
2456 karg.ioc_status = HP_STATUS_OTHER;
2457 break;
2458 }
2459
2460 karg.base_io_addr = pci_resource_start(pdev, 0);
2461
Moore, Eric9cc1cfb2006-02-02 17:19:33 -07002462 if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 karg.bus_phys_width = HP_BUS_WIDTH_UNK;
2464 else
2465 karg.bus_phys_width = HP_BUS_WIDTH_16;
2466
2467 karg.hard_resets = 0;
2468 karg.soft_resets = 0;
2469 karg.timeouts = 0;
2470 if (ioc->sh != NULL) {
Eric Mooree7eae9f2007-09-29 10:15:59 -06002471 MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 if (hd && (cim_rev == 1)) {
2474 karg.hard_resets = hd->hard_resets;
2475 karg.soft_resets = hd->soft_resets;
2476 karg.timeouts = hd->timeouts;
2477 }
2478 }
2479
Moore, Eric592f9c22006-02-02 17:19:47 -07002480 /*
2481 * Gather ISTWI(Industry Standard Two Wire Interface) Data
2482 */
2483 if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302484 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, no msg frames!!\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002485 ioc->name,__func__));
Moore, Eric592f9c22006-02-02 17:19:47 -07002486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
2488
Moore, Eric592f9c22006-02-02 17:19:47 -07002489 IstwiRWRequest = (ToolboxIstwiReadWriteRequest_t *)mf;
2490 mpi_hdr = (MPIHeader_t *) mf;
2491 memset(IstwiRWRequest,0,sizeof(ToolboxIstwiReadWriteRequest_t));
2492 IstwiRWRequest->Function = MPI_FUNCTION_TOOLBOX;
2493 IstwiRWRequest->Tool = MPI_TOOLBOX_ISTWI_READ_WRITE_TOOL;
2494 IstwiRWRequest->MsgContext = mpi_hdr->MsgContext;
2495 IstwiRWRequest->Flags = MPI_TB_ISTWI_FLAGS_READ;
2496 IstwiRWRequest->NumAddressBytes = 0x01;
2497 IstwiRWRequest->DataLength = cpu_to_le16(0x04);
2498 if (pdev->devfn & 1)
2499 IstwiRWRequest->DeviceAddr = 0xB2;
2500 else
2501 IstwiRWRequest->DeviceAddr = 0xB0;
2502
2503 pbuf = pci_alloc_consistent(ioc->pcidev, 4, &buf_dma);
2504 if (!pbuf)
2505 goto out;
2506 mpt_add_sge((char *)&IstwiRWRequest->SGL,
2507 (MPT_SGE_FLAGS_SSIMPLE_READ|4), buf_dma);
2508
2509 ioc->ioctl->wait_done = 0;
2510 mpt_put_msg_frame(mptctl_id, ioc, mf);
2511
2512 rc = wait_event_timeout(mptctl_wait,
2513 ioc->ioctl->wait_done == 1,
2514 HZ*MPT_IOCTL_DEFAULT_TIMEOUT /* 10 sec */);
2515
2516 if(rc <=0 && (ioc->ioctl->wait_done != 1 )) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302517 /*
Moore, Eric592f9c22006-02-02 17:19:47 -07002518 * Now we need to reset the board
2519 */
2520 mpt_free_msg_frame(ioc, mf);
2521 mptctl_timeout_expired(ioc->ioctl);
2522 goto out;
2523 }
2524
Prakash, Sathya09120a82007-07-24 15:49:05 +05302525 /*
Moore, Eric592f9c22006-02-02 17:19:47 -07002526 *ISTWI Data Definition
2527 * pbuf[0] = FW_VERSION = 0x4
2528 * pbuf[1] = Bay Count = 6 or 4 or 2, depending on
2529 * the config, you should be seeing one out of these three values
2530 * pbuf[2] = Drive Installed Map = bit pattern depend on which
2531 * bays have drives in them
2532 * pbuf[3] = Checksum (0x100 = (byte0 + byte2 + byte3)
2533 */
2534 if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID)
2535 karg.rsvd = *(u32 *)pbuf;
2536
2537 out:
2538 if (pbuf)
2539 pci_free_consistent(ioc->pcidev, 4, pbuf, buf_dma);
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 /* Copy the data from kernel memory to user memory
2542 */
2543 if (copy_to_user((char __user *)arg, &karg, sizeof(hp_host_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002544 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_hpgethostinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 "Unable to write out hp_host_info @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002546 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return -EFAULT;
2548 }
2549
2550 return 0;
2551
2552}
2553
2554/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Eric Mooreba856d32006-07-11 17:34:01 -06002555/* Prototype Routine for the TARGET INFO command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 *
2557 * Outputs: None.
2558 * Return: 0 if successful
2559 * -EFAULT if data unavailable
Joe Perchesfc1323b2008-02-03 17:21:01 +02002560 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 * -ENODEV if no such device/adapter
2562 * -ETIME if timer expires
2563 * -ENOMEM if memory allocation error
2564 */
2565static int
2566mptctl_hp_targetinfo(unsigned long arg)
2567{
2568 hp_target_info_t __user *uarg = (void __user *) arg;
2569 SCSIDevicePage0_t *pg0_alloc;
2570 SCSIDevicePage3_t *pg3_alloc;
2571 MPT_ADAPTER *ioc;
2572 MPT_SCSI_HOST *hd = NULL;
2573 hp_target_info_t karg;
2574 int iocnum;
2575 int data_sz;
2576 dma_addr_t page_dma;
2577 CONFIGPARMS cfg;
2578 ConfigPageHeader_t hdr;
2579 int tmp, np, rc = 0;
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (copy_from_user(&karg, uarg, sizeof(hp_target_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002582 printk(KERN_ERR MYNAM "%s@%d::mptctl_hp_targetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 "Unable to read in hp_host_targetinfo struct @ %p\n",
2584 __FILE__, __LINE__, uarg);
2585 return -EFAULT;
2586 }
2587
2588 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2589 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002590 printk(KERN_DEBUG MYNAM "%s::mptctl_hp_targetinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05302591 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return -ENODEV;
2593 }
Eric Moore29dd3602007-09-14 18:46:51 -06002594 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_hp_targetinfo called.\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05302595 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 /* There is nothing to do for FCP parts.
2598 */
Moore, Eric9cc1cfb2006-02-02 17:19:33 -07002599 if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
2601
2602 if ((ioc->spi_data.sdp0length == 0) || (ioc->sh == NULL))
2603 return 0;
2604
2605 if (ioc->sh->host_no != karg.hdr.host)
2606 return -ENODEV;
2607
2608 /* Get the data transfer speeds
2609 */
2610 data_sz = ioc->spi_data.sdp0length * 4;
2611 pg0_alloc = (SCSIDevicePage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page_dma);
2612 if (pg0_alloc) {
2613 hdr.PageVersion = ioc->spi_data.sdp0version;
2614 hdr.PageLength = data_sz;
2615 hdr.PageNumber = 0;
2616 hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2617
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002618 cfg.cfghdr.hdr = &hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2620 cfg.dir = 0;
2621 cfg.timeout = 0;
2622 cfg.physAddr = page_dma;
2623
2624 cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2625
2626 if ((rc = mpt_config(ioc, &cfg)) == 0) {
2627 np = le32_to_cpu(pg0_alloc->NegotiatedParameters);
2628 karg.negotiated_width = np & MPI_SCSIDEVPAGE0_NP_WIDE ?
2629 HP_BUS_WIDTH_16 : HP_BUS_WIDTH_8;
2630
2631 if (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) {
2632 tmp = (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
2633 if (tmp < 0x09)
2634 karg.negotiated_speed = HP_DEV_SPEED_ULTRA320;
2635 else if (tmp <= 0x09)
2636 karg.negotiated_speed = HP_DEV_SPEED_ULTRA160;
2637 else if (tmp <= 0x0A)
2638 karg.negotiated_speed = HP_DEV_SPEED_ULTRA2;
2639 else if (tmp <= 0x0C)
2640 karg.negotiated_speed = HP_DEV_SPEED_ULTRA;
2641 else if (tmp <= 0x25)
2642 karg.negotiated_speed = HP_DEV_SPEED_FAST;
2643 else
2644 karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2645 } else
2646 karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2647 }
2648
2649 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg0_alloc, page_dma);
2650 }
2651
2652 /* Set defaults
2653 */
2654 karg.message_rejects = -1;
2655 karg.phase_errors = -1;
2656 karg.parity_errors = -1;
2657 karg.select_timeouts = -1;
2658
2659 /* Get the target error parameters
2660 */
2661 hdr.PageVersion = 0;
2662 hdr.PageLength = 0;
2663 hdr.PageNumber = 3;
2664 hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2665
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002666 cfg.cfghdr.hdr = &hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2668 cfg.dir = 0;
2669 cfg.timeout = 0;
2670 cfg.physAddr = -1;
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002671 if ((mpt_config(ioc, &cfg) == 0) && (cfg.cfghdr.hdr->PageLength > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /* Issue the second config page request */
2673 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002674 data_sz = (int) cfg.cfghdr.hdr->PageLength * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 pg3_alloc = (SCSIDevicePage3_t *) pci_alloc_consistent(
2676 ioc->pcidev, data_sz, &page_dma);
2677 if (pg3_alloc) {
2678 cfg.physAddr = page_dma;
2679 cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2680 if ((rc = mpt_config(ioc, &cfg)) == 0) {
2681 karg.message_rejects = (u32) le16_to_cpu(pg3_alloc->MsgRejectCount);
2682 karg.phase_errors = (u32) le16_to_cpu(pg3_alloc->PhaseErrorCount);
2683 karg.parity_errors = (u32) le16_to_cpu(pg3_alloc->ParityErrorCount);
2684 }
2685 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg3_alloc, page_dma);
2686 }
2687 }
Eric Mooree7eae9f2007-09-29 10:15:59 -06002688 hd = shost_priv(ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (hd != NULL)
2690 karg.select_timeouts = hd->sel_timeout[karg.hdr.id];
2691
2692 /* Copy the data from kernel memory to user memory
2693 */
2694 if (copy_to_user((char __user *)arg, &karg, sizeof(hp_target_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002695 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_hp_target_info - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002697 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return -EFAULT;
2699 }
2700
2701 return 0;
2702}
2703
2704/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2705
Arjan van de Venfa027c22007-02-12 00:55:33 -08002706static const struct file_operations mptctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 .owner = THIS_MODULE,
2708 .llseek = no_llseek,
Moore, Ericea5a7a822006-02-02 17:20:01 -07002709 .release = mptctl_release,
2710 .fasync = mptctl_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 .unlocked_ioctl = mptctl_ioctl,
2712#ifdef CONFIG_COMPAT
2713 .compat_ioctl = compat_mpctl_ioctl,
2714#endif
2715};
2716
2717static struct miscdevice mptctl_miscdev = {
2718 MPT_MINOR,
2719 MYNAM,
2720 &mptctl_fops
2721};
2722
2723/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2724
2725#ifdef CONFIG_COMPAT
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727static int
2728compat_mptfwxfer_ioctl(struct file *filp, unsigned int cmd,
2729 unsigned long arg)
2730{
2731 struct mpt_fw_xfer32 kfw32;
2732 struct mpt_fw_xfer kfw;
2733 MPT_ADAPTER *iocp = NULL;
2734 int iocnum, iocnumX;
2735 int nonblock = (filp->f_flags & O_NONBLOCK);
2736 int ret;
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 if (copy_from_user(&kfw32, (char __user *)arg, sizeof(kfw32)))
2740 return -EFAULT;
2741
2742 /* Verify intended MPT adapter */
2743 iocnumX = kfw32.iocnum & 0xFF;
2744 if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2745 (iocp == NULL)) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302746 printk(KERN_DEBUG MYNAM "::compat_mptfwxfer_ioctl @%d - ioc%d not found!\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002747 __LINE__, iocnumX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return -ENODEV;
2749 }
2750
2751 if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2752 return ret;
2753
Prakash, Sathya09120a82007-07-24 15:49:05 +05302754 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "compat_mptfwxfer_ioctl() called\n",
2755 iocp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 kfw.iocnum = iocnum;
2757 kfw.fwlen = kfw32.fwlen;
2758 kfw.bufp = compat_ptr(kfw32.bufp);
2759
2760 ret = mptctl_do_fw_download(kfw.iocnum, kfw.bufp, kfw.fwlen);
2761
Christoph Hellwigeeb846c2006-01-13 18:27:11 +01002762 mutex_unlock(&iocp->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 return ret;
2765}
2766
2767static int
2768compat_mpt_command(struct file *filp, unsigned int cmd,
2769 unsigned long arg)
2770{
2771 struct mpt_ioctl_command32 karg32;
2772 struct mpt_ioctl_command32 __user *uarg = (struct mpt_ioctl_command32 __user *) arg;
2773 struct mpt_ioctl_command karg;
2774 MPT_ADAPTER *iocp = NULL;
2775 int iocnum, iocnumX;
2776 int nonblock = (filp->f_flags & O_NONBLOCK);
2777 int ret;
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32)))
2780 return -EFAULT;
2781
2782 /* Verify intended MPT adapter */
2783 iocnumX = karg32.hdr.iocnum & 0xFF;
2784 if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2785 (iocp == NULL)) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302786 printk(KERN_DEBUG MYNAM "::compat_mpt_command @%d - ioc%d not found!\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002787 __LINE__, iocnumX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return -ENODEV;
2789 }
2790
2791 if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2792 return ret;
2793
Prakash, Sathya09120a82007-07-24 15:49:05 +05302794 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "compat_mpt_command() called\n",
2795 iocp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 /* Copy data to karg */
2797 karg.hdr.iocnum = karg32.hdr.iocnum;
2798 karg.hdr.port = karg32.hdr.port;
2799 karg.timeout = karg32.timeout;
2800 karg.maxReplyBytes = karg32.maxReplyBytes;
2801
2802 karg.dataInSize = karg32.dataInSize;
2803 karg.dataOutSize = karg32.dataOutSize;
2804 karg.maxSenseBytes = karg32.maxSenseBytes;
2805 karg.dataSgeOffset = karg32.dataSgeOffset;
2806
2807 karg.replyFrameBufPtr = (char __user *)(unsigned long)karg32.replyFrameBufPtr;
2808 karg.dataInBufPtr = (char __user *)(unsigned long)karg32.dataInBufPtr;
2809 karg.dataOutBufPtr = (char __user *)(unsigned long)karg32.dataOutBufPtr;
2810 karg.senseDataPtr = (char __user *)(unsigned long)karg32.senseDataPtr;
2811
2812 /* Pass new structure to do_mpt_command
2813 */
2814 ret = mptctl_do_mpt_command (karg, &uarg->MF);
2815
Christoph Hellwigeeb846c2006-01-13 18:27:11 +01002816 mutex_unlock(&iocp->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 return ret;
2819}
2820
2821static long compat_mpctl_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2822{
2823 long ret;
2824 lock_kernel();
2825 switch (cmd) {
2826 case MPTIOCINFO:
2827 case MPTIOCINFO1:
2828 case MPTIOCINFO2:
2829 case MPTTARGETINFO:
2830 case MPTEVENTQUERY:
2831 case MPTEVENTENABLE:
2832 case MPTEVENTREPORT:
2833 case MPTHARDRESET:
2834 case HP_GETHOSTINFO:
2835 case HP_GETTARGETINFO:
2836 case MPTTEST:
2837 ret = __mptctl_ioctl(f, cmd, arg);
2838 break;
2839 case MPTCOMMAND32:
2840 ret = compat_mpt_command(f, cmd, arg);
2841 break;
2842 case MPTFWDOWNLOAD32:
2843 ret = compat_mptfwxfer_ioctl(f, cmd, arg);
2844 break;
2845 default:
2846 ret = -ENOIOCTLCMD;
2847 break;
2848 }
2849 unlock_kernel();
2850 return ret;
2851}
2852
2853#endif
2854
2855
2856/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2857/*
2858 * mptctl_probe - Installs ioctl devices per bus.
2859 * @pdev: Pointer to pci_dev structure
2860 *
2861 * Returns 0 for success, non-zero for failure.
2862 *
2863 */
2864
2865static int
2866mptctl_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2867{
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07002868 MPT_IOCTL *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2870
2871 /*
2872 * Allocate and inite a MPT_IOCTL structure
2873 */
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07002874 mem = kzalloc(sizeof(MPT_IOCTL), GFP_KERNEL);
2875 if (!mem) {
2876 mptctl_remove(pdev);
2877 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07002880 ioc->ioctl = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 ioc->ioctl->ioc = ioc;
Christoph Hellwigeeb846c2006-01-13 18:27:11 +01002882 mutex_init(&ioc->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
2886/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2887/*
2888 * mptctl_remove - Removed ioctl devices
2889 * @pdev: Pointer to pci_dev structure
2890 *
2891 *
2892 */
2893static void
2894mptctl_remove(struct pci_dev *pdev)
2895{
2896 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2897
2898 kfree ( ioc->ioctl );
2899}
2900
2901static struct mpt_pci_driver mptctl_driver = {
2902 .probe = mptctl_probe,
2903 .remove = mptctl_remove,
2904};
2905
2906/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2907static int __init mptctl_init(void)
2908{
2909 int err;
2910 int where = 1;
2911
2912 show_mptmod_ver(my_NAME, my_VERSION);
2913
Prakash, Sathya09120a82007-07-24 15:49:05 +05302914 mpt_device_driver_register(&mptctl_driver, MPTCTL_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 /* Register this device */
2917 err = misc_register(&mptctl_miscdev);
2918 if (err < 0) {
2919 printk(KERN_ERR MYNAM ": Can't register misc device [minor=%d].\n", MPT_MINOR);
2920 goto out_fail;
2921 }
2922 printk(KERN_INFO MYNAM ": Registered with Fusion MPT base driver\n");
2923 printk(KERN_INFO MYNAM ": /dev/%s @ (major,minor=%d,%d)\n",
2924 mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2925
2926 /*
2927 * Install our handler
2928 */
2929 ++where;
Prakash, Sathyaf606f572007-08-14 16:12:53 +05302930 mptctl_id = mpt_register(mptctl_reply, MPTCTL_DRIVER);
2931 if (!mptctl_id || mptctl_id >= MPT_MAX_PROTOCOL_DRIVERS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 printk(KERN_ERR MYNAM ": ERROR: Failed to register with Fusion MPT base driver\n");
2933 misc_deregister(&mptctl_miscdev);
2934 err = -EBUSY;
2935 goto out_fail;
2936 }
2937
Prakash, Sathya09120a82007-07-24 15:49:05 +05302938 mpt_reset_register(mptctl_id, mptctl_ioc_reset);
2939 mpt_event_register(mptctl_id, mptctl_event_process);
Moore, Ericea5a7a822006-02-02 17:20:01 -07002940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return 0;
2942
2943out_fail:
2944
2945 mpt_device_driver_deregister(MPTCTL_DRIVER);
2946
2947 return err;
2948}
2949
2950/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2951static void mptctl_exit(void)
2952{
2953 misc_deregister(&mptctl_miscdev);
2954 printk(KERN_INFO MYNAM ": Deregistered /dev/%s @ (major,minor=%d,%d)\n",
2955 mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2956
2957 /* De-register reset handler from base module */
2958 mpt_reset_deregister(mptctl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 /* De-register callback handler from base module */
2961 mpt_deregister(mptctl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 mpt_device_driver_deregister(MPTCTL_DRIVER);
2964
2965}
2966
2967/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2968
2969module_init(mptctl_init);
2970module_exit(mptctl_exit);