mpt2sas_ctl.c 85 KB

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