mptctl.c 78 KB

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