mptctl.c 82 KB

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