mptctl.c 81 KB

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