mptctl.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  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@lsi.com)
  9. *
  10. */
  11. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  12. /*
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; version 2 of the License.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. NO WARRANTY
  21. THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  22. CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  23. LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  24. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  25. solely responsible for determining the appropriateness of using and
  26. distributing the Program and assumes all risks associated with its
  27. exercise of rights under this Agreement, including but not limited to
  28. the risks and costs of program errors, damage to or loss of data,
  29. programs or equipment, and unavailability or interruption of operations.
  30. DISCLAIMER OF LIABILITY
  31. NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37. HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38. You should have received a copy of the GNU General Public License
  39. along with this program; if not, write to the Free Software
  40. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41. */
  42. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/delay.h> /* for mdelay */
  51. #include <linux/miscdevice.h>
  52. #include <linux/smp_lock.h>
  53. #include <linux/compat.h>
  54. #include <asm/io.h>
  55. #include <asm/uaccess.h>
  56. #include <scsi/scsi.h>
  57. #include <scsi/scsi_cmnd.h>
  58. #include <scsi/scsi_device.h>
  59. #include <scsi/scsi_host.h>
  60. #include <scsi/scsi_tcq.h>
  61. #define COPYRIGHT "Copyright (c) 1999-2007 LSI Logic Corporation"
  62. #define MODULEAUTHOR "LSI Logic Corporation"
  63. #include "mptbase.h"
  64. #include "mptctl.h"
  65. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  66. #define my_NAME "Fusion MPT misc device (ioctl) driver"
  67. #define my_VERSION MPT_LINUX_VERSION_COMMON
  68. #define MYNAM "mptctl"
  69. MODULE_AUTHOR(MODULEAUTHOR);
  70. MODULE_DESCRIPTION(my_NAME);
  71. MODULE_LICENSE("GPL");
  72. MODULE_VERSION(my_VERSION);
  73. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  74. static 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, CAN_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->id;
  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. int iocnum;
  1002. unsigned int port;
  1003. int cim_rev;
  1004. u8 revision;
  1005. struct scsi_device *sdev;
  1006. VirtDevice *vdev;
  1007. dctlprintk((": mptctl_getiocinfo called.\n"));
  1008. /* Add of PCI INFO results in unaligned access for
  1009. * IA64 and Sparc. Reset long to int. Return no PCI
  1010. * data for obsolete format.
  1011. */
  1012. if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev0))
  1013. cim_rev = 0;
  1014. else if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev1))
  1015. cim_rev = 1;
  1016. else if (data_size == sizeof(struct mpt_ioctl_iocinfo))
  1017. cim_rev = 2;
  1018. else if (data_size == (sizeof(struct mpt_ioctl_iocinfo_rev0)+12))
  1019. cim_rev = 0; /* obsolete */
  1020. else
  1021. return -EFAULT;
  1022. karg = kmalloc(data_size, GFP_KERNEL);
  1023. if (karg == NULL) {
  1024. printk(KERN_ERR "%s::mpt_ioctl_iocinfo() @%d - no memory available!\n",
  1025. __FILE__, __LINE__);
  1026. return -ENOMEM;
  1027. }
  1028. if (copy_from_user(karg, uarg, data_size)) {
  1029. printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
  1030. "Unable to read in mpt_ioctl_iocinfo struct @ %p\n",
  1031. __FILE__, __LINE__, uarg);
  1032. kfree(karg);
  1033. return -EFAULT;
  1034. }
  1035. if (((iocnum = mpt_verify_adapter(karg->hdr.iocnum, &ioc)) < 0) ||
  1036. (ioc == NULL)) {
  1037. dctlprintk((KERN_ERR "%s::mptctl_getiocinfo() @%d - ioc%d not found!\n",
  1038. __FILE__, __LINE__, iocnum));
  1039. kfree(karg);
  1040. return -ENODEV;
  1041. }
  1042. /* Verify the data transfer size is correct. */
  1043. if (karg->hdr.maxDataSize != data_size) {
  1044. printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
  1045. "Structure size mismatch. Command not completed.\n",
  1046. __FILE__, __LINE__);
  1047. kfree(karg);
  1048. return -EFAULT;
  1049. }
  1050. /* Fill in the data and return the structure to the calling
  1051. * program
  1052. */
  1053. if (ioc->bus_type == SAS)
  1054. karg->adapterType = MPT_IOCTL_INTERFACE_SAS;
  1055. else if (ioc->bus_type == FC)
  1056. karg->adapterType = MPT_IOCTL_INTERFACE_FC;
  1057. else
  1058. karg->adapterType = MPT_IOCTL_INTERFACE_SCSI;
  1059. if (karg->hdr.port > 1)
  1060. return -EINVAL;
  1061. port = karg->hdr.port;
  1062. karg->port = port;
  1063. pdev = (struct pci_dev *) ioc->pcidev;
  1064. karg->pciId = pdev->device;
  1065. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
  1066. karg->hwRev = revision;
  1067. karg->subSystemDevice = pdev->subsystem_device;
  1068. karg->subSystemVendor = pdev->subsystem_vendor;
  1069. if (cim_rev == 1) {
  1070. /* Get the PCI bus, device, and function numbers for the IOC
  1071. */
  1072. karg->pciInfo.u.bits.busNumber = pdev->bus->number;
  1073. karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
  1074. karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
  1075. } else if (cim_rev == 2) {
  1076. /* Get the PCI bus, device, function and segment ID numbers
  1077. for the IOC */
  1078. karg->pciInfo.u.bits.busNumber = pdev->bus->number;
  1079. karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
  1080. karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
  1081. karg->pciInfo.segmentID = pci_domain_nr(pdev->bus);
  1082. }
  1083. /* Get number of devices
  1084. */
  1085. karg->numDevices = 0;
  1086. if (ioc->sh) {
  1087. shost_for_each_device(sdev, ioc->sh) {
  1088. vdev = sdev->hostdata;
  1089. if (vdev->vtarget->tflags &
  1090. MPT_TARGET_FLAGS_RAID_COMPONENT)
  1091. continue;
  1092. karg->numDevices++;
  1093. }
  1094. }
  1095. /* Set the BIOS and FW Version
  1096. */
  1097. karg->FWVersion = ioc->facts.FWVersion.Word;
  1098. karg->BIOSVersion = ioc->biosVersion;
  1099. /* Set the Version Strings.
  1100. */
  1101. strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
  1102. karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
  1103. karg->busChangeEvent = 0;
  1104. karg->hostId = ioc->pfacts[port].PortSCSIID;
  1105. karg->rsvd[0] = karg->rsvd[1] = 0;
  1106. /* Copy the data from kernel memory to user memory
  1107. */
  1108. if (copy_to_user((char __user *)arg, karg, data_size)) {
  1109. printk(KERN_ERR "%s@%d::mptctl_getiocinfo - "
  1110. "Unable to write out mpt_ioctl_iocinfo struct @ %p\n",
  1111. __FILE__, __LINE__, uarg);
  1112. kfree(karg);
  1113. return -EFAULT;
  1114. }
  1115. kfree(karg);
  1116. return 0;
  1117. }
  1118. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1119. /*
  1120. * mptctl_gettargetinfo - Query the host adapter for target information.
  1121. * @arg: User space argument
  1122. *
  1123. * Outputs: None.
  1124. * Return: 0 if successful
  1125. * -EFAULT if data unavailable
  1126. * -ENODEV if no such device/adapter
  1127. */
  1128. static int
  1129. mptctl_gettargetinfo (unsigned long arg)
  1130. {
  1131. struct mpt_ioctl_targetinfo __user *uarg = (void __user *) arg;
  1132. struct mpt_ioctl_targetinfo karg;
  1133. MPT_ADAPTER *ioc;
  1134. VirtDevice *vdev;
  1135. char *pmem;
  1136. int *pdata;
  1137. int iocnum;
  1138. int numDevices = 0;
  1139. int lun;
  1140. int maxWordsLeft;
  1141. int numBytes;
  1142. u8 port;
  1143. struct scsi_device *sdev;
  1144. dctlprintk(("mptctl_gettargetinfo called.\n"));
  1145. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_targetinfo))) {
  1146. printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
  1147. "Unable to read in mpt_ioctl_targetinfo struct @ %p\n",
  1148. __FILE__, __LINE__, uarg);
  1149. return -EFAULT;
  1150. }
  1151. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1152. (ioc == NULL)) {
  1153. dctlprintk((KERN_ERR "%s::mptctl_gettargetinfo() @%d - ioc%d not found!\n",
  1154. __FILE__, __LINE__, iocnum));
  1155. return -ENODEV;
  1156. }
  1157. /* Get the port number and set the maximum number of bytes
  1158. * in the returned structure.
  1159. * Ignore the port setting.
  1160. */
  1161. numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
  1162. maxWordsLeft = numBytes/sizeof(int);
  1163. port = karg.hdr.port;
  1164. if (maxWordsLeft <= 0) {
  1165. printk(KERN_ERR "%s::mptctl_gettargetinfo() @%d - no memory available!\n",
  1166. __FILE__, __LINE__);
  1167. return -ENOMEM;
  1168. }
  1169. /* Fill in the data and return the structure to the calling
  1170. * program
  1171. */
  1172. /* struct mpt_ioctl_targetinfo does not contain sufficient space
  1173. * for the target structures so when the IOCTL is called, there is
  1174. * not sufficient stack space for the structure. Allocate memory,
  1175. * populate the memory, copy back to the user, then free memory.
  1176. * targetInfo format:
  1177. * bits 31-24: reserved
  1178. * 23-16: LUN
  1179. * 15- 8: Bus Number
  1180. * 7- 0: Target ID
  1181. */
  1182. pmem = kmalloc(numBytes, GFP_KERNEL);
  1183. if (pmem == NULL) {
  1184. printk(KERN_ERR "%s::mptctl_gettargetinfo() @%d - no memory available!\n",
  1185. __FILE__, __LINE__);
  1186. return -ENOMEM;
  1187. }
  1188. memset(pmem, 0, numBytes);
  1189. pdata = (int *) pmem;
  1190. /* Get number of devices
  1191. */
  1192. if (ioc->sh){
  1193. shost_for_each_device(sdev, ioc->sh) {
  1194. if (!maxWordsLeft)
  1195. continue;
  1196. vdev = sdev->hostdata;
  1197. if (vdev->vtarget->tflags &
  1198. MPT_TARGET_FLAGS_RAID_COMPONENT)
  1199. continue;
  1200. lun = (vdev->vtarget->raidVolume) ? 0x80 : vdev->lun;
  1201. *pdata = (((u8)lun << 16) + (vdev->vtarget->channel << 8) +
  1202. (vdev->vtarget->id ));
  1203. pdata++;
  1204. numDevices++;
  1205. --maxWordsLeft;
  1206. }
  1207. }
  1208. karg.numDevices = numDevices;
  1209. /* Copy part of the data from kernel memory to user memory
  1210. */
  1211. if (copy_to_user((char __user *)arg, &karg,
  1212. sizeof(struct mpt_ioctl_targetinfo))) {
  1213. printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
  1214. "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
  1215. __FILE__, __LINE__, uarg);
  1216. kfree(pmem);
  1217. return -EFAULT;
  1218. }
  1219. /* Copy the remaining data from kernel memory to user memory
  1220. */
  1221. if (copy_to_user(uarg->targetInfo, pmem, numBytes)) {
  1222. printk(KERN_ERR "%s@%d::mptctl_gettargetinfo - "
  1223. "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
  1224. __FILE__, __LINE__, pdata);
  1225. kfree(pmem);
  1226. return -EFAULT;
  1227. }
  1228. kfree(pmem);
  1229. return 0;
  1230. }
  1231. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1232. /* MPT IOCTL Test function.
  1233. *
  1234. * Outputs: None.
  1235. * Return: 0 if successful
  1236. * -EFAULT if data unavailable
  1237. * -ENODEV if no such device/adapter
  1238. */
  1239. static int
  1240. mptctl_readtest (unsigned long arg)
  1241. {
  1242. struct mpt_ioctl_test __user *uarg = (void __user *) arg;
  1243. struct mpt_ioctl_test karg;
  1244. MPT_ADAPTER *ioc;
  1245. int iocnum;
  1246. dctlprintk(("mptctl_readtest called.\n"));
  1247. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_test))) {
  1248. printk(KERN_ERR "%s@%d::mptctl_readtest - "
  1249. "Unable to read in mpt_ioctl_test struct @ %p\n",
  1250. __FILE__, __LINE__, uarg);
  1251. return -EFAULT;
  1252. }
  1253. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1254. (ioc == NULL)) {
  1255. dctlprintk((KERN_ERR "%s::mptctl_readtest() @%d - ioc%d not found!\n",
  1256. __FILE__, __LINE__, iocnum));
  1257. return -ENODEV;
  1258. }
  1259. /* Fill in the data and return the structure to the calling
  1260. * program
  1261. */
  1262. #ifdef MFCNT
  1263. karg.chip_type = ioc->mfcnt;
  1264. #else
  1265. karg.chip_type = ioc->pcidev->device;
  1266. #endif
  1267. strncpy (karg.name, ioc->name, MPT_MAX_NAME);
  1268. karg.name[MPT_MAX_NAME-1]='\0';
  1269. strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
  1270. karg.product[MPT_PRODUCT_LENGTH-1]='\0';
  1271. /* Copy the data from kernel memory to user memory
  1272. */
  1273. if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_test))) {
  1274. printk(KERN_ERR "%s@%d::mptctl_readtest - "
  1275. "Unable to write out mpt_ioctl_test struct @ %p\n",
  1276. __FILE__, __LINE__, uarg);
  1277. return -EFAULT;
  1278. }
  1279. return 0;
  1280. }
  1281. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1282. /*
  1283. * mptctl_eventquery - Query the host adapter for the event types
  1284. * that are being logged.
  1285. * @arg: User space argument
  1286. *
  1287. * Outputs: None.
  1288. * Return: 0 if successful
  1289. * -EFAULT if data unavailable
  1290. * -ENODEV if no such device/adapter
  1291. */
  1292. static int
  1293. mptctl_eventquery (unsigned long arg)
  1294. {
  1295. struct mpt_ioctl_eventquery __user *uarg = (void __user *) arg;
  1296. struct mpt_ioctl_eventquery karg;
  1297. MPT_ADAPTER *ioc;
  1298. int iocnum;
  1299. dctlprintk(("mptctl_eventquery called.\n"));
  1300. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventquery))) {
  1301. printk(KERN_ERR "%s@%d::mptctl_eventquery - "
  1302. "Unable to read in mpt_ioctl_eventquery struct @ %p\n",
  1303. __FILE__, __LINE__, uarg);
  1304. return -EFAULT;
  1305. }
  1306. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1307. (ioc == NULL)) {
  1308. dctlprintk((KERN_ERR "%s::mptctl_eventquery() @%d - ioc%d not found!\n",
  1309. __FILE__, __LINE__, iocnum));
  1310. return -ENODEV;
  1311. }
  1312. karg.eventEntries = MPTCTL_EVENT_LOG_SIZE;
  1313. karg.eventTypes = ioc->eventTypes;
  1314. /* Copy the data from kernel memory to user memory
  1315. */
  1316. if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_eventquery))) {
  1317. printk(KERN_ERR "%s@%d::mptctl_eventquery - "
  1318. "Unable to write out mpt_ioctl_eventquery struct @ %p\n",
  1319. __FILE__, __LINE__, uarg);
  1320. return -EFAULT;
  1321. }
  1322. return 0;
  1323. }
  1324. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1325. static int
  1326. mptctl_eventenable (unsigned long arg)
  1327. {
  1328. struct mpt_ioctl_eventenable __user *uarg = (void __user *) arg;
  1329. struct mpt_ioctl_eventenable karg;
  1330. MPT_ADAPTER *ioc;
  1331. int iocnum;
  1332. dctlprintk(("mptctl_eventenable called.\n"));
  1333. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventenable))) {
  1334. printk(KERN_ERR "%s@%d::mptctl_eventenable - "
  1335. "Unable to read in mpt_ioctl_eventenable struct @ %p\n",
  1336. __FILE__, __LINE__, uarg);
  1337. return -EFAULT;
  1338. }
  1339. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1340. (ioc == NULL)) {
  1341. dctlprintk((KERN_ERR "%s::mptctl_eventenable() @%d - ioc%d not found!\n",
  1342. __FILE__, __LINE__, iocnum));
  1343. return -ENODEV;
  1344. }
  1345. if (ioc->events == NULL) {
  1346. /* Have not yet allocated memory - do so now.
  1347. */
  1348. int sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
  1349. ioc->events = kmalloc(sz, GFP_KERNEL);
  1350. if (ioc->events == NULL) {
  1351. printk(KERN_ERR MYNAM ": ERROR - Insufficient memory to add adapter!\n");
  1352. return -ENOMEM;
  1353. }
  1354. memset(ioc->events, 0, sz);
  1355. ioc->alloc_total += sz;
  1356. ioc->eventContext = 0;
  1357. }
  1358. /* Update the IOC event logging flag.
  1359. */
  1360. ioc->eventTypes = karg.eventTypes;
  1361. return 0;
  1362. }
  1363. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1364. static int
  1365. mptctl_eventreport (unsigned long arg)
  1366. {
  1367. struct mpt_ioctl_eventreport __user *uarg = (void __user *) arg;
  1368. struct mpt_ioctl_eventreport karg;
  1369. MPT_ADAPTER *ioc;
  1370. int iocnum;
  1371. int numBytes, maxEvents, max;
  1372. dctlprintk(("mptctl_eventreport called.\n"));
  1373. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventreport))) {
  1374. printk(KERN_ERR "%s@%d::mptctl_eventreport - "
  1375. "Unable to read in mpt_ioctl_eventreport struct @ %p\n",
  1376. __FILE__, __LINE__, uarg);
  1377. return -EFAULT;
  1378. }
  1379. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1380. (ioc == NULL)) {
  1381. dctlprintk((KERN_ERR "%s::mptctl_eventreport() @%d - ioc%d not found!\n",
  1382. __FILE__, __LINE__, iocnum));
  1383. return -ENODEV;
  1384. }
  1385. numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
  1386. maxEvents = numBytes/sizeof(MPT_IOCTL_EVENTS);
  1387. max = MPTCTL_EVENT_LOG_SIZE < maxEvents ? MPTCTL_EVENT_LOG_SIZE : maxEvents;
  1388. /* If fewer than 1 event is requested, there must have
  1389. * been some type of error.
  1390. */
  1391. if ((max < 1) || !ioc->events)
  1392. return -ENODATA;
  1393. /* reset this flag so SIGIO can restart */
  1394. ioc->aen_event_read_flag=0;
  1395. /* Copy the data from kernel memory to user memory
  1396. */
  1397. numBytes = max * sizeof(MPT_IOCTL_EVENTS);
  1398. if (copy_to_user(uarg->eventData, ioc->events, numBytes)) {
  1399. printk(KERN_ERR "%s@%d::mptctl_eventreport - "
  1400. "Unable to write out mpt_ioctl_eventreport struct @ %p\n",
  1401. __FILE__, __LINE__, ioc->events);
  1402. return -EFAULT;
  1403. }
  1404. return 0;
  1405. }
  1406. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1407. static int
  1408. mptctl_replace_fw (unsigned long arg)
  1409. {
  1410. struct mpt_ioctl_replace_fw __user *uarg = (void __user *) arg;
  1411. struct mpt_ioctl_replace_fw karg;
  1412. MPT_ADAPTER *ioc;
  1413. int iocnum;
  1414. int newFwSize;
  1415. dctlprintk(("mptctl_replace_fw called.\n"));
  1416. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_replace_fw))) {
  1417. printk(KERN_ERR "%s@%d::mptctl_replace_fw - "
  1418. "Unable to read in mpt_ioctl_replace_fw struct @ %p\n",
  1419. __FILE__, __LINE__, uarg);
  1420. return -EFAULT;
  1421. }
  1422. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1423. (ioc == NULL)) {
  1424. dctlprintk((KERN_ERR "%s::mptctl_replace_fw() @%d - ioc%d not found!\n",
  1425. __FILE__, __LINE__, iocnum));
  1426. return -ENODEV;
  1427. }
  1428. /* If caching FW, Free the old FW image
  1429. */
  1430. if (ioc->cached_fw == NULL)
  1431. return 0;
  1432. mpt_free_fw_memory(ioc);
  1433. /* Allocate memory for the new FW image
  1434. */
  1435. newFwSize = karg.newImageSize;
  1436. if (newFwSize & 0x01)
  1437. newFwSize += 1;
  1438. if (newFwSize & 0x02)
  1439. newFwSize += 2;
  1440. mpt_alloc_fw_memory(ioc, newFwSize);
  1441. if (ioc->cached_fw == NULL)
  1442. return -ENOMEM;
  1443. /* Copy the data from user memory to kernel space
  1444. */
  1445. if (copy_from_user(ioc->cached_fw, uarg->newImage, newFwSize)) {
  1446. printk(KERN_ERR "%s@%d::mptctl_replace_fw - "
  1447. "Unable to read in mpt_ioctl_replace_fw image "
  1448. "@ %p\n", __FILE__, __LINE__, uarg);
  1449. mpt_free_fw_memory(ioc);
  1450. return -EFAULT;
  1451. }
  1452. /* Update IOCFactsReply
  1453. */
  1454. ioc->facts.FWImageSize = newFwSize;
  1455. return 0;
  1456. }
  1457. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1458. /* MPT IOCTL MPTCOMMAND function.
  1459. * Cast the arg into the mpt_ioctl_mpt_command structure.
  1460. *
  1461. * Outputs: None.
  1462. * Return: 0 if successful
  1463. * -EBUSY if previous command timout and IOC reset is not complete.
  1464. * -EFAULT if data unavailable
  1465. * -ENODEV if no such device/adapter
  1466. * -ETIME if timer expires
  1467. * -ENOMEM if memory allocation error
  1468. */
  1469. static int
  1470. mptctl_mpt_command (unsigned long arg)
  1471. {
  1472. struct mpt_ioctl_command __user *uarg = (void __user *) arg;
  1473. struct mpt_ioctl_command karg;
  1474. MPT_ADAPTER *ioc;
  1475. int iocnum;
  1476. int rc;
  1477. dctlprintk(("mptctl_command called.\n"));
  1478. if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_command))) {
  1479. printk(KERN_ERR "%s@%d::mptctl_mpt_command - "
  1480. "Unable to read in mpt_ioctl_command struct @ %p\n",
  1481. __FILE__, __LINE__, uarg);
  1482. return -EFAULT;
  1483. }
  1484. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1485. (ioc == NULL)) {
  1486. dctlprintk((KERN_ERR "%s::mptctl_mpt_command() @%d - ioc%d not found!\n",
  1487. __FILE__, __LINE__, iocnum));
  1488. return -ENODEV;
  1489. }
  1490. rc = mptctl_do_mpt_command (karg, &uarg->MF);
  1491. return rc;
  1492. }
  1493. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1494. /* Worker routine for the IOCTL MPTCOMMAND and MPTCOMMAND32 (sparc) commands.
  1495. *
  1496. * Outputs: None.
  1497. * Return: 0 if successful
  1498. * -EBUSY if previous command timout and IOC reset is not complete.
  1499. * -EFAULT if data unavailable
  1500. * -ENODEV if no such device/adapter
  1501. * -ETIME if timer expires
  1502. * -ENOMEM if memory allocation error
  1503. * -EPERM if SCSI I/O and target is untagged
  1504. */
  1505. static int
  1506. mptctl_do_mpt_command (struct mpt_ioctl_command karg, void __user *mfPtr)
  1507. {
  1508. MPT_ADAPTER *ioc;
  1509. MPT_FRAME_HDR *mf = NULL;
  1510. MPIHeader_t *hdr;
  1511. char *psge;
  1512. struct buflist bufIn; /* data In buffer */
  1513. struct buflist bufOut; /* data Out buffer */
  1514. dma_addr_t dma_addr_in;
  1515. dma_addr_t dma_addr_out;
  1516. int sgSize = 0; /* Num SG elements */
  1517. int iocnum, flagsLength;
  1518. int sz, rc = 0;
  1519. int msgContext;
  1520. u16 req_idx;
  1521. ulong timeout;
  1522. struct scsi_device *sdev;
  1523. dctlprintk(("mptctl_do_mpt_command called.\n"));
  1524. bufIn.kptr = bufOut.kptr = NULL;
  1525. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  1526. (ioc == NULL)) {
  1527. dctlprintk((KERN_ERR "%s::mptctl_do_mpt_command() @%d - ioc%d not found!\n",
  1528. __FILE__, __LINE__, iocnum));
  1529. return -ENODEV;
  1530. }
  1531. if (!ioc->ioctl) {
  1532. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1533. "No memory available during driver init.\n",
  1534. __FILE__, __LINE__);
  1535. return -ENOMEM;
  1536. } else if (ioc->ioctl->status & MPT_IOCTL_STATUS_DID_IOCRESET) {
  1537. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1538. "Busy with IOC Reset \n", __FILE__, __LINE__);
  1539. return -EBUSY;
  1540. }
  1541. /* Verify that the final request frame will not be too large.
  1542. */
  1543. sz = karg.dataSgeOffset * 4;
  1544. if (karg.dataInSize > 0)
  1545. sz += sizeof(dma_addr_t) + sizeof(u32);
  1546. if (karg.dataOutSize > 0)
  1547. sz += sizeof(dma_addr_t) + sizeof(u32);
  1548. if (sz > ioc->req_sz) {
  1549. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1550. "Request frame too large (%d) maximum (%d)\n",
  1551. __FILE__, __LINE__, sz, ioc->req_sz);
  1552. return -EFAULT;
  1553. }
  1554. /* Get a free request frame and save the message context.
  1555. */
  1556. if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL)
  1557. return -EAGAIN;
  1558. hdr = (MPIHeader_t *) mf;
  1559. msgContext = le32_to_cpu(hdr->MsgContext);
  1560. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  1561. /* Copy the request frame
  1562. * Reset the saved message context.
  1563. * Request frame in user space
  1564. */
  1565. if (copy_from_user(mf, mfPtr, karg.dataSgeOffset * 4)) {
  1566. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1567. "Unable to read MF from mpt_ioctl_command struct @ %p\n",
  1568. __FILE__, __LINE__, mfPtr);
  1569. rc = -EFAULT;
  1570. goto done_free_mem;
  1571. }
  1572. hdr->MsgContext = cpu_to_le32(msgContext);
  1573. /* Verify that this request is allowed.
  1574. */
  1575. switch (hdr->Function) {
  1576. case MPI_FUNCTION_IOC_FACTS:
  1577. case MPI_FUNCTION_PORT_FACTS:
  1578. karg.dataOutSize = karg.dataInSize = 0;
  1579. break;
  1580. case MPI_FUNCTION_CONFIG:
  1581. case MPI_FUNCTION_FC_COMMON_TRANSPORT_SEND:
  1582. case MPI_FUNCTION_FC_EX_LINK_SRVC_SEND:
  1583. case MPI_FUNCTION_FW_UPLOAD:
  1584. case MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  1585. case MPI_FUNCTION_FW_DOWNLOAD:
  1586. case MPI_FUNCTION_FC_PRIMITIVE_SEND:
  1587. case MPI_FUNCTION_TOOLBOX:
  1588. case MPI_FUNCTION_SAS_IO_UNIT_CONTROL:
  1589. break;
  1590. case MPI_FUNCTION_SCSI_IO_REQUEST:
  1591. if (ioc->sh) {
  1592. SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
  1593. int qtag = MPI_SCSIIO_CONTROL_UNTAGGED;
  1594. int scsidir = 0;
  1595. int dataSize;
  1596. u32 id;
  1597. id = (ioc->devices_per_bus == 0) ? 256 : ioc->devices_per_bus;
  1598. if (pScsiReq->TargetID > id) {
  1599. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1600. "Target ID out of bounds. \n",
  1601. __FILE__, __LINE__);
  1602. rc = -ENODEV;
  1603. goto done_free_mem;
  1604. }
  1605. if (pScsiReq->Bus >= ioc->number_of_buses) {
  1606. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1607. "Target Bus out of bounds. \n",
  1608. __FILE__, __LINE__);
  1609. rc = -ENODEV;
  1610. goto done_free_mem;
  1611. }
  1612. pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
  1613. pScsiReq->MsgFlags |= mpt_msg_flags();
  1614. /* verify that app has not requested
  1615. * more sense data than driver
  1616. * can provide, if so, reset this parameter
  1617. * set the sense buffer pointer low address
  1618. * update the control field to specify Q type
  1619. */
  1620. if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
  1621. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  1622. else
  1623. pScsiReq->SenseBufferLength = karg.maxSenseBytes;
  1624. pScsiReq->SenseBufferLowAddr =
  1625. cpu_to_le32(ioc->sense_buf_low_dma
  1626. + (req_idx * MPT_SENSE_BUFFER_ALLOC));
  1627. shost_for_each_device(sdev, ioc->sh) {
  1628. struct scsi_target *starget = scsi_target(sdev);
  1629. VirtTarget *vtarget = starget->hostdata;
  1630. if ((pScsiReq->TargetID == vtarget->id) &&
  1631. (pScsiReq->Bus == vtarget->channel) &&
  1632. (vtarget->tflags & MPT_TARGET_FLAGS_Q_YES))
  1633. qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
  1634. }
  1635. /* Have the IOCTL driver set the direction based
  1636. * on the dataOutSize (ordering issue with Sparc).
  1637. */
  1638. if (karg.dataOutSize > 0) {
  1639. scsidir = MPI_SCSIIO_CONTROL_WRITE;
  1640. dataSize = karg.dataOutSize;
  1641. } else {
  1642. scsidir = MPI_SCSIIO_CONTROL_READ;
  1643. dataSize = karg.dataInSize;
  1644. }
  1645. pScsiReq->Control = cpu_to_le32(scsidir | qtag);
  1646. pScsiReq->DataLength = cpu_to_le32(dataSize);
  1647. ioc->ioctl->reset = MPTCTL_RESET_OK;
  1648. ioc->ioctl->id = pScsiReq->TargetID;
  1649. } else {
  1650. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1651. "SCSI driver is not loaded. \n",
  1652. __FILE__, __LINE__);
  1653. rc = -EFAULT;
  1654. goto done_free_mem;
  1655. }
  1656. break;
  1657. case MPI_FUNCTION_SMP_PASSTHROUGH:
  1658. /* Check mf->PassthruFlags to determine if
  1659. * transfer is ImmediateMode or not.
  1660. * Immediate mode returns data in the ReplyFrame.
  1661. * Else, we are sending request and response data
  1662. * in two SGLs at the end of the mf.
  1663. */
  1664. break;
  1665. case MPI_FUNCTION_SATA_PASSTHROUGH:
  1666. if (!ioc->sh) {
  1667. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1668. "SCSI driver is not loaded. \n",
  1669. __FILE__, __LINE__);
  1670. rc = -EFAULT;
  1671. goto done_free_mem;
  1672. }
  1673. break;
  1674. case MPI_FUNCTION_RAID_ACTION:
  1675. /* Just add a SGE
  1676. */
  1677. break;
  1678. case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  1679. if (ioc->sh) {
  1680. SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
  1681. int qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
  1682. int scsidir = MPI_SCSIIO_CONTROL_READ;
  1683. int dataSize;
  1684. pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
  1685. pScsiReq->MsgFlags |= mpt_msg_flags();
  1686. /* verify that app has not requested
  1687. * more sense data than driver
  1688. * can provide, if so, reset this parameter
  1689. * set the sense buffer pointer low address
  1690. * update the control field to specify Q type
  1691. */
  1692. if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
  1693. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  1694. else
  1695. pScsiReq->SenseBufferLength = karg.maxSenseBytes;
  1696. pScsiReq->SenseBufferLowAddr =
  1697. cpu_to_le32(ioc->sense_buf_low_dma
  1698. + (req_idx * MPT_SENSE_BUFFER_ALLOC));
  1699. /* All commands to physical devices are tagged
  1700. */
  1701. /* Have the IOCTL driver set the direction based
  1702. * on the dataOutSize (ordering issue with Sparc).
  1703. */
  1704. if (karg.dataOutSize > 0) {
  1705. scsidir = MPI_SCSIIO_CONTROL_WRITE;
  1706. dataSize = karg.dataOutSize;
  1707. } else {
  1708. scsidir = MPI_SCSIIO_CONTROL_READ;
  1709. dataSize = karg.dataInSize;
  1710. }
  1711. pScsiReq->Control = cpu_to_le32(scsidir | qtag);
  1712. pScsiReq->DataLength = cpu_to_le32(dataSize);
  1713. ioc->ioctl->reset = MPTCTL_RESET_OK;
  1714. ioc->ioctl->id = pScsiReq->TargetID;
  1715. } else {
  1716. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1717. "SCSI driver is not loaded. \n",
  1718. __FILE__, __LINE__);
  1719. rc = -EFAULT;
  1720. goto done_free_mem;
  1721. }
  1722. break;
  1723. case MPI_FUNCTION_SCSI_TASK_MGMT:
  1724. {
  1725. MPT_SCSI_HOST *hd = NULL;
  1726. if ((ioc->sh == NULL) || ((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL)) {
  1727. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1728. "SCSI driver not loaded or SCSI host not found. \n",
  1729. __FILE__, __LINE__);
  1730. rc = -EFAULT;
  1731. goto done_free_mem;
  1732. } else if (mptctl_set_tm_flags(hd) != 0) {
  1733. rc = -EPERM;
  1734. goto done_free_mem;
  1735. }
  1736. }
  1737. break;
  1738. case MPI_FUNCTION_IOC_INIT:
  1739. {
  1740. IOCInit_t *pInit = (IOCInit_t *) mf;
  1741. u32 high_addr, sense_high;
  1742. /* Verify that all entries in the IOC INIT match
  1743. * existing setup (and in LE format).
  1744. */
  1745. if (sizeof(dma_addr_t) == sizeof(u64)) {
  1746. high_addr = cpu_to_le32((u32)((u64)ioc->req_frames_dma >> 32));
  1747. sense_high= cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
  1748. } else {
  1749. high_addr = 0;
  1750. sense_high= 0;
  1751. }
  1752. if ((pInit->Flags != 0) || (pInit->MaxDevices != ioc->facts.MaxDevices) ||
  1753. (pInit->MaxBuses != ioc->facts.MaxBuses) ||
  1754. (pInit->ReplyFrameSize != cpu_to_le16(ioc->reply_sz)) ||
  1755. (pInit->HostMfaHighAddr != high_addr) ||
  1756. (pInit->SenseBufferHighAddr != sense_high)) {
  1757. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1758. "IOC_INIT issued with 1 or more incorrect parameters. Rejected.\n",
  1759. __FILE__, __LINE__);
  1760. rc = -EFAULT;
  1761. goto done_free_mem;
  1762. }
  1763. }
  1764. break;
  1765. default:
  1766. /*
  1767. * MPI_FUNCTION_PORT_ENABLE
  1768. * MPI_FUNCTION_TARGET_CMD_BUFFER_POST
  1769. * MPI_FUNCTION_TARGET_ASSIST
  1770. * MPI_FUNCTION_TARGET_STATUS_SEND
  1771. * MPI_FUNCTION_TARGET_MODE_ABORT
  1772. * MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET
  1773. * MPI_FUNCTION_IO_UNIT_RESET
  1774. * MPI_FUNCTION_HANDSHAKE
  1775. * MPI_FUNCTION_REPLY_FRAME_REMOVAL
  1776. * MPI_FUNCTION_EVENT_NOTIFICATION
  1777. * (driver handles event notification)
  1778. * MPI_FUNCTION_EVENT_ACK
  1779. */
  1780. /* What to do with these??? CHECK ME!!!
  1781. MPI_FUNCTION_FC_LINK_SRVC_BUF_POST
  1782. MPI_FUNCTION_FC_LINK_SRVC_RSP
  1783. MPI_FUNCTION_FC_ABORT
  1784. MPI_FUNCTION_LAN_SEND
  1785. MPI_FUNCTION_LAN_RECEIVE
  1786. MPI_FUNCTION_LAN_RESET
  1787. */
  1788. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1789. "Illegal request (function 0x%x) \n",
  1790. __FILE__, __LINE__, hdr->Function);
  1791. rc = -EFAULT;
  1792. goto done_free_mem;
  1793. }
  1794. /* Add the SGL ( at most one data in SGE and one data out SGE )
  1795. * In the case of two SGE's - the data out (write) will always
  1796. * preceede the data in (read) SGE. psgList is used to free the
  1797. * allocated memory.
  1798. */
  1799. psge = (char *) (((int *) mf) + karg.dataSgeOffset);
  1800. flagsLength = 0;
  1801. /* bufIn and bufOut are used for user to kernel space transfers
  1802. */
  1803. bufIn.kptr = bufOut.kptr = NULL;
  1804. bufIn.len = bufOut.len = 0;
  1805. if (karg.dataOutSize > 0)
  1806. sgSize ++;
  1807. if (karg.dataInSize > 0)
  1808. sgSize ++;
  1809. if (sgSize > 0) {
  1810. /* Set up the dataOut memory allocation */
  1811. if (karg.dataOutSize > 0) {
  1812. if (karg.dataInSize > 0) {
  1813. flagsLength = ( MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  1814. MPI_SGE_FLAGS_END_OF_BUFFER |
  1815. MPI_SGE_FLAGS_DIRECTION |
  1816. mpt_addr_size() )
  1817. << MPI_SGE_FLAGS_SHIFT;
  1818. } else {
  1819. flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE;
  1820. }
  1821. flagsLength |= karg.dataOutSize;
  1822. bufOut.len = karg.dataOutSize;
  1823. bufOut.kptr = pci_alloc_consistent(
  1824. ioc->pcidev, bufOut.len, &dma_addr_out);
  1825. if (bufOut.kptr == NULL) {
  1826. rc = -ENOMEM;
  1827. goto done_free_mem;
  1828. } else {
  1829. /* Set up this SGE.
  1830. * Copy to MF and to sglbuf
  1831. */
  1832. mpt_add_sge(psge, flagsLength, dma_addr_out);
  1833. psge += (sizeof(u32) + sizeof(dma_addr_t));
  1834. /* Copy user data to kernel space.
  1835. */
  1836. if (copy_from_user(bufOut.kptr,
  1837. karg.dataOutBufPtr,
  1838. bufOut.len)) {
  1839. printk(KERN_ERR
  1840. "%s@%d::mptctl_do_mpt_command - Unable "
  1841. "to read user data "
  1842. "struct @ %p\n",
  1843. __FILE__, __LINE__,karg.dataOutBufPtr);
  1844. rc = -EFAULT;
  1845. goto done_free_mem;
  1846. }
  1847. }
  1848. }
  1849. if (karg.dataInSize > 0) {
  1850. flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
  1851. flagsLength |= karg.dataInSize;
  1852. bufIn.len = karg.dataInSize;
  1853. bufIn.kptr = pci_alloc_consistent(ioc->pcidev,
  1854. bufIn.len, &dma_addr_in);
  1855. if (bufIn.kptr == NULL) {
  1856. rc = -ENOMEM;
  1857. goto done_free_mem;
  1858. } else {
  1859. /* Set up this SGE
  1860. * Copy to MF and to sglbuf
  1861. */
  1862. mpt_add_sge(psge, flagsLength, dma_addr_in);
  1863. }
  1864. }
  1865. } else {
  1866. /* Add a NULL SGE
  1867. */
  1868. mpt_add_sge(psge, flagsLength, (dma_addr_t) -1);
  1869. }
  1870. ioc->ioctl->wait_done = 0;
  1871. if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT) {
  1872. DBG_DUMP_TM_REQUEST_FRAME((u32 *)mf);
  1873. if (mpt_send_handshake_request(mptctl_id, ioc,
  1874. sizeof(SCSITaskMgmt_t), (u32*)mf,
  1875. CAN_SLEEP) != 0) {
  1876. dfailprintk((MYIOC_s_ERR_FMT "_send_handshake FAILED!"
  1877. " (ioc %p, mf %p) \n", ioc->name,
  1878. ioc, mf));
  1879. mptctl_free_tm_flags(ioc);
  1880. rc = -ENODATA;
  1881. goto done_free_mem;
  1882. }
  1883. } else
  1884. mpt_put_msg_frame(mptctl_id, ioc, mf);
  1885. /* Now wait for the command to complete */
  1886. timeout = (karg.timeout > 0) ? karg.timeout : MPT_IOCTL_DEFAULT_TIMEOUT;
  1887. timeout = wait_event_timeout(mptctl_wait,
  1888. ioc->ioctl->wait_done == 1,
  1889. HZ*timeout);
  1890. if(timeout <=0 && (ioc->ioctl->wait_done != 1 )) {
  1891. /* Now we need to reset the board */
  1892. if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT)
  1893. mptctl_free_tm_flags(ioc);
  1894. mptctl_timeout_expired(ioc->ioctl);
  1895. rc = -ENODATA;
  1896. goto done_free_mem;
  1897. }
  1898. mf = NULL;
  1899. /* If a valid reply frame, copy to the user.
  1900. * Offset 2: reply length in U32's
  1901. */
  1902. if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID) {
  1903. if (karg.maxReplyBytes < ioc->reply_sz) {
  1904. sz = min(karg.maxReplyBytes, 4*ioc->ioctl->ReplyFrame[2]);
  1905. } else {
  1906. sz = min(ioc->reply_sz, 4*ioc->ioctl->ReplyFrame[2]);
  1907. }
  1908. if (sz > 0) {
  1909. if (copy_to_user(karg.replyFrameBufPtr,
  1910. &ioc->ioctl->ReplyFrame, sz)){
  1911. printk(KERN_ERR
  1912. "%s@%d::mptctl_do_mpt_command - "
  1913. "Unable to write out reply frame %p\n",
  1914. __FILE__, __LINE__, karg.replyFrameBufPtr);
  1915. rc = -ENODATA;
  1916. goto done_free_mem;
  1917. }
  1918. }
  1919. }
  1920. /* If valid sense data, copy to user.
  1921. */
  1922. if (ioc->ioctl->status & MPT_IOCTL_STATUS_SENSE_VALID) {
  1923. sz = min(karg.maxSenseBytes, MPT_SENSE_BUFFER_SIZE);
  1924. if (sz > 0) {
  1925. if (copy_to_user(karg.senseDataPtr, ioc->ioctl->sense, sz)) {
  1926. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1927. "Unable to write sense data to user %p\n",
  1928. __FILE__, __LINE__,
  1929. karg.senseDataPtr);
  1930. rc = -ENODATA;
  1931. goto done_free_mem;
  1932. }
  1933. }
  1934. }
  1935. /* If the overall status is _GOOD and data in, copy data
  1936. * to user.
  1937. */
  1938. if ((ioc->ioctl->status & MPT_IOCTL_STATUS_COMMAND_GOOD) &&
  1939. (karg.dataInSize > 0) && (bufIn.kptr)) {
  1940. if (copy_to_user(karg.dataInBufPtr,
  1941. bufIn.kptr, karg.dataInSize)) {
  1942. printk(KERN_ERR "%s@%d::mptctl_do_mpt_command - "
  1943. "Unable to write data to user %p\n",
  1944. __FILE__, __LINE__,
  1945. karg.dataInBufPtr);
  1946. rc = -ENODATA;
  1947. }
  1948. }
  1949. done_free_mem:
  1950. ioc->ioctl->status &= ~(MPT_IOCTL_STATUS_COMMAND_GOOD |
  1951. MPT_IOCTL_STATUS_SENSE_VALID |
  1952. MPT_IOCTL_STATUS_RF_VALID );
  1953. /* Free the allocated memory.
  1954. */
  1955. if (bufOut.kptr != NULL) {
  1956. pci_free_consistent(ioc->pcidev,
  1957. bufOut.len, (void *) bufOut.kptr, dma_addr_out);
  1958. }
  1959. if (bufIn.kptr != NULL) {
  1960. pci_free_consistent(ioc->pcidev,
  1961. bufIn.len, (void *) bufIn.kptr, dma_addr_in);
  1962. }
  1963. /* mf is null if command issued successfully
  1964. * otherwise, failure occured after mf acquired.
  1965. */
  1966. if (mf)
  1967. mpt_free_msg_frame(ioc, mf);
  1968. return rc;
  1969. }
  1970. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1971. /* Prototype Routine for the HOST INFO command.
  1972. *
  1973. * Outputs: None.
  1974. * Return: 0 if successful
  1975. * -EFAULT if data unavailable
  1976. * -EBUSY if previous command timout and IOC reset is not complete.
  1977. * -ENODEV if no such device/adapter
  1978. * -ETIME if timer expires
  1979. * -ENOMEM if memory allocation error
  1980. */
  1981. static int
  1982. mptctl_hp_hostinfo(unsigned long arg, unsigned int data_size)
  1983. {
  1984. hp_host_info_t __user *uarg = (void __user *) arg;
  1985. MPT_ADAPTER *ioc;
  1986. struct pci_dev *pdev;
  1987. char *pbuf=NULL;
  1988. dma_addr_t buf_dma;
  1989. hp_host_info_t karg;
  1990. CONFIGPARMS cfg;
  1991. ConfigPageHeader_t hdr;
  1992. int iocnum;
  1993. int rc, cim_rev;
  1994. ToolboxIstwiReadWriteRequest_t *IstwiRWRequest;
  1995. MPT_FRAME_HDR *mf = NULL;
  1996. MPIHeader_t *mpi_hdr;
  1997. dctlprintk((": mptctl_hp_hostinfo called.\n"));
  1998. /* Reset long to int. Should affect IA64 and SPARC only
  1999. */
  2000. if (data_size == sizeof(hp_host_info_t))
  2001. cim_rev = 1;
  2002. else if (data_size == sizeof(hp_host_info_rev0_t))
  2003. cim_rev = 0; /* obsolete */
  2004. else
  2005. return -EFAULT;
  2006. if (copy_from_user(&karg, uarg, sizeof(hp_host_info_t))) {
  2007. printk(KERN_ERR "%s@%d::mptctl_hp_host_info - "
  2008. "Unable to read in hp_host_info struct @ %p\n",
  2009. __FILE__, __LINE__, uarg);
  2010. return -EFAULT;
  2011. }
  2012. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  2013. (ioc == NULL)) {
  2014. dctlprintk((KERN_ERR "%s::mptctl_hp_hostinfo() @%d - ioc%d not found!\n",
  2015. __FILE__, __LINE__, iocnum));
  2016. return -ENODEV;
  2017. }
  2018. /* Fill in the data and return the structure to the calling
  2019. * program
  2020. */
  2021. pdev = (struct pci_dev *) ioc->pcidev;
  2022. karg.vendor = pdev->vendor;
  2023. karg.device = pdev->device;
  2024. karg.subsystem_id = pdev->subsystem_device;
  2025. karg.subsystem_vendor = pdev->subsystem_vendor;
  2026. karg.devfn = pdev->devfn;
  2027. karg.bus = pdev->bus->number;
  2028. /* Save the SCSI host no. if
  2029. * SCSI driver loaded
  2030. */
  2031. if (ioc->sh != NULL)
  2032. karg.host_no = ioc->sh->host_no;
  2033. else
  2034. karg.host_no = -1;
  2035. /* Reformat the fw_version into a string
  2036. */
  2037. karg.fw_version[0] = ioc->facts.FWVersion.Struct.Major >= 10 ?
  2038. ((ioc->facts.FWVersion.Struct.Major / 10) + '0') : '0';
  2039. karg.fw_version[1] = (ioc->facts.FWVersion.Struct.Major % 10 ) + '0';
  2040. karg.fw_version[2] = '.';
  2041. karg.fw_version[3] = ioc->facts.FWVersion.Struct.Minor >= 10 ?
  2042. ((ioc->facts.FWVersion.Struct.Minor / 10) + '0') : '0';
  2043. karg.fw_version[4] = (ioc->facts.FWVersion.Struct.Minor % 10 ) + '0';
  2044. karg.fw_version[5] = '.';
  2045. karg.fw_version[6] = ioc->facts.FWVersion.Struct.Unit >= 10 ?
  2046. ((ioc->facts.FWVersion.Struct.Unit / 10) + '0') : '0';
  2047. karg.fw_version[7] = (ioc->facts.FWVersion.Struct.Unit % 10 ) + '0';
  2048. karg.fw_version[8] = '.';
  2049. karg.fw_version[9] = ioc->facts.FWVersion.Struct.Dev >= 10 ?
  2050. ((ioc->facts.FWVersion.Struct.Dev / 10) + '0') : '0';
  2051. karg.fw_version[10] = (ioc->facts.FWVersion.Struct.Dev % 10 ) + '0';
  2052. karg.fw_version[11] = '\0';
  2053. /* Issue a config request to get the device serial number
  2054. */
  2055. hdr.PageVersion = 0;
  2056. hdr.PageLength = 0;
  2057. hdr.PageNumber = 0;
  2058. hdr.PageType = MPI_CONFIG_PAGETYPE_MANUFACTURING;
  2059. cfg.cfghdr.hdr = &hdr;
  2060. cfg.physAddr = -1;
  2061. cfg.pageAddr = 0;
  2062. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2063. cfg.dir = 0; /* read */
  2064. cfg.timeout = 10;
  2065. strncpy(karg.serial_number, " ", 24);
  2066. if (mpt_config(ioc, &cfg) == 0) {
  2067. if (cfg.cfghdr.hdr->PageLength > 0) {
  2068. /* Issue the second config page request */
  2069. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2070. pbuf = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4, &buf_dma);
  2071. if (pbuf) {
  2072. cfg.physAddr = buf_dma;
  2073. if (mpt_config(ioc, &cfg) == 0) {
  2074. ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;
  2075. if (strlen(pdata->BoardTracerNumber) > 1) {
  2076. strncpy(karg.serial_number, pdata->BoardTracerNumber, 24);
  2077. karg.serial_number[24-1]='\0';
  2078. }
  2079. }
  2080. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, pbuf, buf_dma);
  2081. pbuf = NULL;
  2082. }
  2083. }
  2084. }
  2085. rc = mpt_GetIocState(ioc, 1);
  2086. switch (rc) {
  2087. case MPI_IOC_STATE_OPERATIONAL:
  2088. karg.ioc_status = HP_STATUS_OK;
  2089. break;
  2090. case MPI_IOC_STATE_FAULT:
  2091. karg.ioc_status = HP_STATUS_FAILED;
  2092. break;
  2093. case MPI_IOC_STATE_RESET:
  2094. case MPI_IOC_STATE_READY:
  2095. default:
  2096. karg.ioc_status = HP_STATUS_OTHER;
  2097. break;
  2098. }
  2099. karg.base_io_addr = pci_resource_start(pdev, 0);
  2100. if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
  2101. karg.bus_phys_width = HP_BUS_WIDTH_UNK;
  2102. else
  2103. karg.bus_phys_width = HP_BUS_WIDTH_16;
  2104. karg.hard_resets = 0;
  2105. karg.soft_resets = 0;
  2106. karg.timeouts = 0;
  2107. if (ioc->sh != NULL) {
  2108. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
  2109. if (hd && (cim_rev == 1)) {
  2110. karg.hard_resets = hd->hard_resets;
  2111. karg.soft_resets = hd->soft_resets;
  2112. karg.timeouts = hd->timeouts;
  2113. }
  2114. }
  2115. /*
  2116. * Gather ISTWI(Industry Standard Two Wire Interface) Data
  2117. */
  2118. if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL) {
  2119. dfailprintk((MYIOC_s_WARN_FMT "%s, no msg frames!!\n",
  2120. ioc->name,__FUNCTION__));
  2121. goto out;
  2122. }
  2123. IstwiRWRequest = (ToolboxIstwiReadWriteRequest_t *)mf;
  2124. mpi_hdr = (MPIHeader_t *) mf;
  2125. memset(IstwiRWRequest,0,sizeof(ToolboxIstwiReadWriteRequest_t));
  2126. IstwiRWRequest->Function = MPI_FUNCTION_TOOLBOX;
  2127. IstwiRWRequest->Tool = MPI_TOOLBOX_ISTWI_READ_WRITE_TOOL;
  2128. IstwiRWRequest->MsgContext = mpi_hdr->MsgContext;
  2129. IstwiRWRequest->Flags = MPI_TB_ISTWI_FLAGS_READ;
  2130. IstwiRWRequest->NumAddressBytes = 0x01;
  2131. IstwiRWRequest->DataLength = cpu_to_le16(0x04);
  2132. if (pdev->devfn & 1)
  2133. IstwiRWRequest->DeviceAddr = 0xB2;
  2134. else
  2135. IstwiRWRequest->DeviceAddr = 0xB0;
  2136. pbuf = pci_alloc_consistent(ioc->pcidev, 4, &buf_dma);
  2137. if (!pbuf)
  2138. goto out;
  2139. mpt_add_sge((char *)&IstwiRWRequest->SGL,
  2140. (MPT_SGE_FLAGS_SSIMPLE_READ|4), buf_dma);
  2141. ioc->ioctl->wait_done = 0;
  2142. mpt_put_msg_frame(mptctl_id, ioc, mf);
  2143. rc = wait_event_timeout(mptctl_wait,
  2144. ioc->ioctl->wait_done == 1,
  2145. HZ*MPT_IOCTL_DEFAULT_TIMEOUT /* 10 sec */);
  2146. if(rc <=0 && (ioc->ioctl->wait_done != 1 )) {
  2147. /*
  2148. * Now we need to reset the board
  2149. */
  2150. mpt_free_msg_frame(ioc, mf);
  2151. mptctl_timeout_expired(ioc->ioctl);
  2152. goto out;
  2153. }
  2154. /*
  2155. *ISTWI Data Definition
  2156. * pbuf[0] = FW_VERSION = 0x4
  2157. * pbuf[1] = Bay Count = 6 or 4 or 2, depending on
  2158. * the config, you should be seeing one out of these three values
  2159. * pbuf[2] = Drive Installed Map = bit pattern depend on which
  2160. * bays have drives in them
  2161. * pbuf[3] = Checksum (0x100 = (byte0 + byte2 + byte3)
  2162. */
  2163. if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID)
  2164. karg.rsvd = *(u32 *)pbuf;
  2165. out:
  2166. if (pbuf)
  2167. pci_free_consistent(ioc->pcidev, 4, pbuf, buf_dma);
  2168. /* Copy the data from kernel memory to user memory
  2169. */
  2170. if (copy_to_user((char __user *)arg, &karg, sizeof(hp_host_info_t))) {
  2171. printk(KERN_ERR "%s@%d::mptctl_hpgethostinfo - "
  2172. "Unable to write out hp_host_info @ %p\n",
  2173. __FILE__, __LINE__, uarg);
  2174. return -EFAULT;
  2175. }
  2176. return 0;
  2177. }
  2178. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2179. /* Prototype Routine for the TARGET INFO command.
  2180. *
  2181. * Outputs: None.
  2182. * Return: 0 if successful
  2183. * -EFAULT if data unavailable
  2184. * -EBUSY if previous command timout and IOC reset is not complete.
  2185. * -ENODEV if no such device/adapter
  2186. * -ETIME if timer expires
  2187. * -ENOMEM if memory allocation error
  2188. */
  2189. static int
  2190. mptctl_hp_targetinfo(unsigned long arg)
  2191. {
  2192. hp_target_info_t __user *uarg = (void __user *) arg;
  2193. SCSIDevicePage0_t *pg0_alloc;
  2194. SCSIDevicePage3_t *pg3_alloc;
  2195. MPT_ADAPTER *ioc;
  2196. MPT_SCSI_HOST *hd = NULL;
  2197. hp_target_info_t karg;
  2198. int iocnum;
  2199. int data_sz;
  2200. dma_addr_t page_dma;
  2201. CONFIGPARMS cfg;
  2202. ConfigPageHeader_t hdr;
  2203. int tmp, np, rc = 0;
  2204. dctlprintk((": mptctl_hp_targetinfo called.\n"));
  2205. if (copy_from_user(&karg, uarg, sizeof(hp_target_info_t))) {
  2206. printk(KERN_ERR "%s@%d::mptctl_hp_targetinfo - "
  2207. "Unable to read in hp_host_targetinfo struct @ %p\n",
  2208. __FILE__, __LINE__, uarg);
  2209. return -EFAULT;
  2210. }
  2211. if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
  2212. (ioc == NULL)) {
  2213. dctlprintk((KERN_ERR "%s::mptctl_hp_targetinfo() @%d - ioc%d not found!\n",
  2214. __FILE__, __LINE__, iocnum));
  2215. return -ENODEV;
  2216. }
  2217. /* There is nothing to do for FCP parts.
  2218. */
  2219. if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
  2220. return 0;
  2221. if ((ioc->spi_data.sdp0length == 0) || (ioc->sh == NULL))
  2222. return 0;
  2223. if (ioc->sh->host_no != karg.hdr.host)
  2224. return -ENODEV;
  2225. /* Get the data transfer speeds
  2226. */
  2227. data_sz = ioc->spi_data.sdp0length * 4;
  2228. pg0_alloc = (SCSIDevicePage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page_dma);
  2229. if (pg0_alloc) {
  2230. hdr.PageVersion = ioc->spi_data.sdp0version;
  2231. hdr.PageLength = data_sz;
  2232. hdr.PageNumber = 0;
  2233. hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  2234. cfg.cfghdr.hdr = &hdr;
  2235. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2236. cfg.dir = 0;
  2237. cfg.timeout = 0;
  2238. cfg.physAddr = page_dma;
  2239. cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
  2240. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  2241. np = le32_to_cpu(pg0_alloc->NegotiatedParameters);
  2242. karg.negotiated_width = np & MPI_SCSIDEVPAGE0_NP_WIDE ?
  2243. HP_BUS_WIDTH_16 : HP_BUS_WIDTH_8;
  2244. if (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) {
  2245. tmp = (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
  2246. if (tmp < 0x09)
  2247. karg.negotiated_speed = HP_DEV_SPEED_ULTRA320;
  2248. else if (tmp <= 0x09)
  2249. karg.negotiated_speed = HP_DEV_SPEED_ULTRA160;
  2250. else if (tmp <= 0x0A)
  2251. karg.negotiated_speed = HP_DEV_SPEED_ULTRA2;
  2252. else if (tmp <= 0x0C)
  2253. karg.negotiated_speed = HP_DEV_SPEED_ULTRA;
  2254. else if (tmp <= 0x25)
  2255. karg.negotiated_speed = HP_DEV_SPEED_FAST;
  2256. else
  2257. karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
  2258. } else
  2259. karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
  2260. }
  2261. pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg0_alloc, page_dma);
  2262. }
  2263. /* Set defaults
  2264. */
  2265. karg.message_rejects = -1;
  2266. karg.phase_errors = -1;
  2267. karg.parity_errors = -1;
  2268. karg.select_timeouts = -1;
  2269. /* Get the target error parameters
  2270. */
  2271. hdr.PageVersion = 0;
  2272. hdr.PageLength = 0;
  2273. hdr.PageNumber = 3;
  2274. hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  2275. cfg.cfghdr.hdr = &hdr;
  2276. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2277. cfg.dir = 0;
  2278. cfg.timeout = 0;
  2279. cfg.physAddr = -1;
  2280. if ((mpt_config(ioc, &cfg) == 0) && (cfg.cfghdr.hdr->PageLength > 0)) {
  2281. /* Issue the second config page request */
  2282. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2283. data_sz = (int) cfg.cfghdr.hdr->PageLength * 4;
  2284. pg3_alloc = (SCSIDevicePage3_t *) pci_alloc_consistent(
  2285. ioc->pcidev, data_sz, &page_dma);
  2286. if (pg3_alloc) {
  2287. cfg.physAddr = page_dma;
  2288. cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
  2289. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  2290. karg.message_rejects = (u32) le16_to_cpu(pg3_alloc->MsgRejectCount);
  2291. karg.phase_errors = (u32) le16_to_cpu(pg3_alloc->PhaseErrorCount);
  2292. karg.parity_errors = (u32) le16_to_cpu(pg3_alloc->ParityErrorCount);
  2293. }
  2294. pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg3_alloc, page_dma);
  2295. }
  2296. }
  2297. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  2298. if (hd != NULL)
  2299. karg.select_timeouts = hd->sel_timeout[karg.hdr.id];
  2300. /* Copy the data from kernel memory to user memory
  2301. */
  2302. if (copy_to_user((char __user *)arg, &karg, sizeof(hp_target_info_t))) {
  2303. printk(KERN_ERR "%s@%d::mptctl_hp_target_info - "
  2304. "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
  2305. __FILE__, __LINE__, uarg);
  2306. return -EFAULT;
  2307. }
  2308. return 0;
  2309. }
  2310. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2311. static const struct file_operations mptctl_fops = {
  2312. .owner = THIS_MODULE,
  2313. .llseek = no_llseek,
  2314. .release = mptctl_release,
  2315. .fasync = mptctl_fasync,
  2316. .unlocked_ioctl = mptctl_ioctl,
  2317. #ifdef CONFIG_COMPAT
  2318. .compat_ioctl = compat_mpctl_ioctl,
  2319. #endif
  2320. };
  2321. static struct miscdevice mptctl_miscdev = {
  2322. MPT_MINOR,
  2323. MYNAM,
  2324. &mptctl_fops
  2325. };
  2326. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2327. #ifdef CONFIG_COMPAT
  2328. static int
  2329. compat_mptfwxfer_ioctl(struct file *filp, unsigned int cmd,
  2330. unsigned long arg)
  2331. {
  2332. struct mpt_fw_xfer32 kfw32;
  2333. struct mpt_fw_xfer kfw;
  2334. MPT_ADAPTER *iocp = NULL;
  2335. int iocnum, iocnumX;
  2336. int nonblock = (filp->f_flags & O_NONBLOCK);
  2337. int ret;
  2338. dctlprintk((KERN_INFO MYNAM "::compat_mptfwxfer_ioctl() called\n"));
  2339. if (copy_from_user(&kfw32, (char __user *)arg, sizeof(kfw32)))
  2340. return -EFAULT;
  2341. /* Verify intended MPT adapter */
  2342. iocnumX = kfw32.iocnum & 0xFF;
  2343. if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
  2344. (iocp == NULL)) {
  2345. dctlprintk((KERN_ERR MYNAM "::compat_mptfwxfer_ioctl @%d - ioc%d not found!\n",
  2346. __LINE__, iocnumX));
  2347. return -ENODEV;
  2348. }
  2349. if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
  2350. return ret;
  2351. kfw.iocnum = iocnum;
  2352. kfw.fwlen = kfw32.fwlen;
  2353. kfw.bufp = compat_ptr(kfw32.bufp);
  2354. ret = mptctl_do_fw_download(kfw.iocnum, kfw.bufp, kfw.fwlen);
  2355. mutex_unlock(&iocp->ioctl->ioctl_mutex);
  2356. return ret;
  2357. }
  2358. static int
  2359. compat_mpt_command(struct file *filp, unsigned int cmd,
  2360. unsigned long arg)
  2361. {
  2362. struct mpt_ioctl_command32 karg32;
  2363. struct mpt_ioctl_command32 __user *uarg = (struct mpt_ioctl_command32 __user *) arg;
  2364. struct mpt_ioctl_command karg;
  2365. MPT_ADAPTER *iocp = NULL;
  2366. int iocnum, iocnumX;
  2367. int nonblock = (filp->f_flags & O_NONBLOCK);
  2368. int ret;
  2369. dctlprintk((KERN_INFO MYNAM "::compat_mpt_command() called\n"));
  2370. if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32)))
  2371. return -EFAULT;
  2372. /* Verify intended MPT adapter */
  2373. iocnumX = karg32.hdr.iocnum & 0xFF;
  2374. if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
  2375. (iocp == NULL)) {
  2376. dctlprintk((KERN_ERR MYNAM "::compat_mpt_command @%d - ioc%d not found!\n",
  2377. __LINE__, iocnumX));
  2378. return -ENODEV;
  2379. }
  2380. if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
  2381. return ret;
  2382. /* Copy data to karg */
  2383. karg.hdr.iocnum = karg32.hdr.iocnum;
  2384. karg.hdr.port = karg32.hdr.port;
  2385. karg.timeout = karg32.timeout;
  2386. karg.maxReplyBytes = karg32.maxReplyBytes;
  2387. karg.dataInSize = karg32.dataInSize;
  2388. karg.dataOutSize = karg32.dataOutSize;
  2389. karg.maxSenseBytes = karg32.maxSenseBytes;
  2390. karg.dataSgeOffset = karg32.dataSgeOffset;
  2391. karg.replyFrameBufPtr = (char __user *)(unsigned long)karg32.replyFrameBufPtr;
  2392. karg.dataInBufPtr = (char __user *)(unsigned long)karg32.dataInBufPtr;
  2393. karg.dataOutBufPtr = (char __user *)(unsigned long)karg32.dataOutBufPtr;
  2394. karg.senseDataPtr = (char __user *)(unsigned long)karg32.senseDataPtr;
  2395. /* Pass new structure to do_mpt_command
  2396. */
  2397. ret = mptctl_do_mpt_command (karg, &uarg->MF);
  2398. mutex_unlock(&iocp->ioctl->ioctl_mutex);
  2399. return ret;
  2400. }
  2401. static long compat_mpctl_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  2402. {
  2403. long ret;
  2404. lock_kernel();
  2405. switch (cmd) {
  2406. case MPTIOCINFO:
  2407. case MPTIOCINFO1:
  2408. case MPTIOCINFO2:
  2409. case MPTTARGETINFO:
  2410. case MPTEVENTQUERY:
  2411. case MPTEVENTENABLE:
  2412. case MPTEVENTREPORT:
  2413. case MPTHARDRESET:
  2414. case HP_GETHOSTINFO:
  2415. case HP_GETTARGETINFO:
  2416. case MPTTEST:
  2417. ret = __mptctl_ioctl(f, cmd, arg);
  2418. break;
  2419. case MPTCOMMAND32:
  2420. ret = compat_mpt_command(f, cmd, arg);
  2421. break;
  2422. case MPTFWDOWNLOAD32:
  2423. ret = compat_mptfwxfer_ioctl(f, cmd, arg);
  2424. break;
  2425. default:
  2426. ret = -ENOIOCTLCMD;
  2427. break;
  2428. }
  2429. unlock_kernel();
  2430. return ret;
  2431. }
  2432. #endif
  2433. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2434. /*
  2435. * mptctl_probe - Installs ioctl devices per bus.
  2436. * @pdev: Pointer to pci_dev structure
  2437. *
  2438. * Returns 0 for success, non-zero for failure.
  2439. *
  2440. */
  2441. static int
  2442. mptctl_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2443. {
  2444. int err;
  2445. int sz;
  2446. u8 *mem;
  2447. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  2448. /*
  2449. * Allocate and inite a MPT_IOCTL structure
  2450. */
  2451. sz = sizeof (MPT_IOCTL);
  2452. mem = kmalloc(sz, GFP_KERNEL);
  2453. if (mem == NULL) {
  2454. err = -ENOMEM;
  2455. goto out_fail;
  2456. }
  2457. memset(mem, 0, sz);
  2458. ioc->ioctl = (MPT_IOCTL *) mem;
  2459. ioc->ioctl->ioc = ioc;
  2460. mutex_init(&ioc->ioctl->ioctl_mutex);
  2461. return 0;
  2462. out_fail:
  2463. mptctl_remove(pdev);
  2464. return err;
  2465. }
  2466. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2467. /*
  2468. * mptctl_remove - Removed ioctl devices
  2469. * @pdev: Pointer to pci_dev structure
  2470. *
  2471. *
  2472. */
  2473. static void
  2474. mptctl_remove(struct pci_dev *pdev)
  2475. {
  2476. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  2477. kfree ( ioc->ioctl );
  2478. }
  2479. static struct mpt_pci_driver mptctl_driver = {
  2480. .probe = mptctl_probe,
  2481. .remove = mptctl_remove,
  2482. };
  2483. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2484. static int __init mptctl_init(void)
  2485. {
  2486. int err;
  2487. int where = 1;
  2488. show_mptmod_ver(my_NAME, my_VERSION);
  2489. if(mpt_device_driver_register(&mptctl_driver,
  2490. MPTCTL_DRIVER) != 0 ) {
  2491. dprintk((KERN_INFO MYNAM
  2492. ": failed to register dd callbacks\n"));
  2493. }
  2494. /* Register this device */
  2495. err = misc_register(&mptctl_miscdev);
  2496. if (err < 0) {
  2497. printk(KERN_ERR MYNAM ": Can't register misc device [minor=%d].\n", MPT_MINOR);
  2498. goto out_fail;
  2499. }
  2500. printk(KERN_INFO MYNAM ": Registered with Fusion MPT base driver\n");
  2501. printk(KERN_INFO MYNAM ": /dev/%s @ (major,minor=%d,%d)\n",
  2502. mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
  2503. /*
  2504. * Install our handler
  2505. */
  2506. ++where;
  2507. if ((mptctl_id = mpt_register(mptctl_reply, MPTCTL_DRIVER)) < 0) {
  2508. printk(KERN_ERR MYNAM ": ERROR: Failed to register with Fusion MPT base driver\n");
  2509. misc_deregister(&mptctl_miscdev);
  2510. err = -EBUSY;
  2511. goto out_fail;
  2512. }
  2513. if (mpt_reset_register(mptctl_id, mptctl_ioc_reset) == 0) {
  2514. dprintk((KERN_INFO MYNAM ": Registered for IOC reset notifications\n"));
  2515. } else {
  2516. /* FIXME! */
  2517. }
  2518. if (mpt_event_register(mptctl_id, mptctl_event_process) == 0) {
  2519. devtverboseprintk((KERN_INFO MYNAM
  2520. ": Registered for IOC event notifications\n"));
  2521. }
  2522. return 0;
  2523. out_fail:
  2524. mpt_device_driver_deregister(MPTCTL_DRIVER);
  2525. return err;
  2526. }
  2527. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2528. static void mptctl_exit(void)
  2529. {
  2530. misc_deregister(&mptctl_miscdev);
  2531. printk(KERN_INFO MYNAM ": Deregistered /dev/%s @ (major,minor=%d,%d)\n",
  2532. mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
  2533. /* De-register reset handler from base module */
  2534. mpt_reset_deregister(mptctl_id);
  2535. dprintk((KERN_INFO MYNAM ": Deregistered for IOC reset notifications\n"));
  2536. /* De-register callback handler from base module */
  2537. mpt_deregister(mptctl_id);
  2538. printk(KERN_INFO MYNAM ": Deregistered from Fusion MPT base driver\n");
  2539. mpt_device_driver_deregister(MPTCTL_DRIVER);
  2540. }
  2541. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2542. module_init(mptctl_init);
  2543. module_exit(mptctl_exit);