mpt2sas_ctl.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  1. /*
  2. * Management Module Support for MPT (Message Passing Technology) based
  3. * controllers
  4. *
  5. * This code is based on drivers/scsi/mpt2sas/mpt2_ctl.c
  6. * Copyright (C) 2007-2010 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/types.h>
  48. #include <linux/pci.h>
  49. #include <linux/delay.h>
  50. #include <linux/mutex.h>
  51. #include <linux/compat.h>
  52. #include <linux/poll.h>
  53. #include <linux/io.h>
  54. #include <linux/uaccess.h>
  55. #include "mpt2sas_base.h"
  56. #include "mpt2sas_ctl.h"
  57. static DEFINE_MUTEX(_ctl_mutex);
  58. static struct fasync_struct *async_queue;
  59. static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
  60. static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
  61. u8 *issue_reset);
  62. /**
  63. * enum block_state - blocking state
  64. * @NON_BLOCKING: non blocking
  65. * @BLOCKING: blocking
  66. *
  67. * These states are for ioctls that need to wait for a response
  68. * from firmware, so they probably require sleep.
  69. */
  70. enum block_state {
  71. NON_BLOCKING,
  72. BLOCKING,
  73. };
  74. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  75. /**
  76. * _ctl_sas_device_find_by_handle - sas device search
  77. * @ioc: per adapter object
  78. * @handle: sas device handle (assigned by firmware)
  79. * Context: Calling function should acquire ioc->sas_device_lock
  80. *
  81. * This searches for sas_device based on sas_address, then return sas_device
  82. * object.
  83. */
  84. static struct _sas_device *
  85. _ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  86. {
  87. struct _sas_device *sas_device, *r;
  88. r = NULL;
  89. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  90. if (sas_device->handle != handle)
  91. continue;
  92. r = sas_device;
  93. goto out;
  94. }
  95. out:
  96. return r;
  97. }
  98. /**
  99. * _ctl_display_some_debug - debug routine
  100. * @ioc: per adapter object
  101. * @smid: system request message index
  102. * @calling_function_name: string pass from calling function
  103. * @mpi_reply: reply message frame
  104. * Context: none.
  105. *
  106. * Function for displaying debug info helpful when debugging issues
  107. * in this module.
  108. */
  109. static void
  110. _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  111. char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
  112. {
  113. Mpi2ConfigRequest_t *mpi_request;
  114. char *desc = NULL;
  115. if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
  116. return;
  117. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  118. switch (mpi_request->Function) {
  119. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  120. {
  121. Mpi2SCSIIORequest_t *scsi_request =
  122. (Mpi2SCSIIORequest_t *)mpi_request;
  123. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  124. "scsi_io, cmd(0x%02x), cdb_len(%d)",
  125. scsi_request->CDB.CDB32[0],
  126. le16_to_cpu(scsi_request->IoFlags) & 0xF);
  127. desc = ioc->tmp_string;
  128. break;
  129. }
  130. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  131. desc = "task_mgmt";
  132. break;
  133. case MPI2_FUNCTION_IOC_INIT:
  134. desc = "ioc_init";
  135. break;
  136. case MPI2_FUNCTION_IOC_FACTS:
  137. desc = "ioc_facts";
  138. break;
  139. case MPI2_FUNCTION_CONFIG:
  140. {
  141. Mpi2ConfigRequest_t *config_request =
  142. (Mpi2ConfigRequest_t *)mpi_request;
  143. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  144. "config, type(0x%02x), ext_type(0x%02x), number(%d)",
  145. (config_request->Header.PageType &
  146. MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
  147. config_request->Header.PageNumber);
  148. desc = ioc->tmp_string;
  149. break;
  150. }
  151. case MPI2_FUNCTION_PORT_FACTS:
  152. desc = "port_facts";
  153. break;
  154. case MPI2_FUNCTION_PORT_ENABLE:
  155. desc = "port_enable";
  156. break;
  157. case MPI2_FUNCTION_EVENT_NOTIFICATION:
  158. desc = "event_notification";
  159. break;
  160. case MPI2_FUNCTION_FW_DOWNLOAD:
  161. desc = "fw_download";
  162. break;
  163. case MPI2_FUNCTION_FW_UPLOAD:
  164. desc = "fw_upload";
  165. break;
  166. case MPI2_FUNCTION_RAID_ACTION:
  167. desc = "raid_action";
  168. break;
  169. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  170. {
  171. Mpi2SCSIIORequest_t *scsi_request =
  172. (Mpi2SCSIIORequest_t *)mpi_request;
  173. snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
  174. "raid_pass, cmd(0x%02x), cdb_len(%d)",
  175. scsi_request->CDB.CDB32[0],
  176. le16_to_cpu(scsi_request->IoFlags) & 0xF);
  177. desc = ioc->tmp_string;
  178. break;
  179. }
  180. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  181. desc = "sas_iounit_cntl";
  182. break;
  183. case MPI2_FUNCTION_SATA_PASSTHROUGH:
  184. desc = "sata_pass";
  185. break;
  186. case MPI2_FUNCTION_DIAG_BUFFER_POST:
  187. desc = "diag_buffer_post";
  188. break;
  189. case MPI2_FUNCTION_DIAG_RELEASE:
  190. desc = "diag_release";
  191. break;
  192. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  193. desc = "smp_passthrough";
  194. break;
  195. }
  196. if (!desc)
  197. return;
  198. printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
  199. ioc->name, calling_function_name, desc, smid);
  200. if (!mpi_reply)
  201. return;
  202. if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
  203. printk(MPT2SAS_INFO_FMT
  204. "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
  205. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  206. le32_to_cpu(mpi_reply->IOCLogInfo));
  207. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  208. mpi_request->Function ==
  209. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  210. Mpi2SCSIIOReply_t *scsi_reply =
  211. (Mpi2SCSIIOReply_t *)mpi_reply;
  212. struct _sas_device *sas_device = NULL;
  213. unsigned long flags;
  214. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  215. sas_device = _ctl_sas_device_find_by_handle(ioc,
  216. le16_to_cpu(scsi_reply->DevHandle));
  217. if (sas_device) {
  218. printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
  219. "phy(%d)\n", ioc->name, (unsigned long long)
  220. sas_device->sas_address, sas_device->phy);
  221. printk(MPT2SAS_WARN_FMT
  222. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  223. ioc->name, sas_device->enclosure_logical_id,
  224. sas_device->slot);
  225. }
  226. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  227. if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
  228. printk(MPT2SAS_INFO_FMT
  229. "\tscsi_state(0x%02x), scsi_status"
  230. "(0x%02x)\n", ioc->name,
  231. scsi_reply->SCSIState,
  232. scsi_reply->SCSIStatus);
  233. }
  234. }
  235. #endif
  236. /**
  237. * mpt2sas_ctl_done - ctl module completion routine
  238. * @ioc: per adapter object
  239. * @smid: system request message index
  240. * @msix_index: MSIX table index supplied by the OS
  241. * @reply: reply message frame(lower 32bit addr)
  242. * Context: none.
  243. *
  244. * The callback handler when using ioc->ctl_cb_idx.
  245. *
  246. * Return 1 meaning mf should be freed from _base_interrupt
  247. * 0 means the mf is freed from this function.
  248. */
  249. u8
  250. mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  251. u32 reply)
  252. {
  253. MPI2DefaultReply_t *mpi_reply;
  254. Mpi2SCSIIOReply_t *scsiio_reply;
  255. const void *sense_data;
  256. u32 sz;
  257. if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
  258. return 1;
  259. if (ioc->ctl_cmds.smid != smid)
  260. return 1;
  261. ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
  262. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  263. if (mpi_reply) {
  264. memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  265. ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
  266. /* get sense data */
  267. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  268. mpi_reply->Function ==
  269. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  270. scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
  271. if (scsiio_reply->SCSIState &
  272. MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  273. sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  274. le32_to_cpu(scsiio_reply->SenseCount));
  275. sense_data = mpt2sas_base_get_sense_buffer(ioc,
  276. smid);
  277. memcpy(ioc->ctl_cmds.sense, sense_data, sz);
  278. }
  279. }
  280. }
  281. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  282. _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
  283. #endif
  284. ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
  285. complete(&ioc->ctl_cmds.done);
  286. return 1;
  287. }
  288. /**
  289. * _ctl_check_event_type - determines when an event needs logging
  290. * @ioc: per adapter object
  291. * @event: firmware event
  292. *
  293. * The bitmask in ioc->event_type[] indicates which events should be
  294. * be saved in the driver event_log. This bitmask is set by application.
  295. *
  296. * Returns 1 when event should be captured, or zero means no match.
  297. */
  298. static int
  299. _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
  300. {
  301. u16 i;
  302. u32 desired_event;
  303. if (event >= 128 || !event || !ioc->event_log)
  304. return 0;
  305. desired_event = (1 << (event % 32));
  306. if (!desired_event)
  307. desired_event = 1;
  308. i = event / 32;
  309. return desired_event & ioc->event_type[i];
  310. }
  311. /**
  312. * mpt2sas_ctl_add_to_event_log - add event
  313. * @ioc: per adapter object
  314. * @mpi_reply: reply message frame
  315. *
  316. * Return nothing.
  317. */
  318. void
  319. mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
  320. Mpi2EventNotificationReply_t *mpi_reply)
  321. {
  322. struct MPT2_IOCTL_EVENTS *event_log;
  323. u16 event;
  324. int i;
  325. u32 sz, event_data_sz;
  326. u8 send_aen = 0;
  327. if (!ioc->event_log)
  328. return;
  329. event = le16_to_cpu(mpi_reply->Event);
  330. if (_ctl_check_event_type(ioc, event)) {
  331. /* insert entry into circular event_log */
  332. i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
  333. event_log = ioc->event_log;
  334. event_log[i].event = event;
  335. event_log[i].context = ioc->event_context++;
  336. event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
  337. sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
  338. memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
  339. memcpy(event_log[i].data, mpi_reply->EventData, sz);
  340. send_aen = 1;
  341. }
  342. /* This aen_event_read_flag flag is set until the
  343. * application has read the event log.
  344. * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
  345. */
  346. if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
  347. (send_aen && !ioc->aen_event_read_flag)) {
  348. ioc->aen_event_read_flag = 1;
  349. wake_up_interruptible(&ctl_poll_wait);
  350. if (async_queue)
  351. kill_fasync(&async_queue, SIGIO, POLL_IN);
  352. }
  353. }
  354. /**
  355. * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
  356. * @ioc: per adapter object
  357. * @msix_index: MSIX table index supplied by the OS
  358. * @reply: reply message frame(lower 32bit addr)
  359. * Context: interrupt.
  360. *
  361. * This function merely adds a new work task into ioc->firmware_event_thread.
  362. * The tasks are worked from _firmware_event_work in user context.
  363. *
  364. * Return 1 meaning mf should be freed from _base_interrupt
  365. * 0 means the mf is freed from this function.
  366. */
  367. u8
  368. mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
  369. u32 reply)
  370. {
  371. Mpi2EventNotificationReply_t *mpi_reply;
  372. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  373. mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
  374. return 1;
  375. }
  376. /**
  377. * _ctl_verify_adapter - validates ioc_number passed from application
  378. * @ioc: per adapter object
  379. * @iocpp: The ioc pointer is returned in this.
  380. *
  381. * Return (-1) means error, else ioc_number.
  382. */
  383. static int
  384. _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
  385. {
  386. struct MPT2SAS_ADAPTER *ioc;
  387. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  388. if (ioc->id != ioc_number)
  389. continue;
  390. *iocpp = ioc;
  391. return ioc_number;
  392. }
  393. *iocpp = NULL;
  394. return -1;
  395. }
  396. /**
  397. * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
  398. * @ioc: per adapter object
  399. * @reset_phase: phase
  400. *
  401. * The handler for doing any required cleanup or initialization.
  402. *
  403. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  404. * MPT2_IOC_DONE_RESET
  405. */
  406. void
  407. mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  408. {
  409. int i;
  410. u8 issue_reset;
  411. switch (reset_phase) {
  412. case MPT2_IOC_PRE_RESET:
  413. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  414. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  415. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  416. if (!(ioc->diag_buffer_status[i] &
  417. MPT2_DIAG_BUFFER_IS_REGISTERED))
  418. continue;
  419. if ((ioc->diag_buffer_status[i] &
  420. MPT2_DIAG_BUFFER_IS_RELEASED))
  421. continue;
  422. _ctl_send_release(ioc, i, &issue_reset);
  423. }
  424. break;
  425. case MPT2_IOC_AFTER_RESET:
  426. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  427. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  428. if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
  429. ioc->ctl_cmds.status |= MPT2_CMD_RESET;
  430. mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
  431. complete(&ioc->ctl_cmds.done);
  432. }
  433. break;
  434. case MPT2_IOC_DONE_RESET:
  435. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  436. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  437. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  438. if (!(ioc->diag_buffer_status[i] &
  439. MPT2_DIAG_BUFFER_IS_REGISTERED))
  440. continue;
  441. if ((ioc->diag_buffer_status[i] &
  442. MPT2_DIAG_BUFFER_IS_RELEASED))
  443. continue;
  444. ioc->diag_buffer_status[i] |=
  445. MPT2_DIAG_BUFFER_IS_DIAG_RESET;
  446. }
  447. break;
  448. }
  449. }
  450. /**
  451. * _ctl_fasync -
  452. * @fd -
  453. * @filep -
  454. * @mode -
  455. *
  456. * Called when application request fasyn callback handler.
  457. */
  458. static int
  459. _ctl_fasync(int fd, struct file *filep, int mode)
  460. {
  461. return fasync_helper(fd, filep, mode, &async_queue);
  462. }
  463. /**
  464. * _ctl_release -
  465. * @inode -
  466. * @filep -
  467. *
  468. * Called when application releases the fasyn callback handler.
  469. */
  470. static int
  471. _ctl_release(struct inode *inode, struct file *filep)
  472. {
  473. return fasync_helper(-1, filep, 0, &async_queue);
  474. }
  475. /**
  476. * _ctl_poll -
  477. * @file -
  478. * @wait -
  479. *
  480. */
  481. static unsigned int
  482. _ctl_poll(struct file *filep, poll_table *wait)
  483. {
  484. struct MPT2SAS_ADAPTER *ioc;
  485. poll_wait(filep, &ctl_poll_wait, wait);
  486. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  487. if (ioc->aen_event_read_flag)
  488. return POLLIN | POLLRDNORM;
  489. }
  490. return 0;
  491. }
  492. /**
  493. * _ctl_set_task_mid - assign an active smid to tm request
  494. * @ioc: per adapter object
  495. * @karg - (struct mpt2_ioctl_command)
  496. * @tm_request - pointer to mf from user space
  497. *
  498. * Returns 0 when an smid if found, else fail.
  499. * during failure, the reply frame is filled.
  500. */
  501. static int
  502. _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
  503. Mpi2SCSITaskManagementRequest_t *tm_request)
  504. {
  505. u8 found = 0;
  506. u16 i;
  507. u16 handle;
  508. struct scsi_cmnd *scmd;
  509. struct MPT2SAS_DEVICE *priv_data;
  510. unsigned long flags;
  511. Mpi2SCSITaskManagementReply_t *tm_reply;
  512. u32 sz;
  513. u32 lun;
  514. char *desc = NULL;
  515. if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  516. desc = "abort_task";
  517. else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
  518. desc = "query_task";
  519. else
  520. return 0;
  521. lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
  522. handle = le16_to_cpu(tm_request->DevHandle);
  523. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  524. for (i = ioc->scsiio_depth; i && !found; i--) {
  525. scmd = ioc->scsi_lookup[i - 1].scmd;
  526. if (scmd == NULL || scmd->device == NULL ||
  527. scmd->device->hostdata == NULL)
  528. continue;
  529. if (lun != scmd->device->lun)
  530. continue;
  531. priv_data = scmd->device->hostdata;
  532. if (priv_data->sas_target == NULL)
  533. continue;
  534. if (priv_data->sas_target->handle != handle)
  535. continue;
  536. tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
  537. found = 1;
  538. }
  539. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  540. if (!found) {
  541. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  542. "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
  543. desc, le16_to_cpu(tm_request->DevHandle), lun));
  544. tm_reply = ioc->ctl_cmds.reply;
  545. tm_reply->DevHandle = tm_request->DevHandle;
  546. tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  547. tm_reply->TaskType = tm_request->TaskType;
  548. tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
  549. tm_reply->VP_ID = tm_request->VP_ID;
  550. tm_reply->VF_ID = tm_request->VF_ID;
  551. sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
  552. if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  553. sz))
  554. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  555. __LINE__, __func__);
  556. return 1;
  557. }
  558. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  559. "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
  560. desc, le16_to_cpu(tm_request->DevHandle), lun,
  561. le16_to_cpu(tm_request->TaskMID)));
  562. return 0;
  563. }
  564. /**
  565. * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
  566. * @ioc: per adapter object
  567. * @karg - (struct mpt2_ioctl_command)
  568. * @mf - pointer to mf in user space
  569. * @state - NON_BLOCKING or BLOCKING
  570. */
  571. static long
  572. _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
  573. struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
  574. {
  575. MPI2RequestHeader_t *mpi_request = NULL, *request;
  576. MPI2DefaultReply_t *mpi_reply;
  577. u32 ioc_state;
  578. u16 ioc_status;
  579. u16 smid;
  580. unsigned long timeout, timeleft;
  581. u8 issue_reset;
  582. u32 sz;
  583. void *psge;
  584. void *data_out = NULL;
  585. dma_addr_t data_out_dma;
  586. size_t data_out_sz = 0;
  587. void *data_in = NULL;
  588. dma_addr_t data_in_dma;
  589. size_t data_in_sz = 0;
  590. u32 sgl_flags;
  591. long ret;
  592. u16 wait_state_count;
  593. issue_reset = 0;
  594. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  595. return -EAGAIN;
  596. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  597. return -ERESTARTSYS;
  598. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  599. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  600. ioc->name, __func__);
  601. ret = -EAGAIN;
  602. goto out;
  603. }
  604. wait_state_count = 0;
  605. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  606. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  607. if (wait_state_count++ == 10) {
  608. printk(MPT2SAS_ERR_FMT
  609. "%s: failed due to ioc not operational\n",
  610. ioc->name, __func__);
  611. ret = -EFAULT;
  612. goto out;
  613. }
  614. ssleep(1);
  615. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  616. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  617. "operational state(count=%d)\n", ioc->name,
  618. __func__, wait_state_count);
  619. }
  620. if (wait_state_count)
  621. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  622. ioc->name, __func__);
  623. mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
  624. if (!mpi_request) {
  625. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
  626. "mpi_request\n", ioc->name, __func__);
  627. ret = -ENOMEM;
  628. goto out;
  629. }
  630. /* Check for overflow and wraparound */
  631. if (karg.data_sge_offset * 4 > ioc->request_sz ||
  632. karg.data_sge_offset > (UINT_MAX / 4)) {
  633. ret = -EINVAL;
  634. goto out;
  635. }
  636. /* copy in request message frame from user */
  637. if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
  638. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
  639. __func__);
  640. ret = -EFAULT;
  641. goto out;
  642. }
  643. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  644. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
  645. if (!smid) {
  646. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  647. ioc->name, __func__);
  648. ret = -EAGAIN;
  649. goto out;
  650. }
  651. } else {
  652. smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
  653. if (!smid) {
  654. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  655. ioc->name, __func__);
  656. ret = -EAGAIN;
  657. goto out;
  658. }
  659. }
  660. ret = 0;
  661. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  662. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  663. request = mpt2sas_base_get_msg_frame(ioc, smid);
  664. memcpy(request, mpi_request, karg.data_sge_offset*4);
  665. ioc->ctl_cmds.smid = smid;
  666. data_out_sz = karg.data_out_size;
  667. data_in_sz = karg.data_in_size;
  668. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  669. mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  670. if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
  671. le16_to_cpu(mpi_request->FunctionDependent1) >
  672. ioc->facts.MaxDevHandle) {
  673. ret = -EINVAL;
  674. mpt2sas_base_free_smid(ioc, smid);
  675. goto out;
  676. }
  677. }
  678. /* obtain dma-able memory for data transfer */
  679. if (data_out_sz) /* WRITE */ {
  680. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
  681. &data_out_dma);
  682. if (!data_out) {
  683. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  684. __LINE__, __func__);
  685. ret = -ENOMEM;
  686. mpt2sas_base_free_smid(ioc, smid);
  687. goto out;
  688. }
  689. if (copy_from_user(data_out, karg.data_out_buf_ptr,
  690. data_out_sz)) {
  691. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  692. __LINE__, __func__);
  693. ret = -EFAULT;
  694. mpt2sas_base_free_smid(ioc, smid);
  695. goto out;
  696. }
  697. }
  698. if (data_in_sz) /* READ */ {
  699. data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
  700. &data_in_dma);
  701. if (!data_in) {
  702. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  703. __LINE__, __func__);
  704. ret = -ENOMEM;
  705. mpt2sas_base_free_smid(ioc, smid);
  706. goto out;
  707. }
  708. }
  709. /* add scatter gather elements */
  710. psge = (void *)request + (karg.data_sge_offset*4);
  711. if (!data_out_sz && !data_in_sz) {
  712. mpt2sas_base_build_zero_len_sge(ioc, psge);
  713. } else if (data_out_sz && data_in_sz) {
  714. /* WRITE sgel first */
  715. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  716. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  717. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  718. ioc->base_add_sg_single(psge, sgl_flags |
  719. data_out_sz, data_out_dma);
  720. /* incr sgel */
  721. psge += ioc->sge_size;
  722. /* READ sgel last */
  723. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  724. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  725. MPI2_SGE_FLAGS_END_OF_LIST);
  726. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  727. ioc->base_add_sg_single(psge, sgl_flags |
  728. data_in_sz, data_in_dma);
  729. } else if (data_out_sz) /* WRITE */ {
  730. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  731. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  732. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  733. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  734. ioc->base_add_sg_single(psge, sgl_flags |
  735. data_out_sz, data_out_dma);
  736. } else if (data_in_sz) /* READ */ {
  737. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  738. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  739. MPI2_SGE_FLAGS_END_OF_LIST);
  740. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  741. ioc->base_add_sg_single(psge, sgl_flags |
  742. data_in_sz, data_in_dma);
  743. }
  744. /* send command to firmware */
  745. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  746. _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
  747. #endif
  748. switch (mpi_request->Function) {
  749. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  750. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  751. {
  752. Mpi2SCSIIORequest_t *scsiio_request =
  753. (Mpi2SCSIIORequest_t *)request;
  754. scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  755. scsiio_request->SenseBufferLowAddress =
  756. mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  757. memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
  758. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
  759. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  760. le16_to_cpu(mpi_request->FunctionDependent1));
  761. else
  762. mpt2sas_base_put_smid_default(ioc, smid);
  763. break;
  764. }
  765. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  766. {
  767. Mpi2SCSITaskManagementRequest_t *tm_request =
  768. (Mpi2SCSITaskManagementRequest_t *)request;
  769. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
  770. "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
  771. le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
  772. if (tm_request->TaskType ==
  773. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
  774. tm_request->TaskType ==
  775. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
  776. if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
  777. mpt2sas_base_free_smid(ioc, smid);
  778. goto out;
  779. }
  780. }
  781. mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
  782. tm_request->DevHandle));
  783. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  784. break;
  785. }
  786. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  787. {
  788. Mpi2SmpPassthroughRequest_t *smp_request =
  789. (Mpi2SmpPassthroughRequest_t *)mpi_request;
  790. u8 *data;
  791. /* ioc determines which port to use */
  792. smp_request->PhysicalPort = 0xFF;
  793. if (smp_request->PassthroughFlags &
  794. MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
  795. data = (u8 *)&smp_request->SGL;
  796. else
  797. data = data_out;
  798. if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
  799. ioc->ioc_link_reset_in_progress = 1;
  800. ioc->ignore_loginfos = 1;
  801. }
  802. mpt2sas_base_put_smid_default(ioc, smid);
  803. break;
  804. }
  805. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  806. {
  807. Mpi2SasIoUnitControlRequest_t *sasiounit_request =
  808. (Mpi2SasIoUnitControlRequest_t *)mpi_request;
  809. if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
  810. || sasiounit_request->Operation ==
  811. MPI2_SAS_OP_PHY_LINK_RESET) {
  812. ioc->ioc_link_reset_in_progress = 1;
  813. ioc->ignore_loginfos = 1;
  814. }
  815. mpt2sas_base_put_smid_default(ioc, smid);
  816. break;
  817. }
  818. default:
  819. mpt2sas_base_put_smid_default(ioc, smid);
  820. break;
  821. }
  822. if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
  823. timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
  824. else
  825. timeout = karg.timeout;
  826. init_completion(&ioc->ctl_cmds.done);
  827. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  828. timeout*HZ);
  829. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  830. Mpi2SCSITaskManagementRequest_t *tm_request =
  831. (Mpi2SCSITaskManagementRequest_t *)mpi_request;
  832. mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
  833. tm_request->DevHandle));
  834. } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
  835. mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
  836. ioc->ioc_link_reset_in_progress) {
  837. ioc->ioc_link_reset_in_progress = 0;
  838. ioc->ignore_loginfos = 0;
  839. }
  840. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  841. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  842. __func__);
  843. _debug_dump_mf(mpi_request, karg.data_sge_offset);
  844. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  845. issue_reset = 1;
  846. goto issue_host_reset;
  847. }
  848. mpi_reply = ioc->ctl_cmds.reply;
  849. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  850. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  851. if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
  852. (ioc->logging_level & MPT_DEBUG_TM)) {
  853. Mpi2SCSITaskManagementReply_t *tm_reply =
  854. (Mpi2SCSITaskManagementReply_t *)mpi_reply;
  855. printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
  856. "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
  857. "TerminationCount(0x%08x)\n", ioc->name,
  858. le16_to_cpu(tm_reply->IOCStatus),
  859. le32_to_cpu(tm_reply->IOCLogInfo),
  860. le32_to_cpu(tm_reply->TerminationCount));
  861. }
  862. #endif
  863. /* copy out xdata to user */
  864. if (data_in_sz) {
  865. if (copy_to_user(karg.data_in_buf_ptr, data_in,
  866. data_in_sz)) {
  867. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  868. __LINE__, __func__);
  869. ret = -ENODATA;
  870. goto out;
  871. }
  872. }
  873. /* copy out reply message frame to user */
  874. if (karg.max_reply_bytes) {
  875. sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
  876. if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  877. sz)) {
  878. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  879. __LINE__, __func__);
  880. ret = -ENODATA;
  881. goto out;
  882. }
  883. }
  884. /* copy out sense to user */
  885. if (karg.max_sense_bytes && (mpi_request->Function ==
  886. MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
  887. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
  888. sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
  889. if (copy_to_user(karg.sense_data_ptr,
  890. ioc->ctl_cmds.sense, sz)) {
  891. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  892. __LINE__, __func__);
  893. ret = -ENODATA;
  894. goto out;
  895. }
  896. }
  897. issue_host_reset:
  898. if (issue_reset) {
  899. ret = -ENODATA;
  900. if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  901. mpi_request->Function ==
  902. MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
  903. printk(MPT2SAS_INFO_FMT "issue target reset: handle "
  904. "= (0x%04x)\n", ioc->name,
  905. le16_to_cpu(mpi_request->FunctionDependent1));
  906. mpt2sas_halt_firmware(ioc);
  907. mpt2sas_scsih_issue_tm(ioc,
  908. le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
  909. 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
  910. 0, TM_MUTEX_ON);
  911. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  912. } else
  913. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  914. FORCE_BIG_HAMMER);
  915. }
  916. out:
  917. /* free memory associated with sg buffers */
  918. if (data_in)
  919. pci_free_consistent(ioc->pdev, data_in_sz, data_in,
  920. data_in_dma);
  921. if (data_out)
  922. pci_free_consistent(ioc->pdev, data_out_sz, data_out,
  923. data_out_dma);
  924. kfree(mpi_request);
  925. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  926. mutex_unlock(&ioc->ctl_cmds.mutex);
  927. return ret;
  928. }
  929. /**
  930. * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
  931. * @arg - user space buffer containing ioctl content
  932. */
  933. static long
  934. _ctl_getiocinfo(void __user *arg)
  935. {
  936. struct mpt2_ioctl_iocinfo karg;
  937. struct MPT2SAS_ADAPTER *ioc;
  938. u8 revision;
  939. if (copy_from_user(&karg, arg, sizeof(karg))) {
  940. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  941. __FILE__, __LINE__, __func__);
  942. return -EFAULT;
  943. }
  944. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  945. return -ENODEV;
  946. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  947. __func__));
  948. memset(&karg, 0 , sizeof(karg));
  949. if (ioc->is_warpdrive)
  950. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
  951. else
  952. karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
  953. if (ioc->pfacts)
  954. karg.port_number = ioc->pfacts[0].PortNumber;
  955. pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
  956. karg.hw_rev = revision;
  957. karg.pci_id = ioc->pdev->device;
  958. karg.subsystem_device = ioc->pdev->subsystem_device;
  959. karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
  960. karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
  961. karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
  962. karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
  963. karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
  964. karg.firmware_version = ioc->facts.FWVersion.Word;
  965. strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
  966. strcat(karg.driver_version, "-");
  967. strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
  968. karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  969. if (copy_to_user(arg, &karg, sizeof(karg))) {
  970. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  971. __FILE__, __LINE__, __func__);
  972. return -EFAULT;
  973. }
  974. return 0;
  975. }
  976. /**
  977. * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
  978. * @arg - user space buffer containing ioctl content
  979. */
  980. static long
  981. _ctl_eventquery(void __user *arg)
  982. {
  983. struct mpt2_ioctl_eventquery karg;
  984. struct MPT2SAS_ADAPTER *ioc;
  985. if (copy_from_user(&karg, arg, sizeof(karg))) {
  986. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  987. __FILE__, __LINE__, __func__);
  988. return -EFAULT;
  989. }
  990. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  991. return -ENODEV;
  992. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  993. __func__));
  994. karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
  995. memcpy(karg.event_types, ioc->event_type,
  996. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  997. if (copy_to_user(arg, &karg, sizeof(karg))) {
  998. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  999. __FILE__, __LINE__, __func__);
  1000. return -EFAULT;
  1001. }
  1002. return 0;
  1003. }
  1004. /**
  1005. * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
  1006. * @arg - user space buffer containing ioctl content
  1007. */
  1008. static long
  1009. _ctl_eventenable(void __user *arg)
  1010. {
  1011. struct mpt2_ioctl_eventenable karg;
  1012. struct MPT2SAS_ADAPTER *ioc;
  1013. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1014. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1015. __FILE__, __LINE__, __func__);
  1016. return -EFAULT;
  1017. }
  1018. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1019. return -ENODEV;
  1020. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  1021. __func__));
  1022. if (ioc->event_log)
  1023. return 0;
  1024. memcpy(ioc->event_type, karg.event_types,
  1025. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
  1026. mpt2sas_base_validate_event_type(ioc, ioc->event_type);
  1027. /* initialize event_log */
  1028. ioc->event_context = 0;
  1029. ioc->aen_event_read_flag = 0;
  1030. ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
  1031. sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
  1032. if (!ioc->event_log) {
  1033. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1034. __FILE__, __LINE__, __func__);
  1035. return -ENOMEM;
  1036. }
  1037. return 0;
  1038. }
  1039. /**
  1040. * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
  1041. * @arg - user space buffer containing ioctl content
  1042. */
  1043. static long
  1044. _ctl_eventreport(void __user *arg)
  1045. {
  1046. struct mpt2_ioctl_eventreport karg;
  1047. struct MPT2SAS_ADAPTER *ioc;
  1048. u32 number_bytes, max_events, max;
  1049. struct mpt2_ioctl_eventreport __user *uarg = arg;
  1050. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1051. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1052. __FILE__, __LINE__, __func__);
  1053. return -EFAULT;
  1054. }
  1055. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1056. return -ENODEV;
  1057. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  1058. __func__));
  1059. number_bytes = karg.hdr.max_data_size -
  1060. sizeof(struct mpt2_ioctl_header);
  1061. max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
  1062. max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
  1063. /* If fewer than 1 event is requested, there must have
  1064. * been some type of error.
  1065. */
  1066. if (!max || !ioc->event_log)
  1067. return -ENODATA;
  1068. number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
  1069. if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
  1070. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1071. __FILE__, __LINE__, __func__);
  1072. return -EFAULT;
  1073. }
  1074. /* reset flag so SIGIO can restart */
  1075. ioc->aen_event_read_flag = 0;
  1076. return 0;
  1077. }
  1078. /**
  1079. * _ctl_do_reset - main handler for MPT2HARDRESET opcode
  1080. * @arg - user space buffer containing ioctl content
  1081. */
  1082. static long
  1083. _ctl_do_reset(void __user *arg)
  1084. {
  1085. struct mpt2_ioctl_diag_reset karg;
  1086. struct MPT2SAS_ADAPTER *ioc;
  1087. int retval;
  1088. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1089. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1090. __FILE__, __LINE__, __func__);
  1091. return -EFAULT;
  1092. }
  1093. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1094. return -ENODEV;
  1095. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  1096. __func__));
  1097. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1098. FORCE_BIG_HAMMER);
  1099. printk(MPT2SAS_INFO_FMT "host reset: %s\n",
  1100. ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
  1101. return 0;
  1102. }
  1103. /**
  1104. * _ctl_btdh_search_sas_device - searching for sas device
  1105. * @ioc: per adapter object
  1106. * @btdh: btdh ioctl payload
  1107. */
  1108. static int
  1109. _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
  1110. struct mpt2_ioctl_btdh_mapping *btdh)
  1111. {
  1112. struct _sas_device *sas_device;
  1113. unsigned long flags;
  1114. int rc = 0;
  1115. if (list_empty(&ioc->sas_device_list))
  1116. return rc;
  1117. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1118. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  1119. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1120. btdh->handle == sas_device->handle) {
  1121. btdh->bus = sas_device->channel;
  1122. btdh->id = sas_device->id;
  1123. rc = 1;
  1124. goto out;
  1125. } else if (btdh->bus == sas_device->channel && btdh->id ==
  1126. sas_device->id && btdh->handle == 0xFFFF) {
  1127. btdh->handle = sas_device->handle;
  1128. rc = 1;
  1129. goto out;
  1130. }
  1131. }
  1132. out:
  1133. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1134. return rc;
  1135. }
  1136. /**
  1137. * _ctl_btdh_search_raid_device - searching for raid device
  1138. * @ioc: per adapter object
  1139. * @btdh: btdh ioctl payload
  1140. */
  1141. static int
  1142. _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
  1143. struct mpt2_ioctl_btdh_mapping *btdh)
  1144. {
  1145. struct _raid_device *raid_device;
  1146. unsigned long flags;
  1147. int rc = 0;
  1148. if (list_empty(&ioc->raid_device_list))
  1149. return rc;
  1150. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1151. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  1152. if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
  1153. btdh->handle == raid_device->handle) {
  1154. btdh->bus = raid_device->channel;
  1155. btdh->id = raid_device->id;
  1156. rc = 1;
  1157. goto out;
  1158. } else if (btdh->bus == raid_device->channel && btdh->id ==
  1159. raid_device->id && btdh->handle == 0xFFFF) {
  1160. btdh->handle = raid_device->handle;
  1161. rc = 1;
  1162. goto out;
  1163. }
  1164. }
  1165. out:
  1166. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1167. return rc;
  1168. }
  1169. /**
  1170. * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
  1171. * @arg - user space buffer containing ioctl content
  1172. */
  1173. static long
  1174. _ctl_btdh_mapping(void __user *arg)
  1175. {
  1176. struct mpt2_ioctl_btdh_mapping karg;
  1177. struct MPT2SAS_ADAPTER *ioc;
  1178. int rc;
  1179. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1180. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1181. __FILE__, __LINE__, __func__);
  1182. return -EFAULT;
  1183. }
  1184. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1185. return -ENODEV;
  1186. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1187. __func__));
  1188. rc = _ctl_btdh_search_sas_device(ioc, &karg);
  1189. if (!rc)
  1190. _ctl_btdh_search_raid_device(ioc, &karg);
  1191. if (copy_to_user(arg, &karg, sizeof(karg))) {
  1192. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1193. __FILE__, __LINE__, __func__);
  1194. return -EFAULT;
  1195. }
  1196. return 0;
  1197. }
  1198. /**
  1199. * _ctl_diag_capability - return diag buffer capability
  1200. * @ioc: per adapter object
  1201. * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
  1202. *
  1203. * returns 1 when diag buffer support is enabled in firmware
  1204. */
  1205. static u8
  1206. _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
  1207. {
  1208. u8 rc = 0;
  1209. switch (buffer_type) {
  1210. case MPI2_DIAG_BUF_TYPE_TRACE:
  1211. if (ioc->facts.IOCCapabilities &
  1212. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
  1213. rc = 1;
  1214. break;
  1215. case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
  1216. if (ioc->facts.IOCCapabilities &
  1217. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
  1218. rc = 1;
  1219. break;
  1220. case MPI2_DIAG_BUF_TYPE_EXTENDED:
  1221. if (ioc->facts.IOCCapabilities &
  1222. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
  1223. rc = 1;
  1224. }
  1225. return rc;
  1226. }
  1227. /**
  1228. * _ctl_diag_register_2 - wrapper for registering diag buffer support
  1229. * @ioc: per adapter object
  1230. * @diag_register: the diag_register struct passed in from user space
  1231. *
  1232. */
  1233. static long
  1234. _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
  1235. struct mpt2_diag_register *diag_register)
  1236. {
  1237. int rc, i;
  1238. void *request_data = NULL;
  1239. dma_addr_t request_data_dma;
  1240. u32 request_data_sz = 0;
  1241. Mpi2DiagBufferPostRequest_t *mpi_request;
  1242. Mpi2DiagBufferPostReply_t *mpi_reply;
  1243. u8 buffer_type;
  1244. unsigned long timeleft;
  1245. u16 smid;
  1246. u16 ioc_status;
  1247. u8 issue_reset = 0;
  1248. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1249. __func__));
  1250. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  1251. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  1252. ioc->name, __func__);
  1253. rc = -EAGAIN;
  1254. goto out;
  1255. }
  1256. buffer_type = diag_register->buffer_type;
  1257. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1258. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1259. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1260. return -EPERM;
  1261. }
  1262. if (ioc->diag_buffer_status[buffer_type] &
  1263. MPT2_DIAG_BUFFER_IS_REGISTERED) {
  1264. printk(MPT2SAS_ERR_FMT "%s: already has a registered "
  1265. "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
  1266. buffer_type);
  1267. return -EINVAL;
  1268. }
  1269. if (diag_register->requested_buffer_size % 4) {
  1270. printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
  1271. "is not 4 byte aligned\n", ioc->name, __func__);
  1272. return -EINVAL;
  1273. }
  1274. smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1275. if (!smid) {
  1276. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1277. ioc->name, __func__);
  1278. rc = -EAGAIN;
  1279. goto out;
  1280. }
  1281. rc = 0;
  1282. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  1283. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1284. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1285. ioc->ctl_cmds.smid = smid;
  1286. request_data = ioc->diag_buffer[buffer_type];
  1287. request_data_sz = diag_register->requested_buffer_size;
  1288. ioc->unique_id[buffer_type] = diag_register->unique_id;
  1289. ioc->diag_buffer_status[buffer_type] = 0;
  1290. memcpy(ioc->product_specific[buffer_type],
  1291. diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
  1292. ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
  1293. if (request_data) {
  1294. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1295. if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
  1296. pci_free_consistent(ioc->pdev,
  1297. ioc->diag_buffer_sz[buffer_type],
  1298. request_data, request_data_dma);
  1299. request_data = NULL;
  1300. }
  1301. }
  1302. if (request_data == NULL) {
  1303. ioc->diag_buffer_sz[buffer_type] = 0;
  1304. ioc->diag_buffer_dma[buffer_type] = 0;
  1305. request_data = pci_alloc_consistent(
  1306. ioc->pdev, request_data_sz, &request_data_dma);
  1307. if (request_data == NULL) {
  1308. printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
  1309. " for diag buffers, requested size(%d)\n",
  1310. ioc->name, __func__, request_data_sz);
  1311. mpt2sas_base_free_smid(ioc, smid);
  1312. return -ENOMEM;
  1313. }
  1314. ioc->diag_buffer[buffer_type] = request_data;
  1315. ioc->diag_buffer_sz[buffer_type] = request_data_sz;
  1316. ioc->diag_buffer_dma[buffer_type] = request_data_dma;
  1317. }
  1318. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1319. mpi_request->BufferType = diag_register->buffer_type;
  1320. mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
  1321. mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
  1322. mpi_request->BufferLength = cpu_to_le32(request_data_sz);
  1323. mpi_request->VF_ID = 0; /* TODO */
  1324. mpi_request->VP_ID = 0;
  1325. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
  1326. "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
  1327. (unsigned long long)request_data_dma,
  1328. le32_to_cpu(mpi_request->BufferLength)));
  1329. for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
  1330. mpi_request->ProductSpecific[i] =
  1331. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  1332. mpt2sas_base_put_smid_default(ioc, smid);
  1333. init_completion(&ioc->ctl_cmds.done);
  1334. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1335. MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
  1336. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  1337. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  1338. __func__);
  1339. _debug_dump_mf(mpi_request,
  1340. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  1341. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  1342. issue_reset = 1;
  1343. goto issue_host_reset;
  1344. }
  1345. /* process the completed Reply Message Frame */
  1346. if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
  1347. printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
  1348. ioc->name, __func__);
  1349. rc = -EFAULT;
  1350. goto out;
  1351. }
  1352. mpi_reply = ioc->ctl_cmds.reply;
  1353. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1354. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1355. ioc->diag_buffer_status[buffer_type] |=
  1356. MPT2_DIAG_BUFFER_IS_REGISTERED;
  1357. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
  1358. ioc->name, __func__));
  1359. } else {
  1360. printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
  1361. "log_info(0x%08x)\n", ioc->name, __func__,
  1362. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1363. rc = -EFAULT;
  1364. }
  1365. issue_host_reset:
  1366. if (issue_reset)
  1367. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1368. FORCE_BIG_HAMMER);
  1369. out:
  1370. if (rc && request_data)
  1371. pci_free_consistent(ioc->pdev, request_data_sz,
  1372. request_data, request_data_dma);
  1373. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  1374. return rc;
  1375. }
  1376. /**
  1377. * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
  1378. * @ioc: per adapter object
  1379. * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
  1380. *
  1381. * This is called when command line option diag_buffer_enable is enabled
  1382. * at driver load time.
  1383. */
  1384. void
  1385. mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
  1386. {
  1387. struct mpt2_diag_register diag_register;
  1388. memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
  1389. if (bits_to_register & 1) {
  1390. printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
  1391. ioc->name);
  1392. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  1393. /* register for 1MB buffers */
  1394. diag_register.requested_buffer_size = (1024 * 1024);
  1395. diag_register.unique_id = 0x7075900;
  1396. _ctl_diag_register_2(ioc, &diag_register);
  1397. }
  1398. if (bits_to_register & 2) {
  1399. printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
  1400. ioc->name);
  1401. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
  1402. /* register for 2MB buffers */
  1403. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1404. diag_register.unique_id = 0x7075901;
  1405. _ctl_diag_register_2(ioc, &diag_register);
  1406. }
  1407. if (bits_to_register & 4) {
  1408. printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
  1409. ioc->name);
  1410. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
  1411. /* register for 2MB buffers */
  1412. diag_register.requested_buffer_size = 2 * (1024 * 1024);
  1413. diag_register.unique_id = 0x7075901;
  1414. _ctl_diag_register_2(ioc, &diag_register);
  1415. }
  1416. }
  1417. /**
  1418. * _ctl_diag_register - application register with driver
  1419. * @arg - user space buffer containing ioctl content
  1420. * @state - NON_BLOCKING or BLOCKING
  1421. *
  1422. * This will allow the driver to setup any required buffers that will be
  1423. * needed by firmware to communicate with the driver.
  1424. */
  1425. static long
  1426. _ctl_diag_register(void __user *arg, enum block_state state)
  1427. {
  1428. struct mpt2_diag_register karg;
  1429. struct MPT2SAS_ADAPTER *ioc;
  1430. long rc;
  1431. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1432. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1433. __FILE__, __LINE__, __func__);
  1434. return -EFAULT;
  1435. }
  1436. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1437. return -ENODEV;
  1438. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  1439. return -EAGAIN;
  1440. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  1441. return -ERESTARTSYS;
  1442. rc = _ctl_diag_register_2(ioc, &karg);
  1443. mutex_unlock(&ioc->ctl_cmds.mutex);
  1444. return rc;
  1445. }
  1446. /**
  1447. * _ctl_diag_unregister - application unregister with driver
  1448. * @arg - user space buffer containing ioctl content
  1449. *
  1450. * This will allow the driver to cleanup any memory allocated for diag
  1451. * messages and to free up any resources.
  1452. */
  1453. static long
  1454. _ctl_diag_unregister(void __user *arg)
  1455. {
  1456. struct mpt2_diag_unregister karg;
  1457. struct MPT2SAS_ADAPTER *ioc;
  1458. void *request_data;
  1459. dma_addr_t request_data_dma;
  1460. u32 request_data_sz;
  1461. u8 buffer_type;
  1462. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1463. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1464. __FILE__, __LINE__, __func__);
  1465. return -EFAULT;
  1466. }
  1467. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1468. return -ENODEV;
  1469. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1470. __func__));
  1471. buffer_type = karg.unique_id & 0x000000ff;
  1472. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1473. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1474. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1475. return -EPERM;
  1476. }
  1477. if ((ioc->diag_buffer_status[buffer_type] &
  1478. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1479. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
  1480. "registered\n", ioc->name, __func__, buffer_type);
  1481. return -EINVAL;
  1482. }
  1483. if ((ioc->diag_buffer_status[buffer_type] &
  1484. MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
  1485. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
  1486. "released\n", ioc->name, __func__, buffer_type);
  1487. return -EINVAL;
  1488. }
  1489. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1490. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1491. "registered\n", ioc->name, __func__, karg.unique_id);
  1492. return -EINVAL;
  1493. }
  1494. request_data = ioc->diag_buffer[buffer_type];
  1495. if (!request_data) {
  1496. printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
  1497. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1498. return -ENOMEM;
  1499. }
  1500. request_data_sz = ioc->diag_buffer_sz[buffer_type];
  1501. request_data_dma = ioc->diag_buffer_dma[buffer_type];
  1502. pci_free_consistent(ioc->pdev, request_data_sz,
  1503. request_data, request_data_dma);
  1504. ioc->diag_buffer[buffer_type] = NULL;
  1505. ioc->diag_buffer_status[buffer_type] = 0;
  1506. return 0;
  1507. }
  1508. /**
  1509. * _ctl_diag_query - query relevant info associated with diag buffers
  1510. * @arg - user space buffer containing ioctl content
  1511. *
  1512. * The application will send only buffer_type and unique_id. Driver will
  1513. * inspect unique_id first, if valid, fill in all the info. If unique_id is
  1514. * 0x00, the driver will return info specified by Buffer Type.
  1515. */
  1516. static long
  1517. _ctl_diag_query(void __user *arg)
  1518. {
  1519. struct mpt2_diag_query karg;
  1520. struct MPT2SAS_ADAPTER *ioc;
  1521. void *request_data;
  1522. int i;
  1523. u8 buffer_type;
  1524. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1525. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1526. __FILE__, __LINE__, __func__);
  1527. return -EFAULT;
  1528. }
  1529. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1530. return -ENODEV;
  1531. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1532. __func__));
  1533. karg.application_flags = 0;
  1534. buffer_type = karg.buffer_type;
  1535. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1536. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1537. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1538. return -EPERM;
  1539. }
  1540. if ((ioc->diag_buffer_status[buffer_type] &
  1541. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1542. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
  1543. "registered\n", ioc->name, __func__, buffer_type);
  1544. return -EINVAL;
  1545. }
  1546. if (karg.unique_id & 0xffffff00) {
  1547. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1548. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1549. "registered\n", ioc->name, __func__,
  1550. karg.unique_id);
  1551. return -EINVAL;
  1552. }
  1553. }
  1554. request_data = ioc->diag_buffer[buffer_type];
  1555. if (!request_data) {
  1556. printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
  1557. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1558. return -ENOMEM;
  1559. }
  1560. if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
  1561. karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
  1562. MPT2_APP_FLAGS_BUFFER_VALID);
  1563. else
  1564. karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
  1565. MPT2_APP_FLAGS_BUFFER_VALID |
  1566. MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
  1567. for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
  1568. karg.product_specific[i] =
  1569. ioc->product_specific[buffer_type][i];
  1570. karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
  1571. karg.driver_added_buffer_size = 0;
  1572. karg.unique_id = ioc->unique_id[buffer_type];
  1573. karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
  1574. if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
  1575. printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
  1576. "data @ %p\n", ioc->name, __func__, arg);
  1577. return -EFAULT;
  1578. }
  1579. return 0;
  1580. }
  1581. /**
  1582. * _ctl_send_release - Diag Release Message
  1583. * @ioc: per adapter object
  1584. * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
  1585. * @issue_reset - specifies whether host reset is required.
  1586. *
  1587. */
  1588. static int
  1589. _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
  1590. {
  1591. Mpi2DiagReleaseRequest_t *mpi_request;
  1592. Mpi2DiagReleaseReply_t *mpi_reply;
  1593. u16 smid;
  1594. u16 ioc_status;
  1595. u32 ioc_state;
  1596. int rc;
  1597. unsigned long timeleft;
  1598. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1599. __func__));
  1600. rc = 0;
  1601. *issue_reset = 0;
  1602. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1603. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1604. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  1605. "skipping due to FAULT state\n", ioc->name,
  1606. __func__));
  1607. rc = -EAGAIN;
  1608. goto out;
  1609. }
  1610. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  1611. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  1612. ioc->name, __func__);
  1613. rc = -EAGAIN;
  1614. goto out;
  1615. }
  1616. smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1617. if (!smid) {
  1618. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1619. ioc->name, __func__);
  1620. rc = -EAGAIN;
  1621. goto out;
  1622. }
  1623. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  1624. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1625. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1626. ioc->ctl_cmds.smid = smid;
  1627. mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
  1628. mpi_request->BufferType = buffer_type;
  1629. mpi_request->VF_ID = 0; /* TODO */
  1630. mpi_request->VP_ID = 0;
  1631. mpt2sas_base_put_smid_default(ioc, smid);
  1632. init_completion(&ioc->ctl_cmds.done);
  1633. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1634. MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
  1635. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  1636. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  1637. __func__);
  1638. _debug_dump_mf(mpi_request,
  1639. sizeof(Mpi2DiagReleaseRequest_t)/4);
  1640. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  1641. *issue_reset = 1;
  1642. rc = -EFAULT;
  1643. goto out;
  1644. }
  1645. /* process the completed Reply Message Frame */
  1646. if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
  1647. printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
  1648. ioc->name, __func__);
  1649. rc = -EFAULT;
  1650. goto out;
  1651. }
  1652. mpi_reply = ioc->ctl_cmds.reply;
  1653. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1654. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1655. ioc->diag_buffer_status[buffer_type] |=
  1656. MPT2_DIAG_BUFFER_IS_RELEASED;
  1657. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
  1658. ioc->name, __func__));
  1659. } else {
  1660. printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
  1661. "log_info(0x%08x)\n", ioc->name, __func__,
  1662. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1663. rc = -EFAULT;
  1664. }
  1665. out:
  1666. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  1667. return rc;
  1668. }
  1669. /**
  1670. * _ctl_diag_release - request to send Diag Release Message to firmware
  1671. * @arg - user space buffer containing ioctl content
  1672. * @state - NON_BLOCKING or BLOCKING
  1673. *
  1674. * This allows ownership of the specified buffer to returned to the driver,
  1675. * allowing an application to read the buffer without fear that firmware is
  1676. * overwritting information in the buffer.
  1677. */
  1678. static long
  1679. _ctl_diag_release(void __user *arg, enum block_state state)
  1680. {
  1681. struct mpt2_diag_release karg;
  1682. struct MPT2SAS_ADAPTER *ioc;
  1683. void *request_data;
  1684. int rc;
  1685. u8 buffer_type;
  1686. u8 issue_reset = 0;
  1687. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1688. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1689. __FILE__, __LINE__, __func__);
  1690. return -EFAULT;
  1691. }
  1692. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1693. return -ENODEV;
  1694. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1695. __func__));
  1696. buffer_type = karg.unique_id & 0x000000ff;
  1697. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1698. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1699. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1700. return -EPERM;
  1701. }
  1702. if ((ioc->diag_buffer_status[buffer_type] &
  1703. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  1704. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
  1705. "registered\n", ioc->name, __func__, buffer_type);
  1706. return -EINVAL;
  1707. }
  1708. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1709. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1710. "registered\n", ioc->name, __func__, karg.unique_id);
  1711. return -EINVAL;
  1712. }
  1713. if (ioc->diag_buffer_status[buffer_type] &
  1714. MPT2_DIAG_BUFFER_IS_RELEASED) {
  1715. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
  1716. "is already released\n", ioc->name, __func__,
  1717. buffer_type);
  1718. return 0;
  1719. }
  1720. request_data = ioc->diag_buffer[buffer_type];
  1721. if (!request_data) {
  1722. printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
  1723. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1724. return -ENOMEM;
  1725. }
  1726. /* buffers were released by due to host reset */
  1727. if ((ioc->diag_buffer_status[buffer_type] &
  1728. MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
  1729. ioc->diag_buffer_status[buffer_type] |=
  1730. MPT2_DIAG_BUFFER_IS_RELEASED;
  1731. ioc->diag_buffer_status[buffer_type] &=
  1732. ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
  1733. printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
  1734. "was released due to host reset\n", ioc->name, __func__,
  1735. buffer_type);
  1736. return 0;
  1737. }
  1738. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  1739. return -EAGAIN;
  1740. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  1741. return -ERESTARTSYS;
  1742. rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
  1743. if (issue_reset)
  1744. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1745. FORCE_BIG_HAMMER);
  1746. mutex_unlock(&ioc->ctl_cmds.mutex);
  1747. return rc;
  1748. }
  1749. /**
  1750. * _ctl_diag_read_buffer - request for copy of the diag buffer
  1751. * @arg - user space buffer containing ioctl content
  1752. * @state - NON_BLOCKING or BLOCKING
  1753. */
  1754. static long
  1755. _ctl_diag_read_buffer(void __user *arg, enum block_state state)
  1756. {
  1757. struct mpt2_diag_read_buffer karg;
  1758. struct mpt2_diag_read_buffer __user *uarg = arg;
  1759. struct MPT2SAS_ADAPTER *ioc;
  1760. void *request_data, *diag_data;
  1761. Mpi2DiagBufferPostRequest_t *mpi_request;
  1762. Mpi2DiagBufferPostReply_t *mpi_reply;
  1763. int rc, i;
  1764. u8 buffer_type;
  1765. unsigned long timeleft, request_size, copy_size;
  1766. u16 smid;
  1767. u16 ioc_status;
  1768. u8 issue_reset = 0;
  1769. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1770. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1771. __FILE__, __LINE__, __func__);
  1772. return -EFAULT;
  1773. }
  1774. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
  1775. return -ENODEV;
  1776. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1777. __func__));
  1778. buffer_type = karg.unique_id & 0x000000ff;
  1779. if (!_ctl_diag_capability(ioc, buffer_type)) {
  1780. printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
  1781. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1782. return -EPERM;
  1783. }
  1784. if (karg.unique_id != ioc->unique_id[buffer_type]) {
  1785. printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
  1786. "registered\n", ioc->name, __func__, karg.unique_id);
  1787. return -EINVAL;
  1788. }
  1789. request_data = ioc->diag_buffer[buffer_type];
  1790. if (!request_data) {
  1791. printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
  1792. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
  1793. return -ENOMEM;
  1794. }
  1795. request_size = ioc->diag_buffer_sz[buffer_type];
  1796. if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
  1797. printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
  1798. "or bytes_to_read are not 4 byte aligned\n", ioc->name,
  1799. __func__);
  1800. return -EINVAL;
  1801. }
  1802. if (karg.starting_offset > request_size)
  1803. return -EINVAL;
  1804. diag_data = (void *)(request_data + karg.starting_offset);
  1805. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
  1806. "offset(%d), sz(%d)\n", ioc->name, __func__,
  1807. diag_data, karg.starting_offset, karg.bytes_to_read));
  1808. /* Truncate data on requests that are too large */
  1809. if ((diag_data + karg.bytes_to_read < diag_data) ||
  1810. (diag_data + karg.bytes_to_read > request_data + request_size))
  1811. copy_size = request_size - karg.starting_offset;
  1812. else
  1813. copy_size = karg.bytes_to_read;
  1814. if (copy_to_user((void __user *)uarg->diagnostic_data,
  1815. diag_data, copy_size)) {
  1816. printk(MPT2SAS_ERR_FMT "%s: Unable to write "
  1817. "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
  1818. __func__, diag_data);
  1819. return -EFAULT;
  1820. }
  1821. if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
  1822. return 0;
  1823. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
  1824. "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
  1825. if ((ioc->diag_buffer_status[buffer_type] &
  1826. MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
  1827. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  1828. "buffer_type(0x%02x) is still registered\n", ioc->name,
  1829. __func__, buffer_type));
  1830. return 0;
  1831. }
  1832. /* Get a free request frame and save the message context.
  1833. */
  1834. if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
  1835. return -EAGAIN;
  1836. else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
  1837. return -ERESTARTSYS;
  1838. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  1839. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  1840. ioc->name, __func__);
  1841. rc = -EAGAIN;
  1842. goto out;
  1843. }
  1844. smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
  1845. if (!smid) {
  1846. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1847. ioc->name, __func__);
  1848. rc = -EAGAIN;
  1849. goto out;
  1850. }
  1851. rc = 0;
  1852. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  1853. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  1854. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1855. ioc->ctl_cmds.smid = smid;
  1856. mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
  1857. mpi_request->BufferType = buffer_type;
  1858. mpi_request->BufferLength =
  1859. cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
  1860. mpi_request->BufferAddress =
  1861. cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
  1862. for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
  1863. mpi_request->ProductSpecific[i] =
  1864. cpu_to_le32(ioc->product_specific[buffer_type][i]);
  1865. mpi_request->VF_ID = 0; /* TODO */
  1866. mpi_request->VP_ID = 0;
  1867. mpt2sas_base_put_smid_default(ioc, smid);
  1868. init_completion(&ioc->ctl_cmds.done);
  1869. timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
  1870. MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
  1871. if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
  1872. printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
  1873. __func__);
  1874. _debug_dump_mf(mpi_request,
  1875. sizeof(Mpi2DiagBufferPostRequest_t)/4);
  1876. if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
  1877. issue_reset = 1;
  1878. goto issue_host_reset;
  1879. }
  1880. /* process the completed Reply Message Frame */
  1881. if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
  1882. printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
  1883. ioc->name, __func__);
  1884. rc = -EFAULT;
  1885. goto out;
  1886. }
  1887. mpi_reply = ioc->ctl_cmds.reply;
  1888. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  1889. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  1890. ioc->diag_buffer_status[buffer_type] |=
  1891. MPT2_DIAG_BUFFER_IS_REGISTERED;
  1892. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
  1893. ioc->name, __func__));
  1894. } else {
  1895. printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
  1896. "log_info(0x%08x)\n", ioc->name, __func__,
  1897. ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
  1898. rc = -EFAULT;
  1899. }
  1900. issue_host_reset:
  1901. if (issue_reset)
  1902. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1903. FORCE_BIG_HAMMER);
  1904. out:
  1905. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  1906. mutex_unlock(&ioc->ctl_cmds.mutex);
  1907. return rc;
  1908. }
  1909. /**
  1910. * _ctl_ioctl_main - main ioctl entry point
  1911. * @file - (struct file)
  1912. * @cmd - ioctl opcode
  1913. * @arg -
  1914. */
  1915. static long
  1916. _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
  1917. {
  1918. enum block_state state;
  1919. long ret = -EINVAL;
  1920. state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
  1921. BLOCKING;
  1922. switch (cmd) {
  1923. case MPT2IOCINFO:
  1924. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
  1925. ret = _ctl_getiocinfo(arg);
  1926. break;
  1927. case MPT2COMMAND:
  1928. {
  1929. struct mpt2_ioctl_command karg;
  1930. struct mpt2_ioctl_command __user *uarg;
  1931. struct MPT2SAS_ADAPTER *ioc;
  1932. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1933. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1934. __FILE__, __LINE__, __func__);
  1935. return -EFAULT;
  1936. }
  1937. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
  1938. !ioc)
  1939. return -ENODEV;
  1940. if (ioc->shost_recovery || ioc->pci_error_recovery)
  1941. return -EAGAIN;
  1942. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
  1943. uarg = arg;
  1944. ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
  1945. }
  1946. break;
  1947. }
  1948. case MPT2EVENTQUERY:
  1949. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
  1950. ret = _ctl_eventquery(arg);
  1951. break;
  1952. case MPT2EVENTENABLE:
  1953. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
  1954. ret = _ctl_eventenable(arg);
  1955. break;
  1956. case MPT2EVENTREPORT:
  1957. ret = _ctl_eventreport(arg);
  1958. break;
  1959. case MPT2HARDRESET:
  1960. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
  1961. ret = _ctl_do_reset(arg);
  1962. break;
  1963. case MPT2BTDHMAPPING:
  1964. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
  1965. ret = _ctl_btdh_mapping(arg);
  1966. break;
  1967. case MPT2DIAGREGISTER:
  1968. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
  1969. ret = _ctl_diag_register(arg, state);
  1970. break;
  1971. case MPT2DIAGUNREGISTER:
  1972. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
  1973. ret = _ctl_diag_unregister(arg);
  1974. break;
  1975. case MPT2DIAGQUERY:
  1976. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
  1977. ret = _ctl_diag_query(arg);
  1978. break;
  1979. case MPT2DIAGRELEASE:
  1980. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
  1981. ret = _ctl_diag_release(arg, state);
  1982. break;
  1983. case MPT2DIAGREADBUFFER:
  1984. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
  1985. ret = _ctl_diag_read_buffer(arg, state);
  1986. break;
  1987. default:
  1988. {
  1989. struct mpt2_ioctl_command karg;
  1990. struct MPT2SAS_ADAPTER *ioc;
  1991. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1992. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1993. __FILE__, __LINE__, __func__);
  1994. return -EFAULT;
  1995. }
  1996. if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
  1997. !ioc)
  1998. return -ENODEV;
  1999. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
  2000. "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
  2001. break;
  2002. }
  2003. }
  2004. return ret;
  2005. }
  2006. /**
  2007. * _ctl_ioctl - main ioctl entry point (unlocked)
  2008. * @file - (struct file)
  2009. * @cmd - ioctl opcode
  2010. * @arg -
  2011. */
  2012. static long
  2013. _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2014. {
  2015. long ret;
  2016. mutex_lock(&_ctl_mutex);
  2017. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
  2018. mutex_unlock(&_ctl_mutex);
  2019. return ret;
  2020. }
  2021. #ifdef CONFIG_COMPAT
  2022. /**
  2023. * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
  2024. * @file - (struct file)
  2025. * @cmd - ioctl opcode
  2026. * @arg - (struct mpt2_ioctl_command32)
  2027. *
  2028. * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
  2029. */
  2030. static long
  2031. _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
  2032. {
  2033. struct mpt2_ioctl_command32 karg32;
  2034. struct mpt2_ioctl_command32 __user *uarg;
  2035. struct mpt2_ioctl_command karg;
  2036. struct MPT2SAS_ADAPTER *ioc;
  2037. enum block_state state;
  2038. if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
  2039. return -EINVAL;
  2040. uarg = (struct mpt2_ioctl_command32 __user *) arg;
  2041. if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
  2042. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  2043. __FILE__, __LINE__, __func__);
  2044. return -EFAULT;
  2045. }
  2046. if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
  2047. return -ENODEV;
  2048. if (ioc->shost_recovery || ioc->pci_error_recovery)
  2049. return -EAGAIN;
  2050. memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
  2051. karg.hdr.ioc_number = karg32.hdr.ioc_number;
  2052. karg.hdr.port_number = karg32.hdr.port_number;
  2053. karg.hdr.max_data_size = karg32.hdr.max_data_size;
  2054. karg.timeout = karg32.timeout;
  2055. karg.max_reply_bytes = karg32.max_reply_bytes;
  2056. karg.data_in_size = karg32.data_in_size;
  2057. karg.data_out_size = karg32.data_out_size;
  2058. karg.max_sense_bytes = karg32.max_sense_bytes;
  2059. karg.data_sge_offset = karg32.data_sge_offset;
  2060. karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
  2061. karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
  2062. karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
  2063. karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
  2064. state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
  2065. return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
  2066. }
  2067. /**
  2068. * _ctl_ioctl_compat - main ioctl entry point (compat)
  2069. * @file -
  2070. * @cmd -
  2071. * @arg -
  2072. *
  2073. * This routine handles 32 bit applications in 64bit os.
  2074. */
  2075. static long
  2076. _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2077. {
  2078. long ret;
  2079. mutex_lock(&_ctl_mutex);
  2080. if (cmd == MPT2COMMAND32)
  2081. ret = _ctl_compat_mpt_command(file, cmd, arg);
  2082. else
  2083. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
  2084. mutex_unlock(&_ctl_mutex);
  2085. return ret;
  2086. }
  2087. #endif
  2088. /* scsi host attributes */
  2089. /**
  2090. * _ctl_version_fw_show - firmware version
  2091. * @cdev - pointer to embedded class device
  2092. * @buf - the buffer returned
  2093. *
  2094. * A sysfs 'read-only' shost attribute.
  2095. */
  2096. static ssize_t
  2097. _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
  2098. char *buf)
  2099. {
  2100. struct Scsi_Host *shost = class_to_shost(cdev);
  2101. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2102. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2103. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2104. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2105. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2106. ioc->facts.FWVersion.Word & 0x000000FF);
  2107. }
  2108. static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
  2109. /**
  2110. * _ctl_version_bios_show - bios version
  2111. * @cdev - pointer to embedded class device
  2112. * @buf - the buffer returned
  2113. *
  2114. * A sysfs 'read-only' shost attribute.
  2115. */
  2116. static ssize_t
  2117. _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
  2118. char *buf)
  2119. {
  2120. struct Scsi_Host *shost = class_to_shost(cdev);
  2121. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2122. u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  2123. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2124. (version & 0xFF000000) >> 24,
  2125. (version & 0x00FF0000) >> 16,
  2126. (version & 0x0000FF00) >> 8,
  2127. version & 0x000000FF);
  2128. }
  2129. static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
  2130. /**
  2131. * _ctl_version_mpi_show - MPI (message passing interface) version
  2132. * @cdev - pointer to embedded class device
  2133. * @buf - the buffer returned
  2134. *
  2135. * A sysfs 'read-only' shost attribute.
  2136. */
  2137. static ssize_t
  2138. _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
  2139. char *buf)
  2140. {
  2141. struct Scsi_Host *shost = class_to_shost(cdev);
  2142. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2143. return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
  2144. ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
  2145. }
  2146. static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
  2147. /**
  2148. * _ctl_version_product_show - product name
  2149. * @cdev - pointer to embedded class device
  2150. * @buf - the buffer returned
  2151. *
  2152. * A sysfs 'read-only' shost attribute.
  2153. */
  2154. static ssize_t
  2155. _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
  2156. char *buf)
  2157. {
  2158. struct Scsi_Host *shost = class_to_shost(cdev);
  2159. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2160. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
  2161. }
  2162. static DEVICE_ATTR(version_product, S_IRUGO,
  2163. _ctl_version_product_show, NULL);
  2164. /**
  2165. * _ctl_version_nvdata_persistent_show - ndvata persistent version
  2166. * @cdev - pointer to embedded class device
  2167. * @buf - the buffer returned
  2168. *
  2169. * A sysfs 'read-only' shost attribute.
  2170. */
  2171. static ssize_t
  2172. _ctl_version_nvdata_persistent_show(struct device *cdev,
  2173. struct device_attribute *attr, char *buf)
  2174. {
  2175. struct Scsi_Host *shost = class_to_shost(cdev);
  2176. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2177. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2178. le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
  2179. }
  2180. static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2181. _ctl_version_nvdata_persistent_show, NULL);
  2182. /**
  2183. * _ctl_version_nvdata_default_show - nvdata default version
  2184. * @cdev - pointer to embedded class device
  2185. * @buf - the buffer returned
  2186. *
  2187. * A sysfs 'read-only' shost attribute.
  2188. */
  2189. static ssize_t
  2190. _ctl_version_nvdata_default_show(struct device *cdev,
  2191. struct device_attribute *attr, char *buf)
  2192. {
  2193. struct Scsi_Host *shost = class_to_shost(cdev);
  2194. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2195. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2196. le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
  2197. }
  2198. static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2199. _ctl_version_nvdata_default_show, NULL);
  2200. /**
  2201. * _ctl_board_name_show - board name
  2202. * @cdev - pointer to embedded class device
  2203. * @buf - the buffer returned
  2204. *
  2205. * A sysfs 'read-only' shost attribute.
  2206. */
  2207. static ssize_t
  2208. _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
  2209. char *buf)
  2210. {
  2211. struct Scsi_Host *shost = class_to_shost(cdev);
  2212. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2213. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
  2214. }
  2215. static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
  2216. /**
  2217. * _ctl_board_assembly_show - board assembly name
  2218. * @cdev - pointer to embedded class device
  2219. * @buf - the buffer returned
  2220. *
  2221. * A sysfs 'read-only' shost attribute.
  2222. */
  2223. static ssize_t
  2224. _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
  2225. char *buf)
  2226. {
  2227. struct Scsi_Host *shost = class_to_shost(cdev);
  2228. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2229. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
  2230. }
  2231. static DEVICE_ATTR(board_assembly, S_IRUGO,
  2232. _ctl_board_assembly_show, NULL);
  2233. /**
  2234. * _ctl_board_tracer_show - board tracer number
  2235. * @cdev - pointer to embedded class device
  2236. * @buf - the buffer returned
  2237. *
  2238. * A sysfs 'read-only' shost attribute.
  2239. */
  2240. static ssize_t
  2241. _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
  2242. char *buf)
  2243. {
  2244. struct Scsi_Host *shost = class_to_shost(cdev);
  2245. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2246. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
  2247. }
  2248. static DEVICE_ATTR(board_tracer, S_IRUGO,
  2249. _ctl_board_tracer_show, NULL);
  2250. /**
  2251. * _ctl_io_delay_show - io missing delay
  2252. * @cdev - pointer to embedded class device
  2253. * @buf - the buffer returned
  2254. *
  2255. * This is for firmware implemention for deboucing device
  2256. * removal events.
  2257. *
  2258. * A sysfs 'read-only' shost attribute.
  2259. */
  2260. static ssize_t
  2261. _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
  2262. char *buf)
  2263. {
  2264. struct Scsi_Host *shost = class_to_shost(cdev);
  2265. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2266. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2267. }
  2268. static DEVICE_ATTR(io_delay, S_IRUGO,
  2269. _ctl_io_delay_show, NULL);
  2270. /**
  2271. * _ctl_device_delay_show - device missing delay
  2272. * @cdev - pointer to embedded class device
  2273. * @buf - the buffer returned
  2274. *
  2275. * This is for firmware implemention for deboucing device
  2276. * removal events.
  2277. *
  2278. * A sysfs 'read-only' shost attribute.
  2279. */
  2280. static ssize_t
  2281. _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
  2282. char *buf)
  2283. {
  2284. struct Scsi_Host *shost = class_to_shost(cdev);
  2285. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2286. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2287. }
  2288. static DEVICE_ATTR(device_delay, S_IRUGO,
  2289. _ctl_device_delay_show, NULL);
  2290. /**
  2291. * _ctl_fw_queue_depth_show - global credits
  2292. * @cdev - pointer to embedded class device
  2293. * @buf - the buffer returned
  2294. *
  2295. * This is firmware queue depth limit
  2296. *
  2297. * A sysfs 'read-only' shost attribute.
  2298. */
  2299. static ssize_t
  2300. _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
  2301. char *buf)
  2302. {
  2303. struct Scsi_Host *shost = class_to_shost(cdev);
  2304. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2305. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
  2306. }
  2307. static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
  2308. _ctl_fw_queue_depth_show, NULL);
  2309. /**
  2310. * _ctl_sas_address_show - sas address
  2311. * @cdev - pointer to embedded class device
  2312. * @buf - the buffer returned
  2313. *
  2314. * This is the controller sas address
  2315. *
  2316. * A sysfs 'read-only' shost attribute.
  2317. */
  2318. static ssize_t
  2319. _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
  2320. char *buf)
  2321. {
  2322. struct Scsi_Host *shost = class_to_shost(cdev);
  2323. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2324. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2325. (unsigned long long)ioc->sas_hba.sas_address);
  2326. }
  2327. static DEVICE_ATTR(host_sas_address, S_IRUGO,
  2328. _ctl_host_sas_address_show, NULL);
  2329. /**
  2330. * _ctl_logging_level_show - logging level
  2331. * @cdev - pointer to embedded class device
  2332. * @buf - the buffer returned
  2333. *
  2334. * A sysfs 'read/write' shost attribute.
  2335. */
  2336. static ssize_t
  2337. _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
  2338. char *buf)
  2339. {
  2340. struct Scsi_Host *shost = class_to_shost(cdev);
  2341. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2342. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
  2343. }
  2344. static ssize_t
  2345. _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
  2346. const char *buf, size_t count)
  2347. {
  2348. struct Scsi_Host *shost = class_to_shost(cdev);
  2349. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2350. int val = 0;
  2351. if (sscanf(buf, "%x", &val) != 1)
  2352. return -EINVAL;
  2353. ioc->logging_level = val;
  2354. printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
  2355. ioc->logging_level);
  2356. return strlen(buf);
  2357. }
  2358. static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
  2359. _ctl_logging_level_show, _ctl_logging_level_store);
  2360. /* device attributes */
  2361. /*
  2362. * _ctl_fwfault_debug_show - show/store fwfault_debug
  2363. * @cdev - pointer to embedded class device
  2364. * @buf - the buffer returned
  2365. *
  2366. * mpt2sas_fwfault_debug is command line option
  2367. * A sysfs 'read/write' shost attribute.
  2368. */
  2369. static ssize_t
  2370. _ctl_fwfault_debug_show(struct device *cdev,
  2371. struct device_attribute *attr, char *buf)
  2372. {
  2373. struct Scsi_Host *shost = class_to_shost(cdev);
  2374. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2375. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
  2376. }
  2377. static ssize_t
  2378. _ctl_fwfault_debug_store(struct device *cdev,
  2379. struct device_attribute *attr, const char *buf, size_t count)
  2380. {
  2381. struct Scsi_Host *shost = class_to_shost(cdev);
  2382. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2383. int val = 0;
  2384. if (sscanf(buf, "%d", &val) != 1)
  2385. return -EINVAL;
  2386. ioc->fwfault_debug = val;
  2387. printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
  2388. ioc->fwfault_debug);
  2389. return strlen(buf);
  2390. }
  2391. static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
  2392. _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
  2393. /**
  2394. * _ctl_ioc_reset_count_show - ioc reset count
  2395. * @cdev - pointer to embedded class device
  2396. * @buf - the buffer returned
  2397. *
  2398. * This is firmware queue depth limit
  2399. *
  2400. * A sysfs 'read-only' shost attribute.
  2401. */
  2402. static ssize_t
  2403. _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
  2404. char *buf)
  2405. {
  2406. struct Scsi_Host *shost = class_to_shost(cdev);
  2407. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2408. return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
  2409. }
  2410. static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
  2411. _ctl_ioc_reset_count_show, NULL);
  2412. struct DIAG_BUFFER_START {
  2413. __le32 Size;
  2414. __le32 DiagVersion;
  2415. u8 BufferType;
  2416. u8 Reserved[3];
  2417. __le32 Reserved1;
  2418. __le32 Reserved2;
  2419. __le32 Reserved3;
  2420. };
  2421. /**
  2422. * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
  2423. * @cdev - pointer to embedded class device
  2424. * @buf - the buffer returned
  2425. *
  2426. * A sysfs 'read-only' shost attribute.
  2427. */
  2428. static ssize_t
  2429. _ctl_host_trace_buffer_size_show(struct device *cdev,
  2430. struct device_attribute *attr, char *buf)
  2431. {
  2432. struct Scsi_Host *shost = class_to_shost(cdev);
  2433. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2434. u32 size = 0;
  2435. struct DIAG_BUFFER_START *request_data;
  2436. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2437. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2438. "registered\n", ioc->name, __func__);
  2439. return 0;
  2440. }
  2441. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2442. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2443. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2444. "registered\n", ioc->name, __func__);
  2445. return 0;
  2446. }
  2447. request_data = (struct DIAG_BUFFER_START *)
  2448. ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
  2449. if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
  2450. le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
  2451. le32_to_cpu(request_data->Reserved3) == 0x4742444c)
  2452. size = le32_to_cpu(request_data->Size);
  2453. ioc->ring_buffer_sz = size;
  2454. return snprintf(buf, PAGE_SIZE, "%d\n", size);
  2455. }
  2456. static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
  2457. _ctl_host_trace_buffer_size_show, NULL);
  2458. /**
  2459. * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
  2460. * @cdev - pointer to embedded class device
  2461. * @buf - the buffer returned
  2462. *
  2463. * A sysfs 'read/write' shost attribute.
  2464. *
  2465. * You will only be able to read 4k bytes of ring buffer at a time.
  2466. * In order to read beyond 4k bytes, you will have to write out the
  2467. * offset to the same attribute, it will move the pointer.
  2468. */
  2469. static ssize_t
  2470. _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
  2471. char *buf)
  2472. {
  2473. struct Scsi_Host *shost = class_to_shost(cdev);
  2474. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2475. void *request_data;
  2476. u32 size;
  2477. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2478. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2479. "registered\n", ioc->name, __func__);
  2480. return 0;
  2481. }
  2482. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2483. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2484. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2485. "registered\n", ioc->name, __func__);
  2486. return 0;
  2487. }
  2488. if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
  2489. return 0;
  2490. size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
  2491. size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
  2492. request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
  2493. memcpy(buf, request_data, size);
  2494. return size;
  2495. }
  2496. static ssize_t
  2497. _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
  2498. const char *buf, size_t count)
  2499. {
  2500. struct Scsi_Host *shost = class_to_shost(cdev);
  2501. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2502. int val = 0;
  2503. if (sscanf(buf, "%d", &val) != 1)
  2504. return -EINVAL;
  2505. ioc->ring_buffer_offset = val;
  2506. return strlen(buf);
  2507. }
  2508. static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
  2509. _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
  2510. /*****************************************/
  2511. /**
  2512. * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
  2513. * @cdev - pointer to embedded class device
  2514. * @buf - the buffer returned
  2515. *
  2516. * A sysfs 'read/write' shost attribute.
  2517. *
  2518. * This is a mechnism to post/release host_trace_buffers
  2519. */
  2520. static ssize_t
  2521. _ctl_host_trace_buffer_enable_show(struct device *cdev,
  2522. struct device_attribute *attr, char *buf)
  2523. {
  2524. struct Scsi_Host *shost = class_to_shost(cdev);
  2525. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2526. if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
  2527. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2528. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
  2529. return snprintf(buf, PAGE_SIZE, "off\n");
  2530. else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2531. MPT2_DIAG_BUFFER_IS_RELEASED))
  2532. return snprintf(buf, PAGE_SIZE, "release\n");
  2533. else
  2534. return snprintf(buf, PAGE_SIZE, "post\n");
  2535. }
  2536. static ssize_t
  2537. _ctl_host_trace_buffer_enable_store(struct device *cdev,
  2538. struct device_attribute *attr, const char *buf, size_t count)
  2539. {
  2540. struct Scsi_Host *shost = class_to_shost(cdev);
  2541. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2542. char str[10] = "";
  2543. struct mpt2_diag_register diag_register;
  2544. u8 issue_reset = 0;
  2545. if (sscanf(buf, "%s", str) != 1)
  2546. return -EINVAL;
  2547. if (!strcmp(str, "post")) {
  2548. /* exit out if host buffers are already posted */
  2549. if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
  2550. (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2551. MPT2_DIAG_BUFFER_IS_REGISTERED) &&
  2552. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2553. MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
  2554. goto out;
  2555. memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
  2556. printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
  2557. ioc->name);
  2558. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  2559. diag_register.requested_buffer_size = (1024 * 1024);
  2560. diag_register.unique_id = 0x7075900;
  2561. ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
  2562. _ctl_diag_register_2(ioc, &diag_register);
  2563. } else if (!strcmp(str, "release")) {
  2564. /* exit out if host buffers are already released */
  2565. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
  2566. goto out;
  2567. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2568. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
  2569. goto out;
  2570. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2571. MPT2_DIAG_BUFFER_IS_RELEASED))
  2572. goto out;
  2573. printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
  2574. ioc->name);
  2575. _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
  2576. }
  2577. out:
  2578. return strlen(buf);
  2579. }
  2580. static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
  2581. _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
  2582. struct device_attribute *mpt2sas_host_attrs[] = {
  2583. &dev_attr_version_fw,
  2584. &dev_attr_version_bios,
  2585. &dev_attr_version_mpi,
  2586. &dev_attr_version_product,
  2587. &dev_attr_version_nvdata_persistent,
  2588. &dev_attr_version_nvdata_default,
  2589. &dev_attr_board_name,
  2590. &dev_attr_board_assembly,
  2591. &dev_attr_board_tracer,
  2592. &dev_attr_io_delay,
  2593. &dev_attr_device_delay,
  2594. &dev_attr_logging_level,
  2595. &dev_attr_fwfault_debug,
  2596. &dev_attr_fw_queue_depth,
  2597. &dev_attr_host_sas_address,
  2598. &dev_attr_ioc_reset_count,
  2599. &dev_attr_host_trace_buffer_size,
  2600. &dev_attr_host_trace_buffer,
  2601. &dev_attr_host_trace_buffer_enable,
  2602. NULL,
  2603. };
  2604. /**
  2605. * _ctl_device_sas_address_show - sas address
  2606. * @cdev - pointer to embedded class device
  2607. * @buf - the buffer returned
  2608. *
  2609. * This is the sas address for the target
  2610. *
  2611. * A sysfs 'read-only' shost attribute.
  2612. */
  2613. static ssize_t
  2614. _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
  2615. char *buf)
  2616. {
  2617. struct scsi_device *sdev = to_scsi_device(dev);
  2618. struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  2619. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2620. (unsigned long long)sas_device_priv_data->sas_target->sas_address);
  2621. }
  2622. static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
  2623. /**
  2624. * _ctl_device_handle_show - device handle
  2625. * @cdev - pointer to embedded class device
  2626. * @buf - the buffer returned
  2627. *
  2628. * This is the firmware assigned device handle
  2629. *
  2630. * A sysfs 'read-only' shost attribute.
  2631. */
  2632. static ssize_t
  2633. _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
  2634. char *buf)
  2635. {
  2636. struct scsi_device *sdev = to_scsi_device(dev);
  2637. struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  2638. return snprintf(buf, PAGE_SIZE, "0x%04x\n",
  2639. sas_device_priv_data->sas_target->handle);
  2640. }
  2641. static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
  2642. struct device_attribute *mpt2sas_dev_attrs[] = {
  2643. &dev_attr_sas_address,
  2644. &dev_attr_sas_device_handle,
  2645. NULL,
  2646. };
  2647. static const struct file_operations ctl_fops = {
  2648. .owner = THIS_MODULE,
  2649. .unlocked_ioctl = _ctl_ioctl,
  2650. .release = _ctl_release,
  2651. .poll = _ctl_poll,
  2652. .fasync = _ctl_fasync,
  2653. #ifdef CONFIG_COMPAT
  2654. .compat_ioctl = _ctl_ioctl_compat,
  2655. #endif
  2656. .llseek = noop_llseek,
  2657. };
  2658. static struct miscdevice ctl_dev = {
  2659. .minor = MPT2SAS_MINOR,
  2660. .name = MPT2SAS_DEV_NAME,
  2661. .fops = &ctl_fops,
  2662. };
  2663. /**
  2664. * mpt2sas_ctl_init - main entry point for ctl.
  2665. *
  2666. */
  2667. void
  2668. mpt2sas_ctl_init(void)
  2669. {
  2670. async_queue = NULL;
  2671. if (misc_register(&ctl_dev) < 0)
  2672. printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
  2673. MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
  2674. init_waitqueue_head(&ctl_poll_wait);
  2675. }
  2676. /**
  2677. * mpt2sas_ctl_exit - exit point for ctl
  2678. *
  2679. */
  2680. void
  2681. mpt2sas_ctl_exit(void)
  2682. {
  2683. struct MPT2SAS_ADAPTER *ioc;
  2684. int i;
  2685. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  2686. /* free memory associated to diag buffers */
  2687. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  2688. if (!ioc->diag_buffer[i])
  2689. continue;
  2690. pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
  2691. ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
  2692. ioc->diag_buffer[i] = NULL;
  2693. ioc->diag_buffer_status[i] = 0;
  2694. }
  2695. kfree(ioc->event_log);
  2696. }
  2697. misc_deregister(&ctl_dev);
  2698. }