mpt2sas_ctl.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076
  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-2013 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. * Returns void.
  365. */
  366. void
  367. mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
  368. u32 reply)
  369. {
  370. Mpi2EventNotificationReply_t *mpi_reply;
  371. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  372. if (unlikely(!mpi_reply)) {
  373. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  374. ioc->name, __FILE__, __LINE__, __func__);
  375. return;
  376. }
  377. mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
  378. return;
  379. }
  380. /**
  381. * _ctl_verify_adapter - validates ioc_number passed from application
  382. * @ioc: per adapter object
  383. * @iocpp: The ioc pointer is returned in this.
  384. *
  385. * Return (-1) means error, else ioc_number.
  386. */
  387. static int
  388. _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
  389. {
  390. struct MPT2SAS_ADAPTER *ioc;
  391. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  392. if (ioc->id != ioc_number)
  393. continue;
  394. *iocpp = ioc;
  395. return ioc_number;
  396. }
  397. *iocpp = NULL;
  398. return -1;
  399. }
  400. /**
  401. * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
  402. * @ioc: per adapter object
  403. * @reset_phase: phase
  404. *
  405. * The handler for doing any required cleanup or initialization.
  406. *
  407. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  408. * MPT2_IOC_DONE_RESET
  409. */
  410. void
  411. mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  412. {
  413. int i;
  414. u8 issue_reset;
  415. switch (reset_phase) {
  416. case MPT2_IOC_PRE_RESET:
  417. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  418. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  419. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  420. if (!(ioc->diag_buffer_status[i] &
  421. MPT2_DIAG_BUFFER_IS_REGISTERED))
  422. continue;
  423. if ((ioc->diag_buffer_status[i] &
  424. MPT2_DIAG_BUFFER_IS_RELEASED))
  425. continue;
  426. _ctl_send_release(ioc, i, &issue_reset);
  427. }
  428. break;
  429. case MPT2_IOC_AFTER_RESET:
  430. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  431. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  432. if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
  433. ioc->ctl_cmds.status |= MPT2_CMD_RESET;
  434. mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
  435. complete(&ioc->ctl_cmds.done);
  436. }
  437. break;
  438. case MPT2_IOC_DONE_RESET:
  439. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  440. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  441. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  442. if (!(ioc->diag_buffer_status[i] &
  443. MPT2_DIAG_BUFFER_IS_REGISTERED))
  444. continue;
  445. if ((ioc->diag_buffer_status[i] &
  446. MPT2_DIAG_BUFFER_IS_RELEASED))
  447. continue;
  448. ioc->diag_buffer_status[i] |=
  449. MPT2_DIAG_BUFFER_IS_DIAG_RESET;
  450. }
  451. break;
  452. }
  453. }
  454. /**
  455. * _ctl_fasync -
  456. * @fd -
  457. * @filep -
  458. * @mode -
  459. *
  460. * Called when application request fasyn callback handler.
  461. */
  462. static int
  463. _ctl_fasync(int fd, struct file *filep, int mode)
  464. {
  465. return fasync_helper(fd, filep, mode, &async_queue);
  466. }
  467. /**
  468. * _ctl_poll -
  469. * @file -
  470. * @wait -
  471. *
  472. */
  473. static unsigned int
  474. _ctl_poll(struct file *filep, poll_table *wait)
  475. {
  476. struct MPT2SAS_ADAPTER *ioc;
  477. poll_wait(filep, &ctl_poll_wait, wait);
  478. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  479. if (ioc->aen_event_read_flag)
  480. return POLLIN | POLLRDNORM;
  481. }
  482. return 0;
  483. }
  484. /**
  485. * _ctl_set_task_mid - assign an active smid to tm request
  486. * @ioc: per adapter object
  487. * @karg - (struct mpt2_ioctl_command)
  488. * @tm_request - pointer to mf from user space
  489. *
  490. * Returns 0 when an smid if found, else fail.
  491. * during failure, the reply frame is filled.
  492. */
  493. static int
  494. _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
  495. Mpi2SCSITaskManagementRequest_t *tm_request)
  496. {
  497. u8 found = 0;
  498. u16 i;
  499. u16 handle;
  500. struct scsi_cmnd *scmd;
  501. struct MPT2SAS_DEVICE *priv_data;
  502. unsigned long flags;
  503. Mpi2SCSITaskManagementReply_t *tm_reply;
  504. u32 sz;
  505. u32 lun;
  506. char *desc = NULL;
  507. if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  508. desc = "abort_task";
  509. else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
  510. desc = "query_task";
  511. else
  512. return 0;
  513. lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
  514. handle = le16_to_cpu(tm_request->DevHandle);
  515. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  516. for (i = ioc->scsiio_depth; i && !found; i--) {
  517. scmd = ioc->scsi_lookup[i - 1].scmd;
  518. if (scmd == NULL || scmd->device == NULL ||
  519. scmd->device->hostdata == NULL)
  520. continue;
  521. if (lun != scmd->device->lun)
  522. continue;
  523. priv_data = scmd->device->hostdata;
  524. if (priv_data->sas_target == NULL)
  525. continue;
  526. if (priv_data->sas_target->handle != handle)
  527. continue;
  528. tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
  529. found = 1;
  530. }
  531. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  532. if (!found) {
  533. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  534. "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
  535. desc, le16_to_cpu(tm_request->DevHandle), lun));
  536. tm_reply = ioc->ctl_cmds.reply;
  537. tm_reply->DevHandle = tm_request->DevHandle;
  538. tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  539. tm_reply->TaskType = tm_request->TaskType;
  540. tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
  541. tm_reply->VP_ID = tm_request->VP_ID;
  542. tm_reply->VF_ID = tm_request->VF_ID;
  543. sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
  544. if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
  545. sz))
  546. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  547. __LINE__, __func__);
  548. return 1;
  549. }
  550. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  551. "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
  552. desc, le16_to_cpu(tm_request->DevHandle), lun,
  553. le16_to_cpu(tm_request->TaskMID)));
  554. return 0;
  555. }
  556. /**
  557. * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
  558. * @ioc: per adapter object
  559. * @karg - (struct mpt2_ioctl_command)
  560. * @mf - pointer to mf in user space
  561. */
  562. static long
  563. _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
  564. void __user *mf)
  565. {
  566. MPI2RequestHeader_t *mpi_request = NULL, *request;
  567. MPI2DefaultReply_t *mpi_reply;
  568. u32 ioc_state;
  569. u16 ioc_status;
  570. u16 smid;
  571. unsigned long timeout, timeleft;
  572. u8 issue_reset;
  573. u32 sz;
  574. void *psge;
  575. void *data_out = NULL;
  576. dma_addr_t data_out_dma;
  577. size_t data_out_sz = 0;
  578. void *data_in = NULL;
  579. dma_addr_t data_in_dma;
  580. size_t data_in_sz = 0;
  581. u32 sgl_flags;
  582. long ret;
  583. u16 wait_state_count;
  584. issue_reset = 0;
  585. if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
  586. printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
  587. ioc->name, __func__);
  588. ret = -EAGAIN;
  589. goto out;
  590. }
  591. wait_state_count = 0;
  592. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  593. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  594. if (wait_state_count++ == 10) {
  595. printk(MPT2SAS_ERR_FMT
  596. "%s: failed due to ioc not operational\n",
  597. ioc->name, __func__);
  598. ret = -EFAULT;
  599. goto out;
  600. }
  601. ssleep(1);
  602. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  603. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  604. "operational state(count=%d)\n", ioc->name,
  605. __func__, wait_state_count);
  606. }
  607. if (wait_state_count)
  608. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  609. ioc->name, __func__);
  610. mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
  611. if (!mpi_request) {
  612. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
  613. "mpi_request\n", ioc->name, __func__);
  614. ret = -ENOMEM;
  615. goto out;
  616. }
  617. /* Check for overflow and wraparound */
  618. if (karg.data_sge_offset * 4 > ioc->request_sz ||
  619. karg.data_sge_offset > (UINT_MAX / 4)) {
  620. ret = -EINVAL;
  621. goto out;
  622. }
  623. /* copy in request message frame from user */
  624. if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
  625. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
  626. __func__);
  627. ret = -EFAULT;
  628. goto out;
  629. }
  630. if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
  631. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
  632. if (!smid) {
  633. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  634. ioc->name, __func__);
  635. ret = -EAGAIN;
  636. goto out;
  637. }
  638. } else {
  639. smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
  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. }
  647. ret = 0;
  648. ioc->ctl_cmds.status = MPT2_CMD_PENDING;
  649. memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
  650. request = mpt2sas_base_get_msg_frame(ioc, smid);
  651. memcpy(request, mpi_request, karg.data_sge_offset*4);
  652. ioc->ctl_cmds.smid = smid;
  653. data_out_sz = karg.data_out_size;
  654. data_in_sz = karg.data_in_size;
  655. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  656. mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
  657. if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
  658. le16_to_cpu(mpi_request->FunctionDependent1) >
  659. ioc->facts.MaxDevHandle) {
  660. ret = -EINVAL;
  661. mpt2sas_base_free_smid(ioc, smid);
  662. goto out;
  663. }
  664. }
  665. /* obtain dma-able memory for data transfer */
  666. if (data_out_sz) /* WRITE */ {
  667. data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
  668. &data_out_dma);
  669. if (!data_out) {
  670. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  671. __LINE__, __func__);
  672. ret = -ENOMEM;
  673. mpt2sas_base_free_smid(ioc, smid);
  674. goto out;
  675. }
  676. if (copy_from_user(data_out, karg.data_out_buf_ptr,
  677. data_out_sz)) {
  678. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  679. __LINE__, __func__);
  680. ret = -EFAULT;
  681. mpt2sas_base_free_smid(ioc, smid);
  682. goto out;
  683. }
  684. }
  685. if (data_in_sz) /* READ */ {
  686. data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
  687. &data_in_dma);
  688. if (!data_in) {
  689. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  690. __LINE__, __func__);
  691. ret = -ENOMEM;
  692. mpt2sas_base_free_smid(ioc, smid);
  693. goto out;
  694. }
  695. }
  696. /* add scatter gather elements */
  697. psge = (void *)request + (karg.data_sge_offset*4);
  698. if (!data_out_sz && !data_in_sz) {
  699. mpt2sas_base_build_zero_len_sge(ioc, psge);
  700. } else if (data_out_sz && data_in_sz) {
  701. /* WRITE sgel first */
  702. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  703. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  704. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  705. ioc->base_add_sg_single(psge, sgl_flags |
  706. data_out_sz, data_out_dma);
  707. /* incr sgel */
  708. psge += ioc->sge_size;
  709. /* READ sgel last */
  710. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  711. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  712. MPI2_SGE_FLAGS_END_OF_LIST);
  713. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  714. ioc->base_add_sg_single(psge, sgl_flags |
  715. data_in_sz, data_in_dma);
  716. } else if (data_out_sz) /* WRITE */ {
  717. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  718. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  719. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  720. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  721. ioc->base_add_sg_single(psge, sgl_flags |
  722. data_out_sz, data_out_dma);
  723. } else if (data_in_sz) /* READ */ {
  724. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  725. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  726. MPI2_SGE_FLAGS_END_OF_LIST);
  727. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  728. ioc->base_add_sg_single(psge, sgl_flags |
  729. data_in_sz, data_in_dma);
  730. }
  731. /* send command to firmware */
  732. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  733. _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
  734. #endif
  735. init_completion(&ioc->ctl_cmds.done);
  736. switch (mpi_request->Function) {
  737. case MPI2_FUNCTION_SCSI_IO_REQUEST:
  738. case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
  739. {
  740. Mpi2SCSIIORequest_t *scsiio_request =
  741. (Mpi2SCSIIORequest_t *)request;
  742. scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  743. scsiio_request->SenseBufferLowAddress =
  744. mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  745. memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
  746. if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
  747. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  748. le16_to_cpu(mpi_request->FunctionDependent1));
  749. else
  750. mpt2sas_base_put_smid_default(ioc, smid);
  751. break;
  752. }
  753. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  754. {
  755. Mpi2SCSITaskManagementRequest_t *tm_request =
  756. (Mpi2SCSITaskManagementRequest_t *)request;
  757. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
  758. "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
  759. le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
  760. if (tm_request->TaskType ==
  761. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
  762. tm_request->TaskType ==
  763. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
  764. if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
  765. mpt2sas_base_free_smid(ioc, smid);
  766. goto out;
  767. }
  768. }
  769. mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
  770. tm_request->DevHandle));
  771. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  772. break;
  773. }
  774. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  775. {
  776. Mpi2SmpPassthroughRequest_t *smp_request =
  777. (Mpi2SmpPassthroughRequest_t *)mpi_request;
  778. u8 *data;
  779. /* ioc determines which port to use */
  780. smp_request->PhysicalPort = 0xFF;
  781. if (smp_request->PassthroughFlags &
  782. MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
  783. data = (u8 *)&smp_request->SGL;
  784. else {
  785. if (unlikely(data_out == NULL)) {
  786. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  787. __FILE__, __LINE__, __func__);
  788. mpt2sas_base_free_smid(ioc, smid);
  789. ret = -EINVAL;
  790. goto out;
  791. }
  792. data = data_out;
  793. }
  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) {
  1934. if (!mutex_trylock(&ioc->ctl_cmds.mutex))
  1935. return -EAGAIN;
  1936. } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
  1937. return -ERESTARTSYS;
  1938. }
  1939. switch (cmd) {
  1940. case MPT2IOCINFO:
  1941. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
  1942. ret = _ctl_getiocinfo(ioc, arg);
  1943. break;
  1944. #ifdef CONFIG_COMPAT
  1945. case MPT2COMMAND32:
  1946. #endif
  1947. case MPT2COMMAND:
  1948. {
  1949. struct mpt2_ioctl_command __user *uarg;
  1950. struct mpt2_ioctl_command karg;
  1951. #ifdef CONFIG_COMPAT
  1952. if (compat) {
  1953. ret = _ctl_compat_mpt_command(ioc, cmd, arg);
  1954. break;
  1955. }
  1956. #endif
  1957. if (copy_from_user(&karg, arg, sizeof(karg))) {
  1958. printk(KERN_ERR "failure at %s:%d/%s()!\n",
  1959. __FILE__, __LINE__, __func__);
  1960. ret = -EFAULT;
  1961. break;
  1962. }
  1963. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
  1964. uarg = arg;
  1965. ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
  1966. }
  1967. break;
  1968. }
  1969. case MPT2EVENTQUERY:
  1970. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
  1971. ret = _ctl_eventquery(ioc, arg);
  1972. break;
  1973. case MPT2EVENTENABLE:
  1974. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
  1975. ret = _ctl_eventenable(ioc, arg);
  1976. break;
  1977. case MPT2EVENTREPORT:
  1978. ret = _ctl_eventreport(ioc, arg);
  1979. break;
  1980. case MPT2HARDRESET:
  1981. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
  1982. ret = _ctl_do_reset(ioc, arg);
  1983. break;
  1984. case MPT2BTDHMAPPING:
  1985. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
  1986. ret = _ctl_btdh_mapping(ioc, arg);
  1987. break;
  1988. case MPT2DIAGREGISTER:
  1989. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
  1990. ret = _ctl_diag_register(ioc, arg);
  1991. break;
  1992. case MPT2DIAGUNREGISTER:
  1993. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
  1994. ret = _ctl_diag_unregister(ioc, arg);
  1995. break;
  1996. case MPT2DIAGQUERY:
  1997. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
  1998. ret = _ctl_diag_query(ioc, arg);
  1999. break;
  2000. case MPT2DIAGRELEASE:
  2001. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
  2002. ret = _ctl_diag_release(ioc, arg);
  2003. break;
  2004. case MPT2DIAGREADBUFFER:
  2005. if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
  2006. ret = _ctl_diag_read_buffer(ioc, arg);
  2007. break;
  2008. default:
  2009. dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
  2010. "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
  2011. break;
  2012. }
  2013. mutex_unlock(&ioc->ctl_cmds.mutex);
  2014. return ret;
  2015. }
  2016. /**
  2017. * _ctl_ioctl - main ioctl entry point (unlocked)
  2018. * @file - (struct file)
  2019. * @cmd - ioctl opcode
  2020. * @arg -
  2021. */
  2022. static long
  2023. _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2024. {
  2025. long ret;
  2026. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
  2027. return ret;
  2028. }
  2029. #ifdef CONFIG_COMPAT
  2030. /**
  2031. * _ctl_ioctl_compat - main ioctl entry point (compat)
  2032. * @file -
  2033. * @cmd -
  2034. * @arg -
  2035. *
  2036. * This routine handles 32 bit applications in 64bit os.
  2037. */
  2038. static long
  2039. _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
  2040. {
  2041. long ret;
  2042. ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
  2043. return ret;
  2044. }
  2045. #endif
  2046. /* scsi host attributes */
  2047. /**
  2048. * _ctl_version_fw_show - firmware version
  2049. * @cdev - pointer to embedded class device
  2050. * @buf - the buffer returned
  2051. *
  2052. * A sysfs 'read-only' shost attribute.
  2053. */
  2054. static ssize_t
  2055. _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
  2056. char *buf)
  2057. {
  2058. struct Scsi_Host *shost = class_to_shost(cdev);
  2059. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2060. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2061. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  2062. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  2063. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  2064. ioc->facts.FWVersion.Word & 0x000000FF);
  2065. }
  2066. static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
  2067. /**
  2068. * _ctl_version_bios_show - bios version
  2069. * @cdev - pointer to embedded class device
  2070. * @buf - the buffer returned
  2071. *
  2072. * A sysfs 'read-only' shost attribute.
  2073. */
  2074. static ssize_t
  2075. _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
  2076. char *buf)
  2077. {
  2078. struct Scsi_Host *shost = class_to_shost(cdev);
  2079. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2080. u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  2081. return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
  2082. (version & 0xFF000000) >> 24,
  2083. (version & 0x00FF0000) >> 16,
  2084. (version & 0x0000FF00) >> 8,
  2085. version & 0x000000FF);
  2086. }
  2087. static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
  2088. /**
  2089. * _ctl_version_mpi_show - MPI (message passing interface) version
  2090. * @cdev - pointer to embedded class device
  2091. * @buf - the buffer returned
  2092. *
  2093. * A sysfs 'read-only' shost attribute.
  2094. */
  2095. static ssize_t
  2096. _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
  2097. char *buf)
  2098. {
  2099. struct Scsi_Host *shost = class_to_shost(cdev);
  2100. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2101. return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
  2102. ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
  2103. }
  2104. static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
  2105. /**
  2106. * _ctl_version_product_show - product name
  2107. * @cdev - pointer to embedded class device
  2108. * @buf - the buffer returned
  2109. *
  2110. * A sysfs 'read-only' shost attribute.
  2111. */
  2112. static ssize_t
  2113. _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
  2114. char *buf)
  2115. {
  2116. struct Scsi_Host *shost = class_to_shost(cdev);
  2117. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2118. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
  2119. }
  2120. static DEVICE_ATTR(version_product, S_IRUGO,
  2121. _ctl_version_product_show, NULL);
  2122. /**
  2123. * _ctl_version_nvdata_persistent_show - ndvata persistent version
  2124. * @cdev - pointer to embedded class device
  2125. * @buf - the buffer returned
  2126. *
  2127. * A sysfs 'read-only' shost attribute.
  2128. */
  2129. static ssize_t
  2130. _ctl_version_nvdata_persistent_show(struct device *cdev,
  2131. struct device_attribute *attr, char *buf)
  2132. {
  2133. struct Scsi_Host *shost = class_to_shost(cdev);
  2134. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2135. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2136. le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
  2137. }
  2138. static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
  2139. _ctl_version_nvdata_persistent_show, NULL);
  2140. /**
  2141. * _ctl_version_nvdata_default_show - nvdata default version
  2142. * @cdev - pointer to embedded class device
  2143. * @buf - the buffer returned
  2144. *
  2145. * A sysfs 'read-only' shost attribute.
  2146. */
  2147. static ssize_t
  2148. _ctl_version_nvdata_default_show(struct device *cdev,
  2149. struct device_attribute *attr, char *buf)
  2150. {
  2151. struct Scsi_Host *shost = class_to_shost(cdev);
  2152. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2153. return snprintf(buf, PAGE_SIZE, "%08xh\n",
  2154. le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
  2155. }
  2156. static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
  2157. _ctl_version_nvdata_default_show, NULL);
  2158. /**
  2159. * _ctl_board_name_show - board name
  2160. * @cdev - pointer to embedded class device
  2161. * @buf - the buffer returned
  2162. *
  2163. * A sysfs 'read-only' shost attribute.
  2164. */
  2165. static ssize_t
  2166. _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
  2167. char *buf)
  2168. {
  2169. struct Scsi_Host *shost = class_to_shost(cdev);
  2170. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2171. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
  2172. }
  2173. static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
  2174. /**
  2175. * _ctl_board_assembly_show - board assembly name
  2176. * @cdev - pointer to embedded class device
  2177. * @buf - the buffer returned
  2178. *
  2179. * A sysfs 'read-only' shost attribute.
  2180. */
  2181. static ssize_t
  2182. _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
  2183. char *buf)
  2184. {
  2185. struct Scsi_Host *shost = class_to_shost(cdev);
  2186. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2187. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
  2188. }
  2189. static DEVICE_ATTR(board_assembly, S_IRUGO,
  2190. _ctl_board_assembly_show, NULL);
  2191. /**
  2192. * _ctl_board_tracer_show - board tracer number
  2193. * @cdev - pointer to embedded class device
  2194. * @buf - the buffer returned
  2195. *
  2196. * A sysfs 'read-only' shost attribute.
  2197. */
  2198. static ssize_t
  2199. _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
  2200. char *buf)
  2201. {
  2202. struct Scsi_Host *shost = class_to_shost(cdev);
  2203. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2204. return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
  2205. }
  2206. static DEVICE_ATTR(board_tracer, S_IRUGO,
  2207. _ctl_board_tracer_show, NULL);
  2208. /**
  2209. * _ctl_io_delay_show - io missing delay
  2210. * @cdev - pointer to embedded class device
  2211. * @buf - the buffer returned
  2212. *
  2213. * This is for firmware implemention for deboucing device
  2214. * removal events.
  2215. *
  2216. * A sysfs 'read-only' shost attribute.
  2217. */
  2218. static ssize_t
  2219. _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
  2220. char *buf)
  2221. {
  2222. struct Scsi_Host *shost = class_to_shost(cdev);
  2223. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2224. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
  2225. }
  2226. static DEVICE_ATTR(io_delay, S_IRUGO,
  2227. _ctl_io_delay_show, NULL);
  2228. /**
  2229. * _ctl_device_delay_show - device missing delay
  2230. * @cdev - pointer to embedded class device
  2231. * @buf - the buffer returned
  2232. *
  2233. * This is for firmware implemention for deboucing device
  2234. * removal events.
  2235. *
  2236. * A sysfs 'read-only' shost attribute.
  2237. */
  2238. static ssize_t
  2239. _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
  2240. char *buf)
  2241. {
  2242. struct Scsi_Host *shost = class_to_shost(cdev);
  2243. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2244. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
  2245. }
  2246. static DEVICE_ATTR(device_delay, S_IRUGO,
  2247. _ctl_device_delay_show, NULL);
  2248. /**
  2249. * _ctl_fw_queue_depth_show - global credits
  2250. * @cdev - pointer to embedded class device
  2251. * @buf - the buffer returned
  2252. *
  2253. * This is firmware queue depth limit
  2254. *
  2255. * A sysfs 'read-only' shost attribute.
  2256. */
  2257. static ssize_t
  2258. _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
  2259. char *buf)
  2260. {
  2261. struct Scsi_Host *shost = class_to_shost(cdev);
  2262. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2263. return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
  2264. }
  2265. static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
  2266. _ctl_fw_queue_depth_show, NULL);
  2267. /**
  2268. * _ctl_sas_address_show - sas address
  2269. * @cdev - pointer to embedded class device
  2270. * @buf - the buffer returned
  2271. *
  2272. * This is the controller sas address
  2273. *
  2274. * A sysfs 'read-only' shost attribute.
  2275. */
  2276. static ssize_t
  2277. _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
  2278. char *buf)
  2279. {
  2280. struct Scsi_Host *shost = class_to_shost(cdev);
  2281. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2282. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2283. (unsigned long long)ioc->sas_hba.sas_address);
  2284. }
  2285. static DEVICE_ATTR(host_sas_address, S_IRUGO,
  2286. _ctl_host_sas_address_show, NULL);
  2287. /**
  2288. * _ctl_logging_level_show - logging level
  2289. * @cdev - pointer to embedded class device
  2290. * @buf - the buffer returned
  2291. *
  2292. * A sysfs 'read/write' shost attribute.
  2293. */
  2294. static ssize_t
  2295. _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
  2296. char *buf)
  2297. {
  2298. struct Scsi_Host *shost = class_to_shost(cdev);
  2299. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2300. return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
  2301. }
  2302. static ssize_t
  2303. _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
  2304. const char *buf, size_t count)
  2305. {
  2306. struct Scsi_Host *shost = class_to_shost(cdev);
  2307. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2308. int val = 0;
  2309. if (sscanf(buf, "%x", &val) != 1)
  2310. return -EINVAL;
  2311. ioc->logging_level = val;
  2312. printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
  2313. ioc->logging_level);
  2314. return strlen(buf);
  2315. }
  2316. static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
  2317. _ctl_logging_level_show, _ctl_logging_level_store);
  2318. /* device attributes */
  2319. /*
  2320. * _ctl_fwfault_debug_show - show/store fwfault_debug
  2321. * @cdev - pointer to embedded class device
  2322. * @buf - the buffer returned
  2323. *
  2324. * mpt2sas_fwfault_debug is command line option
  2325. * A sysfs 'read/write' shost attribute.
  2326. */
  2327. static ssize_t
  2328. _ctl_fwfault_debug_show(struct device *cdev,
  2329. struct device_attribute *attr, char *buf)
  2330. {
  2331. struct Scsi_Host *shost = class_to_shost(cdev);
  2332. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2333. return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
  2334. }
  2335. static ssize_t
  2336. _ctl_fwfault_debug_store(struct device *cdev,
  2337. struct device_attribute *attr, const char *buf, size_t count)
  2338. {
  2339. struct Scsi_Host *shost = class_to_shost(cdev);
  2340. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2341. int val = 0;
  2342. if (sscanf(buf, "%d", &val) != 1)
  2343. return -EINVAL;
  2344. ioc->fwfault_debug = val;
  2345. printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
  2346. ioc->fwfault_debug);
  2347. return strlen(buf);
  2348. }
  2349. static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
  2350. _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
  2351. /**
  2352. * _ctl_ioc_reset_count_show - ioc reset count
  2353. * @cdev - pointer to embedded class device
  2354. * @buf - the buffer returned
  2355. *
  2356. * This is firmware queue depth limit
  2357. *
  2358. * A sysfs 'read-only' shost attribute.
  2359. */
  2360. static ssize_t
  2361. _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
  2362. char *buf)
  2363. {
  2364. struct Scsi_Host *shost = class_to_shost(cdev);
  2365. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2366. return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
  2367. }
  2368. static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
  2369. _ctl_ioc_reset_count_show, NULL);
  2370. /**
  2371. * _ctl_ioc_reply_queue_count_show - number of reply queues
  2372. * @cdev - pointer to embedded class device
  2373. * @buf - the buffer returned
  2374. *
  2375. * This is number of reply queues
  2376. *
  2377. * A sysfs 'read-only' shost attribute.
  2378. */
  2379. static ssize_t
  2380. _ctl_ioc_reply_queue_count_show(struct device *cdev,
  2381. struct device_attribute *attr, char *buf)
  2382. {
  2383. u8 reply_queue_count;
  2384. struct Scsi_Host *shost = class_to_shost(cdev);
  2385. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2386. if ((ioc->facts.IOCCapabilities &
  2387. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
  2388. reply_queue_count = ioc->reply_queue_count;
  2389. else
  2390. reply_queue_count = 1;
  2391. return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
  2392. }
  2393. static DEVICE_ATTR(reply_queue_count, S_IRUGO,
  2394. _ctl_ioc_reply_queue_count_show, NULL);
  2395. /**
  2396. * _ctl_BRM_status_show - Backup Rail Monitor Status
  2397. * @cdev - pointer to embedded class device
  2398. * @buf - the buffer returned
  2399. *
  2400. * This is number of reply queues
  2401. *
  2402. * A sysfs 'read-only' shost attribute.
  2403. */
  2404. static ssize_t
  2405. _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
  2406. char *buf)
  2407. {
  2408. struct Scsi_Host *shost = class_to_shost(cdev);
  2409. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2410. Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
  2411. Mpi2ConfigReply_t mpi_reply;
  2412. u16 backup_rail_monitor_status = 0;
  2413. u16 ioc_status;
  2414. int sz;
  2415. ssize_t rc = 0;
  2416. if (!ioc->is_warpdrive) {
  2417. printk(MPT2SAS_ERR_FMT "%s: BRM attribute is only for"\
  2418. "warpdrive\n", ioc->name, __func__);
  2419. goto out;
  2420. }
  2421. /* allocate upto GPIOVal 36 entries */
  2422. sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
  2423. io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
  2424. if (!io_unit_pg3) {
  2425. printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"\
  2426. "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
  2427. goto out;
  2428. }
  2429. if (mpt2sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
  2430. 0) {
  2431. printk(MPT2SAS_ERR_FMT
  2432. "%s: failed reading iounit_pg3\n", ioc->name,
  2433. __func__);
  2434. goto out;
  2435. }
  2436. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  2437. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2438. printk(MPT2SAS_ERR_FMT "%s: iounit_pg3 failed with"\
  2439. "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
  2440. goto out;
  2441. }
  2442. if (io_unit_pg3->GPIOCount < 25) {
  2443. printk(MPT2SAS_ERR_FMT "%s: iounit_pg3->GPIOCount less than"\
  2444. "25 entries, detected (%d) entries\n", ioc->name, __func__,
  2445. io_unit_pg3->GPIOCount);
  2446. goto out;
  2447. }
  2448. /* BRM status is in bit zero of GPIOVal[24] */
  2449. backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
  2450. rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
  2451. out:
  2452. kfree(io_unit_pg3);
  2453. return rc;
  2454. }
  2455. static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
  2456. struct DIAG_BUFFER_START {
  2457. __le32 Size;
  2458. __le32 DiagVersion;
  2459. u8 BufferType;
  2460. u8 Reserved[3];
  2461. __le32 Reserved1;
  2462. __le32 Reserved2;
  2463. __le32 Reserved3;
  2464. };
  2465. /**
  2466. * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
  2467. * @cdev - pointer to embedded class device
  2468. * @buf - the buffer returned
  2469. *
  2470. * A sysfs 'read-only' shost attribute.
  2471. */
  2472. static ssize_t
  2473. _ctl_host_trace_buffer_size_show(struct device *cdev,
  2474. struct device_attribute *attr, char *buf)
  2475. {
  2476. struct Scsi_Host *shost = class_to_shost(cdev);
  2477. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2478. u32 size = 0;
  2479. struct DIAG_BUFFER_START *request_data;
  2480. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2481. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2482. "registered\n", ioc->name, __func__);
  2483. return 0;
  2484. }
  2485. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2486. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2487. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2488. "registered\n", ioc->name, __func__);
  2489. return 0;
  2490. }
  2491. request_data = (struct DIAG_BUFFER_START *)
  2492. ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
  2493. if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
  2494. le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
  2495. le32_to_cpu(request_data->Reserved3) == 0x4742444c)
  2496. size = le32_to_cpu(request_data->Size);
  2497. ioc->ring_buffer_sz = size;
  2498. return snprintf(buf, PAGE_SIZE, "%d\n", size);
  2499. }
  2500. static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
  2501. _ctl_host_trace_buffer_size_show, NULL);
  2502. /**
  2503. * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
  2504. * @cdev - pointer to embedded class device
  2505. * @buf - the buffer returned
  2506. *
  2507. * A sysfs 'read/write' shost attribute.
  2508. *
  2509. * You will only be able to read 4k bytes of ring buffer at a time.
  2510. * In order to read beyond 4k bytes, you will have to write out the
  2511. * offset to the same attribute, it will move the pointer.
  2512. */
  2513. static ssize_t
  2514. _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
  2515. char *buf)
  2516. {
  2517. struct Scsi_Host *shost = class_to_shost(cdev);
  2518. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2519. void *request_data;
  2520. u32 size;
  2521. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
  2522. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2523. "registered\n", ioc->name, __func__);
  2524. return 0;
  2525. }
  2526. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2527. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
  2528. printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
  2529. "registered\n", ioc->name, __func__);
  2530. return 0;
  2531. }
  2532. if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
  2533. return 0;
  2534. size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
  2535. size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
  2536. request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
  2537. memcpy(buf, request_data, size);
  2538. return size;
  2539. }
  2540. static ssize_t
  2541. _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
  2542. const char *buf, size_t count)
  2543. {
  2544. struct Scsi_Host *shost = class_to_shost(cdev);
  2545. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2546. int val = 0;
  2547. if (sscanf(buf, "%d", &val) != 1)
  2548. return -EINVAL;
  2549. ioc->ring_buffer_offset = val;
  2550. return strlen(buf);
  2551. }
  2552. static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
  2553. _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
  2554. /*****************************************/
  2555. /**
  2556. * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
  2557. * @cdev - pointer to embedded class device
  2558. * @buf - the buffer returned
  2559. *
  2560. * A sysfs 'read/write' shost attribute.
  2561. *
  2562. * This is a mechnism to post/release host_trace_buffers
  2563. */
  2564. static ssize_t
  2565. _ctl_host_trace_buffer_enable_show(struct device *cdev,
  2566. struct device_attribute *attr, char *buf)
  2567. {
  2568. struct Scsi_Host *shost = class_to_shost(cdev);
  2569. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2570. if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
  2571. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2572. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
  2573. return snprintf(buf, PAGE_SIZE, "off\n");
  2574. else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2575. MPT2_DIAG_BUFFER_IS_RELEASED))
  2576. return snprintf(buf, PAGE_SIZE, "release\n");
  2577. else
  2578. return snprintf(buf, PAGE_SIZE, "post\n");
  2579. }
  2580. static ssize_t
  2581. _ctl_host_trace_buffer_enable_store(struct device *cdev,
  2582. struct device_attribute *attr, const char *buf, size_t count)
  2583. {
  2584. struct Scsi_Host *shost = class_to_shost(cdev);
  2585. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  2586. char str[10] = "";
  2587. struct mpt2_diag_register diag_register;
  2588. u8 issue_reset = 0;
  2589. if (sscanf(buf, "%9s", str) != 1)
  2590. return -EINVAL;
  2591. if (!strcmp(str, "post")) {
  2592. /* exit out if host buffers are already posted */
  2593. if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
  2594. (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2595. MPT2_DIAG_BUFFER_IS_REGISTERED) &&
  2596. ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2597. MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
  2598. goto out;
  2599. memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
  2600. printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
  2601. ioc->name);
  2602. diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
  2603. diag_register.requested_buffer_size = (1024 * 1024);
  2604. diag_register.unique_id = 0x7075900;
  2605. ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
  2606. _ctl_diag_register_2(ioc, &diag_register);
  2607. } else if (!strcmp(str, "release")) {
  2608. /* exit out if host buffers are already released */
  2609. if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
  2610. goto out;
  2611. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2612. MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
  2613. goto out;
  2614. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  2615. MPT2_DIAG_BUFFER_IS_RELEASED))
  2616. goto out;
  2617. printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
  2618. ioc->name);
  2619. _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
  2620. }
  2621. out:
  2622. return strlen(buf);
  2623. }
  2624. static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
  2625. _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
  2626. struct device_attribute *mpt2sas_host_attrs[] = {
  2627. &dev_attr_version_fw,
  2628. &dev_attr_version_bios,
  2629. &dev_attr_version_mpi,
  2630. &dev_attr_version_product,
  2631. &dev_attr_version_nvdata_persistent,
  2632. &dev_attr_version_nvdata_default,
  2633. &dev_attr_board_name,
  2634. &dev_attr_board_assembly,
  2635. &dev_attr_board_tracer,
  2636. &dev_attr_io_delay,
  2637. &dev_attr_device_delay,
  2638. &dev_attr_logging_level,
  2639. &dev_attr_fwfault_debug,
  2640. &dev_attr_fw_queue_depth,
  2641. &dev_attr_host_sas_address,
  2642. &dev_attr_ioc_reset_count,
  2643. &dev_attr_host_trace_buffer_size,
  2644. &dev_attr_host_trace_buffer,
  2645. &dev_attr_host_trace_buffer_enable,
  2646. &dev_attr_reply_queue_count,
  2647. &dev_attr_BRM_status,
  2648. NULL,
  2649. };
  2650. /**
  2651. * _ctl_device_sas_address_show - sas address
  2652. * @cdev - pointer to embedded class device
  2653. * @buf - the buffer returned
  2654. *
  2655. * This is the sas address for the target
  2656. *
  2657. * A sysfs 'read-only' shost attribute.
  2658. */
  2659. static ssize_t
  2660. _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
  2661. char *buf)
  2662. {
  2663. struct scsi_device *sdev = to_scsi_device(dev);
  2664. struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  2665. return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
  2666. (unsigned long long)sas_device_priv_data->sas_target->sas_address);
  2667. }
  2668. static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
  2669. /**
  2670. * _ctl_device_handle_show - device handle
  2671. * @cdev - pointer to embedded class device
  2672. * @buf - the buffer returned
  2673. *
  2674. * This is the firmware assigned device handle
  2675. *
  2676. * A sysfs 'read-only' shost attribute.
  2677. */
  2678. static ssize_t
  2679. _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
  2680. char *buf)
  2681. {
  2682. struct scsi_device *sdev = to_scsi_device(dev);
  2683. struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
  2684. return snprintf(buf, PAGE_SIZE, "0x%04x\n",
  2685. sas_device_priv_data->sas_target->handle);
  2686. }
  2687. static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
  2688. struct device_attribute *mpt2sas_dev_attrs[] = {
  2689. &dev_attr_sas_address,
  2690. &dev_attr_sas_device_handle,
  2691. NULL,
  2692. };
  2693. static const struct file_operations ctl_fops = {
  2694. .owner = THIS_MODULE,
  2695. .unlocked_ioctl = _ctl_ioctl,
  2696. .poll = _ctl_poll,
  2697. .fasync = _ctl_fasync,
  2698. #ifdef CONFIG_COMPAT
  2699. .compat_ioctl = _ctl_ioctl_compat,
  2700. #endif
  2701. .llseek = noop_llseek,
  2702. };
  2703. static struct miscdevice ctl_dev = {
  2704. .minor = MPT2SAS_MINOR,
  2705. .name = MPT2SAS_DEV_NAME,
  2706. .fops = &ctl_fops,
  2707. };
  2708. /**
  2709. * mpt2sas_ctl_init - main entry point for ctl.
  2710. *
  2711. */
  2712. void
  2713. mpt2sas_ctl_init(void)
  2714. {
  2715. async_queue = NULL;
  2716. if (misc_register(&ctl_dev) < 0)
  2717. printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
  2718. MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
  2719. init_waitqueue_head(&ctl_poll_wait);
  2720. }
  2721. /**
  2722. * mpt2sas_ctl_exit - exit point for ctl
  2723. *
  2724. */
  2725. void
  2726. mpt2sas_ctl_exit(void)
  2727. {
  2728. struct MPT2SAS_ADAPTER *ioc;
  2729. int i;
  2730. list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
  2731. /* free memory associated to diag buffers */
  2732. for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
  2733. if (!ioc->diag_buffer[i])
  2734. continue;
  2735. pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
  2736. ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
  2737. ioc->diag_buffer[i] = NULL;
  2738. ioc->diag_buffer_status[i] = 0;
  2739. }
  2740. kfree(ioc->event_log);
  2741. }
  2742. misc_deregister(&ctl_dev);
  2743. }