mptctl.c 82 KB

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