mptspi.c 41 KB

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