mpt2sas_ctl.c 85 KB

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