mpt2sas_ctl.c 85 KB

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