mptctl.c 78 KB

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