mpt2sas_ctl.c 85 KB

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