mptctl.c 82 KB

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