mptctl.c 82 KB

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