mptspi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*
  2. * linux/drivers/message/fusion/mptspi.c
  3. * For use with LSI Logic PCI chip/adapter(s)
  4. * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2007 LSI Logic Corporation
  7. * (mailto:mpt_linux_developer@lsi.com)
  8. *
  9. */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  11. /*
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; version 2 of the License.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  42. #include "linux_compat.h" /* linux-2.6 tweaks */
  43. #include <linux/module.h>
  44. #include <linux/kernel.h>
  45. #include <linux/init.h>
  46. #include <linux/errno.h>
  47. #include <linux/kdev_t.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/delay.h> /* for mdelay */
  50. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  51. #include <linux/reboot.h> /* notifier code */
  52. #include <linux/workqueue.h>
  53. #include <linux/raid_class.h>
  54. #include <scsi/scsi.h>
  55. #include <scsi/scsi_cmnd.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_host.h>
  58. #include <scsi/scsi_tcq.h>
  59. #include <scsi/scsi_transport.h>
  60. #include <scsi/scsi_transport_spi.h>
  61. #include <scsi/scsi_dbg.h>
  62. #include "mptbase.h"
  63. #include "mptscsih.h"
  64. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  65. #define my_NAME "Fusion MPT SPI Host driver"
  66. #define my_VERSION MPT_LINUX_VERSION_COMMON
  67. #define MYNAM "mptspi"
  68. MODULE_AUTHOR(MODULEAUTHOR);
  69. MODULE_DESCRIPTION(my_NAME);
  70. MODULE_LICENSE("GPL");
  71. MODULE_VERSION(my_VERSION);
  72. /* Command line args */
  73. static int mpt_saf_te = MPTSCSIH_SAF_TE;
  74. module_param(mpt_saf_te, int, 0);
  75. MODULE_PARM_DESC(mpt_saf_te, " Force enabling SEP Processor: enable=1 (default=MPTSCSIH_SAF_TE=0)");
  76. static void mptspi_write_offset(struct scsi_target *, int);
  77. static void mptspi_write_width(struct scsi_target *, int);
  78. static int mptspi_write_spi_device_pg1(struct scsi_target *,
  79. struct _CONFIG_PAGE_SCSI_DEVICE_1 *);
  80. static struct scsi_transport_template *mptspi_transport_template = NULL;
  81. static int mptspiDoneCtx = -1;
  82. static int mptspiTaskCtx = -1;
  83. static int mptspiInternalCtx = -1; /* Used only for internal commands */
  84. /**
  85. * mptspi_setTargetNegoParms - Update the target negotiation
  86. * parameters based on the the Inquiry data, adapter capabilities,
  87. * and NVRAM settings
  88. *
  89. * @hd: Pointer to a SCSI Host Structure
  90. * @vtarget: per target private data
  91. * @sdev: SCSI device
  92. *
  93. **/
  94. static void
  95. mptspi_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtTarget *target,
  96. struct scsi_device *sdev)
  97. {
  98. SpiCfgData *pspi_data = &hd->ioc->spi_data;
  99. int id = (int) target->id;
  100. int nvram;
  101. u8 width = MPT_NARROW;
  102. u8 factor = MPT_ASYNC;
  103. u8 offset = 0;
  104. u8 nfactor;
  105. u8 noQas = 1;
  106. target->negoFlags = pspi_data->noQas;
  107. if (sdev->scsi_level < SCSI_2) {
  108. width = 0;
  109. factor = MPT_ULTRA2;
  110. offset = pspi_data->maxSyncOffset;
  111. target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
  112. } else {
  113. if (scsi_device_wide(sdev))
  114. width = 1;
  115. if (scsi_device_sync(sdev)) {
  116. factor = pspi_data->minSyncFactor;
  117. if (!scsi_device_dt(sdev))
  118. factor = MPT_ULTRA2;
  119. else {
  120. if (!scsi_device_ius(sdev) &&
  121. !scsi_device_qas(sdev))
  122. factor = MPT_ULTRA160;
  123. else {
  124. factor = MPT_ULTRA320;
  125. if (scsi_device_qas(sdev)) {
  126. ddvprintk((KERN_INFO "Enabling QAS due to byte56=%02x on id=%d!\n", scsi_device_qas(sdev), id));
  127. noQas = 0;
  128. }
  129. if (sdev->type == TYPE_TAPE &&
  130. scsi_device_ius(sdev))
  131. target->negoFlags |= MPT_TAPE_NEGO_IDP;
  132. }
  133. }
  134. offset = pspi_data->maxSyncOffset;
  135. /* If RAID, never disable QAS
  136. * else if non RAID, do not disable
  137. * QAS if bit 1 is set
  138. * bit 1 QAS support, non-raid only
  139. * bit 0 IU support
  140. */
  141. if (target->raidVolume == 1)
  142. noQas = 0;
  143. } else {
  144. factor = MPT_ASYNC;
  145. offset = 0;
  146. }
  147. }
  148. if (!sdev->tagged_supported)
  149. target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
  150. /* Update tflags based on NVRAM settings. (SCSI only)
  151. */
  152. if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  153. nvram = pspi_data->nvram[id];
  154. nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
  155. if (width)
  156. width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  157. if (offset > 0) {
  158. /* Ensure factor is set to the
  159. * maximum of: adapter, nvram, inquiry
  160. */
  161. if (nfactor) {
  162. if (nfactor < pspi_data->minSyncFactor )
  163. nfactor = pspi_data->minSyncFactor;
  164. factor = max(factor, nfactor);
  165. if (factor == MPT_ASYNC)
  166. offset = 0;
  167. } else {
  168. offset = 0;
  169. factor = MPT_ASYNC;
  170. }
  171. } else {
  172. factor = MPT_ASYNC;
  173. }
  174. }
  175. /* Make sure data is consistent
  176. */
  177. if ((!width) && (factor < MPT_ULTRA2))
  178. factor = MPT_ULTRA2;
  179. /* Save the data to the target structure.
  180. */
  181. target->minSyncFactor = factor;
  182. target->maxOffset = offset;
  183. target->maxWidth = width;
  184. target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;
  185. /* Disable unused features.
  186. */
  187. if (!width)
  188. target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
  189. if (!offset)
  190. target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
  191. if ( factor > MPT_ULTRA320 )
  192. noQas = 0;
  193. if (noQas && (pspi_data->noQas == 0)) {
  194. pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
  195. target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
  196. /* Disable QAS in a mixed configuration case
  197. */
  198. ddvprintk((KERN_INFO "Disabling QAS due to noQas=%02x on id=%d!\n", noQas, id));
  199. }
  200. }
  201. /**
  202. * mptspi_writeIOCPage4 - write IOC Page 4
  203. * @hd: Pointer to a SCSI Host Structure
  204. * @channel:
  205. * @id: write IOC Page4 for this ID & Bus
  206. *
  207. * Return: -EAGAIN if unable to obtain a Message Frame
  208. * or 0 if success.
  209. *
  210. * Remark: We do not wait for a return, write pages sequentially.
  211. **/
  212. static int
  213. mptspi_writeIOCPage4(MPT_SCSI_HOST *hd, u8 channel , u8 id)
  214. {
  215. MPT_ADAPTER *ioc = hd->ioc;
  216. Config_t *pReq;
  217. IOCPage4_t *IOCPage4Ptr;
  218. MPT_FRAME_HDR *mf;
  219. dma_addr_t dataDma;
  220. u16 req_idx;
  221. u32 frameOffset;
  222. u32 flagsLength;
  223. int ii;
  224. /* Get a MF for this command.
  225. */
  226. if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
  227. dfailprintk((MYIOC_s_WARN_FMT "writeIOCPage4 : no msg frames!\n",
  228. ioc->name));
  229. return -EAGAIN;
  230. }
  231. /* Set the request and the data pointers.
  232. * Place data at end of MF.
  233. */
  234. pReq = (Config_t *)mf;
  235. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  236. frameOffset = ioc->req_sz - sizeof(IOCPage4_t);
  237. /* Complete the request frame (same for all requests).
  238. */
  239. pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  240. pReq->Reserved = 0;
  241. pReq->ChainOffset = 0;
  242. pReq->Function = MPI_FUNCTION_CONFIG;
  243. pReq->ExtPageLength = 0;
  244. pReq->ExtPageType = 0;
  245. pReq->MsgFlags = 0;
  246. for (ii=0; ii < 8; ii++) {
  247. pReq->Reserved2[ii] = 0;
  248. }
  249. IOCPage4Ptr = ioc->spi_data.pIocPg4;
  250. dataDma = ioc->spi_data.IocPg4_dma;
  251. ii = IOCPage4Ptr->ActiveSEP++;
  252. IOCPage4Ptr->SEP[ii].SEPTargetID = id;
  253. IOCPage4Ptr->SEP[ii].SEPBus = channel;
  254. pReq->Header = IOCPage4Ptr->Header;
  255. pReq->PageAddress = cpu_to_le32(id | (channel << 8 ));
  256. /* Add a SGE to the config request.
  257. */
  258. flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
  259. (IOCPage4Ptr->Header.PageLength + ii) * 4;
  260. mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
  261. ddvprintk((MYIOC_s_INFO_FMT
  262. "writeIOCPage4: MaxSEP=%d ActiveSEP=%d id=%d bus=%d\n",
  263. ioc->name, IOCPage4Ptr->MaxSEP, IOCPage4Ptr->ActiveSEP, id, channel));
  264. mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
  265. return 0;
  266. }
  267. /**
  268. * mptspi_initTarget - Target, LUN alloc/free functionality.
  269. * @hd: Pointer to MPT_SCSI_HOST structure
  270. * @vtarget: per target private data
  271. * @sdev: SCSI device
  272. *
  273. * NOTE: It's only SAFE to call this routine if data points to
  274. * sane & valid STANDARD INQUIRY data!
  275. *
  276. * Allocate and initialize memory for this target.
  277. * Save inquiry data.
  278. *
  279. **/
  280. static void
  281. mptspi_initTarget(MPT_SCSI_HOST *hd, VirtTarget *vtarget,
  282. struct scsi_device *sdev)
  283. {
  284. /* Is LUN supported? If so, upper 2 bits will be 0
  285. * in first byte of inquiry data.
  286. */
  287. if (sdev->inq_periph_qual != 0)
  288. return;
  289. if (vtarget == NULL)
  290. return;
  291. vtarget->type = sdev->type;
  292. if ((sdev->type == TYPE_PROCESSOR) && (hd->ioc->spi_data.Saf_Te)) {
  293. /* Treat all Processors as SAF-TE if
  294. * command line option is set */
  295. vtarget->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
  296. mptspi_writeIOCPage4(hd, vtarget->channel, vtarget->id);
  297. }else if ((sdev->type == TYPE_PROCESSOR) &&
  298. !(vtarget->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
  299. if (sdev->inquiry_len > 49 ) {
  300. if (sdev->inquiry[44] == 'S' &&
  301. sdev->inquiry[45] == 'A' &&
  302. sdev->inquiry[46] == 'F' &&
  303. sdev->inquiry[47] == '-' &&
  304. sdev->inquiry[48] == 'T' &&
  305. sdev->inquiry[49] == 'E' ) {
  306. vtarget->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
  307. mptspi_writeIOCPage4(hd, vtarget->channel, vtarget->id);
  308. }
  309. }
  310. }
  311. mptspi_setTargetNegoParms(hd, vtarget, sdev);
  312. }
  313. /**
  314. * mptspi_is_raid - Determines whether target is belonging to volume
  315. * @hd: Pointer to a SCSI HOST structure
  316. * @id: target device id
  317. *
  318. * Return:
  319. * non-zero = true
  320. * zero = false
  321. *
  322. */
  323. static int
  324. mptspi_is_raid(struct _MPT_SCSI_HOST *hd, u32 id)
  325. {
  326. int i, rc = 0;
  327. if (!hd->ioc->raid_data.pIocPg2)
  328. goto out;
  329. if (!hd->ioc->raid_data.pIocPg2->NumActiveVolumes)
  330. goto out;
  331. for (i=0; i < hd->ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  332. if (hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID == id) {
  333. rc = 1;
  334. goto out;
  335. }
  336. }
  337. out:
  338. return rc;
  339. }
  340. static int mptspi_target_alloc(struct scsi_target *starget)
  341. {
  342. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  343. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
  344. VirtTarget *vtarget;
  345. if (hd == NULL)
  346. return -ENODEV;
  347. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  348. if (!vtarget)
  349. return -ENOMEM;
  350. vtarget->ioc_id = hd->ioc->id;
  351. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  352. vtarget->id = (u8)starget->id;
  353. vtarget->channel = (u8)starget->channel;
  354. vtarget->starget = starget;
  355. starget->hostdata = vtarget;
  356. if (starget->channel == 1) {
  357. if (mptscsih_is_phys_disk(hd->ioc, 0, starget->id) == 0)
  358. return 0;
  359. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  360. /* The real channel for this device is zero */
  361. vtarget->channel = 0;
  362. /* The actual physdisknum (for RAID passthrough) */
  363. vtarget->id = mptscsih_raid_id_to_num(hd->ioc, 0,
  364. starget->id);
  365. }
  366. if (starget->channel == 0 &&
  367. mptspi_is_raid(hd, starget->id)) {
  368. vtarget->raidVolume = 1;
  369. ddvprintk((KERN_INFO
  370. "RAID Volume @ channel=%d id=%d\n", starget->channel,
  371. starget->id));
  372. }
  373. if (hd->ioc->spi_data.nvram &&
  374. hd->ioc->spi_data.nvram[starget->id] != MPT_HOST_NVRAM_INVALID) {
  375. u32 nvram = hd->ioc->spi_data.nvram[starget->id];
  376. spi_min_period(starget) = (nvram & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
  377. spi_max_width(starget) = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  378. } else {
  379. spi_min_period(starget) = hd->ioc->spi_data.minSyncFactor;
  380. spi_max_width(starget) = hd->ioc->spi_data.maxBusWidth;
  381. }
  382. spi_max_offset(starget) = hd->ioc->spi_data.maxSyncOffset;
  383. spi_offset(starget) = 0;
  384. mptspi_write_width(starget, 0);
  385. return 0;
  386. }
  387. void
  388. mptspi_target_destroy(struct scsi_target *starget)
  389. {
  390. if (starget->hostdata)
  391. kfree(starget->hostdata);
  392. starget->hostdata = NULL;
  393. }
  394. /**
  395. * mptspi_print_write_nego - negotiation parameters debug info that is being sent
  396. * @hd: Pointer to a SCSI HOST structure
  397. * @starget: SCSI target
  398. * @ii: negotiation parameters
  399. *
  400. */
  401. static void
  402. mptspi_print_write_nego(struct _MPT_SCSI_HOST *hd, struct scsi_target *starget, u32 ii)
  403. {
  404. ddvprintk((MYIOC_s_INFO_FMT "id=%d Requested = 0x%08x"
  405. " ( %s factor = 0x%02x @ offset = 0x%02x %s%s%s%s%s%s%s%s)\n",
  406. hd->ioc->name, starget->id, ii,
  407. ii & MPI_SCSIDEVPAGE0_NP_WIDE ? "Wide ": "",
  408. ((ii >> 8) & 0xFF), ((ii >> 16) & 0xFF),
  409. ii & MPI_SCSIDEVPAGE0_NP_IU ? "IU ": "",
  410. ii & MPI_SCSIDEVPAGE0_NP_DT ? "DT ": "",
  411. ii & MPI_SCSIDEVPAGE0_NP_QAS ? "QAS ": "",
  412. ii & MPI_SCSIDEVPAGE0_NP_HOLD_MCS ? "HOLDMCS ": "",
  413. ii & MPI_SCSIDEVPAGE0_NP_WR_FLOW ? "WRFLOW ": "",
  414. ii & MPI_SCSIDEVPAGE0_NP_RD_STRM ? "RDSTRM ": "",
  415. ii & MPI_SCSIDEVPAGE0_NP_RTI ? "RTI ": "",
  416. ii & MPI_SCSIDEVPAGE0_NP_PCOMP_EN ? "PCOMP ": ""));
  417. }
  418. /**
  419. * mptspi_print_read_nego - negotiation parameters debug info that is being read
  420. * @hd: Pointer to a SCSI HOST structure
  421. * @starget: SCSI target
  422. * @ii: negotiation parameters
  423. *
  424. */
  425. static void
  426. mptspi_print_read_nego(struct _MPT_SCSI_HOST *hd, struct scsi_target *starget, u32 ii)
  427. {
  428. ddvprintk((MYIOC_s_INFO_FMT "id=%d Read = 0x%08x"
  429. " ( %s factor = 0x%02x @ offset = 0x%02x %s%s%s%s%s%s%s%s)\n",
  430. hd->ioc->name, starget->id, ii,
  431. ii & MPI_SCSIDEVPAGE0_NP_WIDE ? "Wide ": "",
  432. ((ii >> 8) & 0xFF), ((ii >> 16) & 0xFF),
  433. ii & MPI_SCSIDEVPAGE0_NP_IU ? "IU ": "",
  434. ii & MPI_SCSIDEVPAGE0_NP_DT ? "DT ": "",
  435. ii & MPI_SCSIDEVPAGE0_NP_QAS ? "QAS ": "",
  436. ii & MPI_SCSIDEVPAGE0_NP_HOLD_MCS ? "HOLDMCS ": "",
  437. ii & MPI_SCSIDEVPAGE0_NP_WR_FLOW ? "WRFLOW ": "",
  438. ii & MPI_SCSIDEVPAGE0_NP_RD_STRM ? "RDSTRM ": "",
  439. ii & MPI_SCSIDEVPAGE0_NP_RTI ? "RTI ": "",
  440. ii & MPI_SCSIDEVPAGE0_NP_PCOMP_EN ? "PCOMP ": ""));
  441. }
  442. static int mptspi_read_spi_device_pg0(struct scsi_target *starget,
  443. struct _CONFIG_PAGE_SCSI_DEVICE_0 *pass_pg0)
  444. {
  445. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  446. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
  447. struct _MPT_ADAPTER *ioc = hd->ioc;
  448. struct _CONFIG_PAGE_SCSI_DEVICE_0 *pg0;
  449. dma_addr_t pg0_dma;
  450. int size;
  451. struct _x_config_parms cfg;
  452. struct _CONFIG_PAGE_HEADER hdr;
  453. int err = -EBUSY;
  454. /* No SPI parameters for RAID devices */
  455. if (starget->channel == 0 &&
  456. mptspi_is_raid(hd, starget->id))
  457. return -1;
  458. size = ioc->spi_data.sdp0length * 4;
  459. /*
  460. if (ioc->spi_data.sdp0length & 1)
  461. size += size + 4;
  462. size += 2048;
  463. */
  464. pg0 = dma_alloc_coherent(&ioc->pcidev->dev, size, &pg0_dma, GFP_KERNEL);
  465. if (pg0 == NULL) {
  466. starget_printk(KERN_ERR, starget, "dma_alloc_coherent for parameters failed\n");
  467. return -EINVAL;
  468. }
  469. memset(&hdr, 0, sizeof(hdr));
  470. hdr.PageVersion = ioc->spi_data.sdp0version;
  471. hdr.PageLength = ioc->spi_data.sdp0length;
  472. hdr.PageNumber = 0;
  473. hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  474. memset(&cfg, 0, sizeof(cfg));
  475. cfg.cfghdr.hdr = &hdr;
  476. cfg.physAddr = pg0_dma;
  477. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  478. cfg.dir = 0;
  479. cfg.pageAddr = starget->id;
  480. if (mpt_config(ioc, &cfg)) {
  481. starget_printk(KERN_ERR, starget, "mpt_config failed\n");
  482. goto out_free;
  483. }
  484. err = 0;
  485. memcpy(pass_pg0, pg0, size);
  486. mptspi_print_read_nego(hd, starget, le32_to_cpu(pg0->NegotiatedParameters));
  487. out_free:
  488. dma_free_coherent(&ioc->pcidev->dev, size, pg0, pg0_dma);
  489. return err;
  490. }
  491. static u32 mptspi_getRP(struct scsi_target *starget)
  492. {
  493. u32 nego = 0;
  494. nego |= spi_iu(starget) ? MPI_SCSIDEVPAGE1_RP_IU : 0;
  495. nego |= spi_dt(starget) ? MPI_SCSIDEVPAGE1_RP_DT : 0;
  496. nego |= spi_qas(starget) ? MPI_SCSIDEVPAGE1_RP_QAS : 0;
  497. nego |= spi_hold_mcs(starget) ? MPI_SCSIDEVPAGE1_RP_HOLD_MCS : 0;
  498. nego |= spi_wr_flow(starget) ? MPI_SCSIDEVPAGE1_RP_WR_FLOW : 0;
  499. nego |= spi_rd_strm(starget) ? MPI_SCSIDEVPAGE1_RP_RD_STRM : 0;
  500. nego |= spi_rti(starget) ? MPI_SCSIDEVPAGE1_RP_RTI : 0;
  501. nego |= spi_pcomp_en(starget) ? MPI_SCSIDEVPAGE1_RP_PCOMP_EN : 0;
  502. nego |= (spi_period(starget) << MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD) & MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
  503. nego |= (spi_offset(starget) << MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET) & MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
  504. nego |= spi_width(starget) ? MPI_SCSIDEVPAGE1_RP_WIDE : 0;
  505. return nego;
  506. }
  507. static void mptspi_read_parameters(struct scsi_target *starget)
  508. {
  509. int nego;
  510. struct _CONFIG_PAGE_SCSI_DEVICE_0 pg0;
  511. mptspi_read_spi_device_pg0(starget, &pg0);
  512. nego = le32_to_cpu(pg0.NegotiatedParameters);
  513. spi_iu(starget) = (nego & MPI_SCSIDEVPAGE0_NP_IU) ? 1 : 0;
  514. spi_dt(starget) = (nego & MPI_SCSIDEVPAGE0_NP_DT) ? 1 : 0;
  515. spi_qas(starget) = (nego & MPI_SCSIDEVPAGE0_NP_QAS) ? 1 : 0;
  516. spi_wr_flow(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WR_FLOW) ? 1 : 0;
  517. spi_rd_strm(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RD_STRM) ? 1 : 0;
  518. spi_rti(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RTI) ? 1 : 0;
  519. spi_pcomp_en(starget) = (nego & MPI_SCSIDEVPAGE0_NP_PCOMP_EN) ? 1 : 0;
  520. spi_hold_mcs(starget) = (nego & MPI_SCSIDEVPAGE0_NP_HOLD_MCS) ? 1 : 0;
  521. spi_period(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_PERIOD;
  522. spi_offset(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_OFFSET;
  523. spi_width(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WIDE) ? 1 : 0;
  524. }
  525. static int
  526. mptscsih_quiesce_raid(MPT_SCSI_HOST *hd, int quiesce, u8 channel, u8 id)
  527. {
  528. MpiRaidActionRequest_t *pReq;
  529. MPT_FRAME_HDR *mf;
  530. /* Get and Populate a free Frame
  531. */
  532. if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
  533. ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
  534. hd->ioc->name));
  535. return -EAGAIN;
  536. }
  537. pReq = (MpiRaidActionRequest_t *)mf;
  538. if (quiesce)
  539. pReq->Action = MPI_RAID_ACTION_QUIESCE_PHYS_IO;
  540. else
  541. pReq->Action = MPI_RAID_ACTION_ENABLE_PHYS_IO;
  542. pReq->Reserved1 = 0;
  543. pReq->ChainOffset = 0;
  544. pReq->Function = MPI_FUNCTION_RAID_ACTION;
  545. pReq->VolumeID = id;
  546. pReq->VolumeBus = channel;
  547. pReq->PhysDiskNum = 0;
  548. pReq->MsgFlags = 0;
  549. pReq->Reserved2 = 0;
  550. pReq->ActionDataWord = 0; /* Reserved for this action */
  551. mpt_add_sge((char *)&pReq->ActionDataSGE,
  552. MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
  553. ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action=%x channel=%d id=%d\n",
  554. hd->ioc->name, pReq->Action, channel, id));
  555. hd->pLocal = NULL;
  556. hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
  557. hd->scandv_wait_done = 0;
  558. /* Save cmd pointer, for resource free if timeout or
  559. * FW reload occurs
  560. */
  561. hd->cmdPtr = mf;
  562. add_timer(&hd->timer);
  563. mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
  564. wait_event(hd->scandv_waitq, hd->scandv_wait_done);
  565. if ((hd->pLocal == NULL) || (hd->pLocal->completion != 0))
  566. return -1;
  567. return 0;
  568. }
  569. static void mptspi_dv_device(struct _MPT_SCSI_HOST *hd,
  570. struct scsi_device *sdev)
  571. {
  572. VirtTarget *vtarget = scsi_target(sdev)->hostdata;
  573. /* no DV on RAID devices */
  574. if (sdev->channel == 0 &&
  575. mptspi_is_raid(hd, sdev->id))
  576. return;
  577. /* If this is a piece of a RAID, then quiesce first */
  578. if (sdev->channel == 1 &&
  579. mptscsih_quiesce_raid(hd, 1, vtarget->channel, vtarget->id) < 0) {
  580. starget_printk(KERN_ERR, scsi_target(sdev),
  581. "Integrated RAID quiesce failed\n");
  582. return;
  583. }
  584. spi_dv_device(sdev);
  585. if (sdev->channel == 1 &&
  586. mptscsih_quiesce_raid(hd, 0, vtarget->channel, vtarget->id) < 0)
  587. starget_printk(KERN_ERR, scsi_target(sdev),
  588. "Integrated RAID resume failed\n");
  589. mptspi_read_parameters(sdev->sdev_target);
  590. spi_display_xfer_agreement(sdev->sdev_target);
  591. mptspi_read_parameters(sdev->sdev_target);
  592. }
  593. static int mptspi_slave_alloc(struct scsi_device *sdev)
  594. {
  595. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
  596. VirtTarget *vtarget;
  597. VirtDevice *vdev;
  598. struct scsi_target *starget;
  599. if (sdev->channel == 1 &&
  600. mptscsih_is_phys_disk(hd->ioc, 0, sdev->id) == 0)
  601. return -ENXIO;
  602. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  603. if (!vdev) {
  604. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  605. hd->ioc->name, sizeof(VirtDevice));
  606. return -ENOMEM;
  607. }
  608. vdev->lun = sdev->lun;
  609. sdev->hostdata = vdev;
  610. starget = scsi_target(sdev);
  611. vtarget = starget->hostdata;
  612. vdev->vtarget = vtarget;
  613. vtarget->num_luns++;
  614. if (sdev->channel == 1)
  615. sdev->no_uld_attach = 1;
  616. return 0;
  617. }
  618. static int mptspi_slave_configure(struct scsi_device *sdev)
  619. {
  620. struct _MPT_SCSI_HOST *hd =
  621. (struct _MPT_SCSI_HOST *)sdev->host->hostdata;
  622. VirtTarget *vtarget = scsi_target(sdev)->hostdata;
  623. int ret = mptscsih_slave_configure(sdev);
  624. if (ret)
  625. return ret;
  626. mptspi_initTarget(hd, vtarget, sdev);
  627. ddvprintk((MYIOC_s_INFO_FMT "id=%d min_period=0x%02x"
  628. " max_offset=0x%02x max_width=%d\n", hd->ioc->name,
  629. sdev->id, spi_min_period(scsi_target(sdev)),
  630. spi_max_offset(scsi_target(sdev)),
  631. spi_max_width(scsi_target(sdev))));
  632. if ((sdev->channel == 1 ||
  633. !(mptspi_is_raid(hd, sdev->id))) &&
  634. !spi_initial_dv(sdev->sdev_target))
  635. mptspi_dv_device(hd, sdev);
  636. return 0;
  637. }
  638. static int
  639. mptspi_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  640. {
  641. struct _MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata;
  642. VirtDevice *vdev = SCpnt->device->hostdata;
  643. if (!vdev || !vdev->vtarget) {
  644. SCpnt->result = DID_NO_CONNECT << 16;
  645. done(SCpnt);
  646. return 0;
  647. }
  648. if (SCpnt->device->channel == 1 &&
  649. mptscsih_is_phys_disk(hd->ioc, 0, SCpnt->device->id) == 0) {
  650. SCpnt->result = DID_NO_CONNECT << 16;
  651. done(SCpnt);
  652. return 0;
  653. }
  654. #ifdef MPT_DEBUG_DV
  655. if (spi_dv_pending(scsi_target(SCpnt->device)))
  656. scsi_print_command(SCpnt);
  657. #endif
  658. return mptscsih_qcmd(SCpnt,done);
  659. }
  660. static void mptspi_slave_destroy(struct scsi_device *sdev)
  661. {
  662. struct scsi_target *starget = scsi_target(sdev);
  663. VirtTarget *vtarget = starget->hostdata;
  664. VirtDevice *vdevice = sdev->hostdata;
  665. /* Will this be the last lun on a non-raid device? */
  666. if (vtarget->num_luns == 1 && vdevice->configured_lun) {
  667. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  668. /* Async Narrow */
  669. pg1.RequestedParameters = 0;
  670. pg1.Reserved = 0;
  671. pg1.Configuration = 0;
  672. mptspi_write_spi_device_pg1(starget, &pg1);
  673. }
  674. mptscsih_slave_destroy(sdev);
  675. }
  676. static struct scsi_host_template mptspi_driver_template = {
  677. .module = THIS_MODULE,
  678. .proc_name = "mptspi",
  679. .proc_info = mptscsih_proc_info,
  680. .name = "MPT SPI Host",
  681. .info = mptscsih_info,
  682. .queuecommand = mptspi_qcmd,
  683. .target_alloc = mptspi_target_alloc,
  684. .slave_alloc = mptspi_slave_alloc,
  685. .slave_configure = mptspi_slave_configure,
  686. .target_destroy = mptspi_target_destroy,
  687. .slave_destroy = mptspi_slave_destroy,
  688. .change_queue_depth = mptscsih_change_queue_depth,
  689. .eh_abort_handler = mptscsih_abort,
  690. .eh_device_reset_handler = mptscsih_dev_reset,
  691. .eh_bus_reset_handler = mptscsih_bus_reset,
  692. .eh_host_reset_handler = mptscsih_host_reset,
  693. .bios_param = mptscsih_bios_param,
  694. .can_queue = MPT_SCSI_CAN_QUEUE,
  695. .this_id = -1,
  696. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  697. .max_sectors = 8192,
  698. .cmd_per_lun = 7,
  699. .use_clustering = ENABLE_CLUSTERING,
  700. };
  701. static int mptspi_write_spi_device_pg1(struct scsi_target *starget,
  702. struct _CONFIG_PAGE_SCSI_DEVICE_1 *pass_pg1)
  703. {
  704. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  705. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
  706. struct _MPT_ADAPTER *ioc = hd->ioc;
  707. struct _CONFIG_PAGE_SCSI_DEVICE_1 *pg1;
  708. dma_addr_t pg1_dma;
  709. int size;
  710. struct _x_config_parms cfg;
  711. struct _CONFIG_PAGE_HEADER hdr;
  712. int err = -EBUSY;
  713. /* don't allow updating nego parameters on RAID devices */
  714. if (starget->channel == 0 &&
  715. mptspi_is_raid(hd, starget->id))
  716. return -1;
  717. size = ioc->spi_data.sdp1length * 4;
  718. pg1 = dma_alloc_coherent(&ioc->pcidev->dev, size, &pg1_dma, GFP_KERNEL);
  719. if (pg1 == NULL) {
  720. starget_printk(KERN_ERR, starget, "dma_alloc_coherent for parameters failed\n");
  721. return -EINVAL;
  722. }
  723. memset(&hdr, 0, sizeof(hdr));
  724. hdr.PageVersion = ioc->spi_data.sdp1version;
  725. hdr.PageLength = ioc->spi_data.sdp1length;
  726. hdr.PageNumber = 1;
  727. hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  728. memset(&cfg, 0, sizeof(cfg));
  729. cfg.cfghdr.hdr = &hdr;
  730. cfg.physAddr = pg1_dma;
  731. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  732. cfg.dir = 1;
  733. cfg.pageAddr = starget->id;
  734. memcpy(pg1, pass_pg1, size);
  735. pg1->Header.PageVersion = hdr.PageVersion;
  736. pg1->Header.PageLength = hdr.PageLength;
  737. pg1->Header.PageNumber = hdr.PageNumber;
  738. pg1->Header.PageType = hdr.PageType;
  739. mptspi_print_write_nego(hd, starget, le32_to_cpu(pg1->RequestedParameters));
  740. if (mpt_config(ioc, &cfg)) {
  741. starget_printk(KERN_ERR, starget, "mpt_config failed\n");
  742. goto out_free;
  743. }
  744. err = 0;
  745. out_free:
  746. dma_free_coherent(&ioc->pcidev->dev, size, pg1, pg1_dma);
  747. return err;
  748. }
  749. static void mptspi_write_offset(struct scsi_target *starget, int offset)
  750. {
  751. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  752. u32 nego;
  753. if (offset < 0)
  754. offset = 0;
  755. if (offset > 255)
  756. offset = 255;
  757. if (spi_offset(starget) == -1)
  758. mptspi_read_parameters(starget);
  759. spi_offset(starget) = offset;
  760. nego = mptspi_getRP(starget);
  761. pg1.RequestedParameters = cpu_to_le32(nego);
  762. pg1.Reserved = 0;
  763. pg1.Configuration = 0;
  764. mptspi_write_spi_device_pg1(starget, &pg1);
  765. }
  766. static void mptspi_write_period(struct scsi_target *starget, int period)
  767. {
  768. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  769. u32 nego;
  770. if (period < 8)
  771. period = 8;
  772. if (period > 255)
  773. period = 255;
  774. if (spi_period(starget) == -1)
  775. mptspi_read_parameters(starget);
  776. if (period == 8) {
  777. spi_iu(starget) = 1;
  778. spi_dt(starget) = 1;
  779. } else if (period == 9) {
  780. spi_dt(starget) = 1;
  781. }
  782. spi_period(starget) = period;
  783. nego = mptspi_getRP(starget);
  784. pg1.RequestedParameters = cpu_to_le32(nego);
  785. pg1.Reserved = 0;
  786. pg1.Configuration = 0;
  787. mptspi_write_spi_device_pg1(starget, &pg1);
  788. }
  789. static void mptspi_write_dt(struct scsi_target *starget, int dt)
  790. {
  791. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  792. u32 nego;
  793. if (spi_period(starget) == -1)
  794. mptspi_read_parameters(starget);
  795. if (!dt && spi_period(starget) < 10)
  796. spi_period(starget) = 10;
  797. spi_dt(starget) = dt;
  798. nego = mptspi_getRP(starget);
  799. pg1.RequestedParameters = cpu_to_le32(nego);
  800. pg1.Reserved = 0;
  801. pg1.Configuration = 0;
  802. mptspi_write_spi_device_pg1(starget, &pg1);
  803. }
  804. static void mptspi_write_iu(struct scsi_target *starget, int iu)
  805. {
  806. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  807. u32 nego;
  808. if (spi_period(starget) == -1)
  809. mptspi_read_parameters(starget);
  810. if (!iu && spi_period(starget) < 9)
  811. spi_period(starget) = 9;
  812. spi_iu(starget) = iu;
  813. nego = mptspi_getRP(starget);
  814. pg1.RequestedParameters = cpu_to_le32(nego);
  815. pg1.Reserved = 0;
  816. pg1.Configuration = 0;
  817. mptspi_write_spi_device_pg1(starget, &pg1);
  818. }
  819. #define MPTSPI_SIMPLE_TRANSPORT_PARM(parm) \
  820. static void mptspi_write_##parm(struct scsi_target *starget, int parm)\
  821. { \
  822. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1; \
  823. u32 nego; \
  824. \
  825. spi_##parm(starget) = parm; \
  826. \
  827. nego = mptspi_getRP(starget); \
  828. \
  829. pg1.RequestedParameters = cpu_to_le32(nego); \
  830. pg1.Reserved = 0; \
  831. pg1.Configuration = 0; \
  832. \
  833. mptspi_write_spi_device_pg1(starget, &pg1); \
  834. }
  835. MPTSPI_SIMPLE_TRANSPORT_PARM(rd_strm)
  836. MPTSPI_SIMPLE_TRANSPORT_PARM(wr_flow)
  837. MPTSPI_SIMPLE_TRANSPORT_PARM(rti)
  838. MPTSPI_SIMPLE_TRANSPORT_PARM(hold_mcs)
  839. MPTSPI_SIMPLE_TRANSPORT_PARM(pcomp_en)
  840. static void mptspi_write_qas(struct scsi_target *starget, int qas)
  841. {
  842. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  843. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  844. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
  845. VirtTarget *vtarget = starget->hostdata;
  846. u32 nego;
  847. if ((vtarget->negoFlags & MPT_TARGET_NO_NEGO_QAS) ||
  848. hd->ioc->spi_data.noQas)
  849. spi_qas(starget) = 0;
  850. else
  851. spi_qas(starget) = qas;
  852. nego = mptspi_getRP(starget);
  853. pg1.RequestedParameters = cpu_to_le32(nego);
  854. pg1.Reserved = 0;
  855. pg1.Configuration = 0;
  856. mptspi_write_spi_device_pg1(starget, &pg1);
  857. }
  858. static void mptspi_write_width(struct scsi_target *starget, int width)
  859. {
  860. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  861. u32 nego;
  862. if (!width) {
  863. spi_dt(starget) = 0;
  864. if (spi_period(starget) < 10)
  865. spi_period(starget) = 10;
  866. }
  867. spi_width(starget) = width;
  868. nego = mptspi_getRP(starget);
  869. pg1.RequestedParameters = cpu_to_le32(nego);
  870. pg1.Reserved = 0;
  871. pg1.Configuration = 0;
  872. mptspi_write_spi_device_pg1(starget, &pg1);
  873. }
  874. struct work_queue_wrapper {
  875. struct work_struct work;
  876. struct _MPT_SCSI_HOST *hd;
  877. int disk;
  878. };
  879. static void mpt_work_wrapper(struct work_struct *work)
  880. {
  881. struct work_queue_wrapper *wqw =
  882. container_of(work, struct work_queue_wrapper, work);
  883. struct _MPT_SCSI_HOST *hd = wqw->hd;
  884. struct Scsi_Host *shost = hd->ioc->sh;
  885. struct scsi_device *sdev;
  886. int disk = wqw->disk;
  887. struct _CONFIG_PAGE_IOC_3 *pg3;
  888. kfree(wqw);
  889. mpt_findImVolumes(hd->ioc);
  890. pg3 = hd->ioc->raid_data.pIocPg3;
  891. if (!pg3)
  892. return;
  893. shost_for_each_device(sdev,shost) {
  894. struct scsi_target *starget = scsi_target(sdev);
  895. VirtTarget *vtarget = starget->hostdata;
  896. /* only want to search RAID components */
  897. if (sdev->channel != 1)
  898. continue;
  899. /* The id is the raid PhysDiskNum, even if
  900. * starget->id is the actual target address */
  901. if(vtarget->id != disk)
  902. continue;
  903. starget_printk(KERN_INFO, vtarget->starget,
  904. "Integrated RAID requests DV of new device\n");
  905. mptspi_dv_device(hd, sdev);
  906. }
  907. shost_printk(KERN_INFO, shost,
  908. "Integrated RAID detects new device %d\n", disk);
  909. scsi_scan_target(&hd->ioc->sh->shost_gendev, 1, disk, 0, 1);
  910. }
  911. static void mpt_dv_raid(struct _MPT_SCSI_HOST *hd, int disk)
  912. {
  913. struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
  914. if (!wqw) {
  915. shost_printk(KERN_ERR, hd->ioc->sh,
  916. "Failed to act on RAID event for physical disk %d\n",
  917. disk);
  918. return;
  919. }
  920. INIT_WORK(&wqw->work, mpt_work_wrapper);
  921. wqw->hd = hd;
  922. wqw->disk = disk;
  923. schedule_work(&wqw->work);
  924. }
  925. static int
  926. mptspi_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
  927. {
  928. u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
  929. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
  930. if (hd && event == MPI_EVENT_INTEGRATED_RAID) {
  931. int reason
  932. = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
  933. if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
  934. int disk = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
  935. mpt_dv_raid(hd, disk);
  936. }
  937. }
  938. return mptscsih_event_process(ioc, pEvReply);
  939. }
  940. static int
  941. mptspi_deny_binding(struct scsi_target *starget)
  942. {
  943. struct _MPT_SCSI_HOST *hd =
  944. (struct _MPT_SCSI_HOST *)dev_to_shost(starget->dev.parent)->hostdata;
  945. return ((mptspi_is_raid(hd, starget->id)) &&
  946. starget->channel == 0) ? 1 : 0;
  947. }
  948. static struct spi_function_template mptspi_transport_functions = {
  949. .get_offset = mptspi_read_parameters,
  950. .set_offset = mptspi_write_offset,
  951. .show_offset = 1,
  952. .get_period = mptspi_read_parameters,
  953. .set_period = mptspi_write_period,
  954. .show_period = 1,
  955. .get_width = mptspi_read_parameters,
  956. .set_width = mptspi_write_width,
  957. .show_width = 1,
  958. .get_iu = mptspi_read_parameters,
  959. .set_iu = mptspi_write_iu,
  960. .show_iu = 1,
  961. .get_dt = mptspi_read_parameters,
  962. .set_dt = mptspi_write_dt,
  963. .show_dt = 1,
  964. .get_qas = mptspi_read_parameters,
  965. .set_qas = mptspi_write_qas,
  966. .show_qas = 1,
  967. .get_wr_flow = mptspi_read_parameters,
  968. .set_wr_flow = mptspi_write_wr_flow,
  969. .show_wr_flow = 1,
  970. .get_rd_strm = mptspi_read_parameters,
  971. .set_rd_strm = mptspi_write_rd_strm,
  972. .show_rd_strm = 1,
  973. .get_rti = mptspi_read_parameters,
  974. .set_rti = mptspi_write_rti,
  975. .show_rti = 1,
  976. .get_pcomp_en = mptspi_read_parameters,
  977. .set_pcomp_en = mptspi_write_pcomp_en,
  978. .show_pcomp_en = 1,
  979. .get_hold_mcs = mptspi_read_parameters,
  980. .set_hold_mcs = mptspi_write_hold_mcs,
  981. .show_hold_mcs = 1,
  982. .deny_binding = mptspi_deny_binding,
  983. };
  984. /****************************************************************************
  985. * Supported hardware
  986. */
  987. static struct pci_device_id mptspi_pci_table[] = {
  988. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_53C1030,
  989. PCI_ANY_ID, PCI_ANY_ID },
  990. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_53C1035,
  991. PCI_ANY_ID, PCI_ANY_ID },
  992. {0} /* Terminating entry */
  993. };
  994. MODULE_DEVICE_TABLE(pci, mptspi_pci_table);
  995. /*
  996. * renegotiate for a given target
  997. */
  998. static void
  999. mptspi_dv_renegotiate_work(struct work_struct *work)
  1000. {
  1001. struct work_queue_wrapper *wqw =
  1002. container_of(work, struct work_queue_wrapper, work);
  1003. struct _MPT_SCSI_HOST *hd = wqw->hd;
  1004. struct scsi_device *sdev;
  1005. kfree(wqw);
  1006. shost_for_each_device(sdev, hd->ioc->sh)
  1007. mptspi_dv_device(hd, sdev);
  1008. }
  1009. static void
  1010. mptspi_dv_renegotiate(struct _MPT_SCSI_HOST *hd)
  1011. {
  1012. struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
  1013. if (!wqw)
  1014. return;
  1015. INIT_WORK(&wqw->work, mptspi_dv_renegotiate_work);
  1016. wqw->hd = hd;
  1017. schedule_work(&wqw->work);
  1018. }
  1019. /*
  1020. * spi module reset handler
  1021. */
  1022. static int
  1023. mptspi_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1024. {
  1025. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
  1026. int rc;
  1027. rc = mptscsih_ioc_reset(ioc, reset_phase);
  1028. if (reset_phase == MPT_IOC_POST_RESET)
  1029. mptspi_dv_renegotiate(hd);
  1030. return rc;
  1031. }
  1032. #ifdef CONFIG_PM
  1033. /*
  1034. * spi module resume handler
  1035. */
  1036. static int
  1037. mptspi_resume(struct pci_dev *pdev)
  1038. {
  1039. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1040. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
  1041. int rc;
  1042. rc = mptscsih_resume(pdev);
  1043. mptspi_dv_renegotiate(hd);
  1044. return rc;
  1045. }
  1046. #endif
  1047. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1048. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1049. /*
  1050. * mptspi_probe - Installs scsi devices per bus.
  1051. * @pdev: Pointer to pci_dev structure
  1052. *
  1053. * Returns 0 for success, non-zero for failure.
  1054. *
  1055. */
  1056. static int
  1057. mptspi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1058. {
  1059. struct Scsi_Host *sh;
  1060. MPT_SCSI_HOST *hd;
  1061. MPT_ADAPTER *ioc;
  1062. unsigned long flags;
  1063. int ii;
  1064. int numSGE = 0;
  1065. int scale;
  1066. int ioc_cap;
  1067. int error=0;
  1068. int r;
  1069. if ((r = mpt_attach(pdev,id)) != 0)
  1070. return r;
  1071. ioc = pci_get_drvdata(pdev);
  1072. ioc->DoneCtx = mptspiDoneCtx;
  1073. ioc->TaskCtx = mptspiTaskCtx;
  1074. ioc->InternalCtx = mptspiInternalCtx;
  1075. /* Added sanity check on readiness of the MPT adapter.
  1076. */
  1077. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  1078. printk(MYIOC_s_WARN_FMT
  1079. "Skipping because it's not operational!\n",
  1080. ioc->name);
  1081. error = -ENODEV;
  1082. goto out_mptspi_probe;
  1083. }
  1084. if (!ioc->active) {
  1085. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  1086. ioc->name);
  1087. error = -ENODEV;
  1088. goto out_mptspi_probe;
  1089. }
  1090. /* Sanity check - ensure at least 1 port is INITIATOR capable
  1091. */
  1092. ioc_cap = 0;
  1093. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  1094. if (ioc->pfacts[ii].ProtocolFlags &
  1095. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  1096. ioc_cap ++;
  1097. }
  1098. if (!ioc_cap) {
  1099. printk(MYIOC_s_WARN_FMT
  1100. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  1101. ioc->name, ioc);
  1102. return 0;
  1103. }
  1104. sh = scsi_host_alloc(&mptspi_driver_template, sizeof(MPT_SCSI_HOST));
  1105. if (!sh) {
  1106. printk(MYIOC_s_WARN_FMT
  1107. "Unable to register controller with SCSI subsystem\n",
  1108. ioc->name);
  1109. error = -1;
  1110. goto out_mptspi_probe;
  1111. }
  1112. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1113. /* Attach the SCSI Host to the IOC structure
  1114. */
  1115. ioc->sh = sh;
  1116. sh->io_port = 0;
  1117. sh->n_io_port = 0;
  1118. sh->irq = 0;
  1119. /* set 16 byte cdb's */
  1120. sh->max_cmd_len = 16;
  1121. /* Yikes! This is important!
  1122. * Otherwise, by default, linux
  1123. * only scans target IDs 0-7!
  1124. * pfactsN->MaxDevices unreliable
  1125. * (not supported in early
  1126. * versions of the FW).
  1127. * max_id = 1 + actual max id,
  1128. * max_lun = 1 + actual last lun,
  1129. * see hosts.h :o(
  1130. */
  1131. sh->max_id = ioc->devices_per_bus;
  1132. sh->max_lun = MPT_LAST_LUN + 1;
  1133. /*
  1134. * If RAID Firmware Detected, setup virtual channel
  1135. */
  1136. if (ioc->ir_firmware)
  1137. sh->max_channel = 1;
  1138. else
  1139. sh->max_channel = 0;
  1140. sh->this_id = ioc->pfacts[0].PortSCSIID;
  1141. /* Required entry.
  1142. */
  1143. sh->unique_id = ioc->id;
  1144. /* Verify that we won't exceed the maximum
  1145. * number of chain buffers
  1146. * We can optimize: ZZ = req_sz/sizeof(SGE)
  1147. * For 32bit SGE's:
  1148. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  1149. * + (req_sz - 64)/sizeof(SGE)
  1150. * A slightly different algorithm is required for
  1151. * 64bit SGEs.
  1152. */
  1153. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  1154. if (sizeof(dma_addr_t) == sizeof(u64)) {
  1155. numSGE = (scale - 1) *
  1156. (ioc->facts.MaxChainDepth-1) + scale +
  1157. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  1158. sizeof(u32));
  1159. } else {
  1160. numSGE = 1 + (scale - 1) *
  1161. (ioc->facts.MaxChainDepth-1) + scale +
  1162. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  1163. sizeof(u32));
  1164. }
  1165. if (numSGE < sh->sg_tablesize) {
  1166. /* Reset this value */
  1167. dprintk((MYIOC_s_INFO_FMT
  1168. "Resetting sg_tablesize to %d from %d\n",
  1169. ioc->name, numSGE, sh->sg_tablesize));
  1170. sh->sg_tablesize = numSGE;
  1171. }
  1172. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1173. hd = (MPT_SCSI_HOST *) sh->hostdata;
  1174. hd->ioc = ioc;
  1175. /* SCSI needs scsi_cmnd lookup table!
  1176. * (with size equal to req_depth*PtrSz!)
  1177. */
  1178. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  1179. if (!hd->ScsiLookup) {
  1180. error = -ENOMEM;
  1181. goto out_mptspi_probe;
  1182. }
  1183. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  1184. ioc->name, hd->ScsiLookup));
  1185. /* Clear the TM flags
  1186. */
  1187. hd->tmPending = 0;
  1188. hd->tmState = TM_STATE_NONE;
  1189. hd->resetPending = 0;
  1190. hd->abortSCpnt = NULL;
  1191. /* Clear the pointer used to store
  1192. * single-threaded commands, i.e., those
  1193. * issued during a bus scan, dv and
  1194. * configuration pages.
  1195. */
  1196. hd->cmdPtr = NULL;
  1197. /* Initialize this SCSI Hosts' timers
  1198. * To use, set the timer expires field
  1199. * and add_timer
  1200. */
  1201. init_timer(&hd->timer);
  1202. hd->timer.data = (unsigned long) hd;
  1203. hd->timer.function = mptscsih_timer_expired;
  1204. ioc->spi_data.Saf_Te = mpt_saf_te;
  1205. hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
  1206. ddvprintk((MYIOC_s_INFO_FMT
  1207. "saf_te %x\n",
  1208. ioc->name,
  1209. mpt_saf_te));
  1210. ioc->spi_data.noQas = 0;
  1211. init_waitqueue_head(&hd->scandv_waitq);
  1212. hd->scandv_wait_done = 0;
  1213. hd->last_queue_full = 0;
  1214. /* Some versions of the firmware don't support page 0; without
  1215. * that we can't get the parameters */
  1216. if (hd->ioc->spi_data.sdp0length != 0)
  1217. sh->transportt = mptspi_transport_template;
  1218. error = scsi_add_host (sh, &ioc->pcidev->dev);
  1219. if(error) {
  1220. dprintk((KERN_ERR MYNAM
  1221. "scsi_add_host failed\n"));
  1222. goto out_mptspi_probe;
  1223. }
  1224. /*
  1225. * issue internal bus reset
  1226. */
  1227. if (ioc->spi_data.bus_reset)
  1228. mptscsih_TMHandler(hd,
  1229. MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
  1230. 0, 0, 0, 0, 5);
  1231. scsi_scan_host(sh);
  1232. return 0;
  1233. out_mptspi_probe:
  1234. mptscsih_remove(pdev);
  1235. return error;
  1236. }
  1237. static struct pci_driver mptspi_driver = {
  1238. .name = "mptspi",
  1239. .id_table = mptspi_pci_table,
  1240. .probe = mptspi_probe,
  1241. .remove = __devexit_p(mptscsih_remove),
  1242. .shutdown = mptscsih_shutdown,
  1243. #ifdef CONFIG_PM
  1244. .suspend = mptscsih_suspend,
  1245. .resume = mptspi_resume,
  1246. #endif
  1247. };
  1248. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1249. /**
  1250. * mptspi_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
  1251. *
  1252. * Returns 0 for success, non-zero for failure.
  1253. */
  1254. static int __init
  1255. mptspi_init(void)
  1256. {
  1257. show_mptmod_ver(my_NAME, my_VERSION);
  1258. mptspi_transport_template = spi_attach_transport(&mptspi_transport_functions);
  1259. if (!mptspi_transport_template)
  1260. return -ENODEV;
  1261. mptspiDoneCtx = mpt_register(mptscsih_io_done, MPTSPI_DRIVER);
  1262. mptspiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSPI_DRIVER);
  1263. mptspiInternalCtx = mpt_register(mptscsih_scandv_complete, MPTSPI_DRIVER);
  1264. if (mpt_event_register(mptspiDoneCtx, mptspi_event_process) == 0) {
  1265. devtverboseprintk((KERN_INFO MYNAM
  1266. ": Registered for IOC event notifications\n"));
  1267. }
  1268. if (mpt_reset_register(mptspiDoneCtx, mptspi_ioc_reset) == 0) {
  1269. dprintk((KERN_INFO MYNAM
  1270. ": Registered for IOC reset notifications\n"));
  1271. }
  1272. return pci_register_driver(&mptspi_driver);
  1273. }
  1274. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1275. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1276. /**
  1277. * mptspi_exit - Unregisters MPT adapter(s)
  1278. */
  1279. static void __exit
  1280. mptspi_exit(void)
  1281. {
  1282. pci_unregister_driver(&mptspi_driver);
  1283. mpt_reset_deregister(mptspiDoneCtx);
  1284. dprintk((KERN_INFO MYNAM
  1285. ": Deregistered for IOC reset notifications\n"));
  1286. mpt_event_deregister(mptspiDoneCtx);
  1287. dprintk((KERN_INFO MYNAM
  1288. ": Deregistered for IOC event notifications\n"));
  1289. mpt_deregister(mptspiInternalCtx);
  1290. mpt_deregister(mptspiTaskCtx);
  1291. mpt_deregister(mptspiDoneCtx);
  1292. spi_release_transport(mptspi_transport_template);
  1293. }
  1294. module_init(mptspi_init);
  1295. module_exit(mptspi_exit);