mptctl.c 83 KB

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