mptspi.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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:DL-MPTFusionLinux@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/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/kdev_t.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  50. #include <linux/reboot.h> /* notifier code */
  51. #include <linux/workqueue.h>
  52. #include <linux/raid_class.h>
  53. #include <scsi/scsi.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsi_device.h>
  56. #include <scsi/scsi_host.h>
  57. #include <scsi/scsi_tcq.h>
  58. #include <scsi/scsi_transport.h>
  59. #include <scsi/scsi_transport_spi.h>
  60. #include <scsi/scsi_dbg.h>
  61. #include "mptbase.h"
  62. #include "mptscsih.h"
  63. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  64. #define my_NAME "Fusion MPT SPI Host driver"
  65. #define my_VERSION MPT_LINUX_VERSION_COMMON
  66. #define MYNAM "mptspi"
  67. MODULE_AUTHOR(MODULEAUTHOR);
  68. MODULE_DESCRIPTION(my_NAME);
  69. MODULE_LICENSE("GPL");
  70. MODULE_VERSION(my_VERSION);
  71. /* Command line args */
  72. static int mpt_saf_te = MPTSCSIH_SAF_TE;
  73. module_param(mpt_saf_te, int, 0);
  74. MODULE_PARM_DESC(mpt_saf_te, " Force enabling SEP Processor: enable=1 (default=MPTSCSIH_SAF_TE=0)");
  75. static void mptspi_write_offset(struct scsi_target *, int);
  76. static void mptspi_write_width(struct scsi_target *, int);
  77. static int mptspi_write_spi_device_pg1(struct scsi_target *,
  78. struct _CONFIG_PAGE_SCSI_DEVICE_1 *);
  79. static struct scsi_transport_template *mptspi_transport_template = NULL;
  80. static int mptspiDoneCtx = -1;
  81. static int mptspiTaskCtx = -1;
  82. static int mptspiInternalCtx = -1; /* Used only for internal commands */
  83. /**
  84. * mptspi_setTargetNegoParms - Update the target negotiation parameters
  85. * @hd: Pointer to a SCSI Host Structure
  86. * @target: per target private data
  87. * @sdev: SCSI device
  88. *
  89. * Update the target negotiation parameters based on the the Inquiry
  90. * data, adapter capabilities, and NVRAM settings.
  91. **/
  92. static void
  93. mptspi_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtTarget *target,
  94. struct scsi_device *sdev)
  95. {
  96. SpiCfgData *pspi_data = &hd->ioc->spi_data;
  97. int id = (int) target->id;
  98. int nvram;
  99. u8 width = MPT_NARROW;
  100. u8 factor = MPT_ASYNC;
  101. u8 offset = 0;
  102. u8 nfactor;
  103. u8 noQas = 1;
  104. target->negoFlags = pspi_data->noQas;
  105. if (sdev->scsi_level < SCSI_2) {
  106. width = 0;
  107. factor = MPT_ULTRA2;
  108. offset = pspi_data->maxSyncOffset;
  109. target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
  110. } else {
  111. if (scsi_device_wide(sdev))
  112. width = 1;
  113. if (scsi_device_sync(sdev)) {
  114. factor = pspi_data->minSyncFactor;
  115. if (!scsi_device_dt(sdev))
  116. factor = MPT_ULTRA2;
  117. else {
  118. if (!scsi_device_ius(sdev) &&
  119. !scsi_device_qas(sdev))
  120. factor = MPT_ULTRA160;
  121. else {
  122. factor = MPT_ULTRA320;
  123. if (scsi_device_qas(sdev)) {
  124. ddvprintk((KERN_INFO "Enabling QAS due to byte56=%02x on id=%d!\n", scsi_device_qas(sdev), id));
  125. noQas = 0;
  126. }
  127. if (sdev->type == TYPE_TAPE &&
  128. scsi_device_ius(sdev))
  129. target->negoFlags |= MPT_TAPE_NEGO_IDP;
  130. }
  131. }
  132. offset = pspi_data->maxSyncOffset;
  133. /* If RAID, never disable QAS
  134. * else if non RAID, do not disable
  135. * QAS if bit 1 is set
  136. * bit 1 QAS support, non-raid only
  137. * bit 0 IU support
  138. */
  139. if (target->raidVolume == 1)
  140. noQas = 0;
  141. } else {
  142. factor = MPT_ASYNC;
  143. offset = 0;
  144. }
  145. }
  146. if (!sdev->tagged_supported)
  147. target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
  148. /* Update tflags based on NVRAM settings. (SCSI only)
  149. */
  150. if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  151. nvram = pspi_data->nvram[id];
  152. nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
  153. if (width)
  154. width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  155. if (offset > 0) {
  156. /* Ensure factor is set to the
  157. * maximum of: adapter, nvram, inquiry
  158. */
  159. if (nfactor) {
  160. if (nfactor < pspi_data->minSyncFactor )
  161. nfactor = pspi_data->minSyncFactor;
  162. factor = max(factor, nfactor);
  163. if (factor == MPT_ASYNC)
  164. offset = 0;
  165. } else {
  166. offset = 0;
  167. factor = MPT_ASYNC;
  168. }
  169. } else {
  170. factor = MPT_ASYNC;
  171. }
  172. }
  173. /* Make sure data is consistent
  174. */
  175. if ((!width) && (factor < MPT_ULTRA2))
  176. factor = MPT_ULTRA2;
  177. /* Save the data to the target structure.
  178. */
  179. target->minSyncFactor = factor;
  180. target->maxOffset = offset;
  181. target->maxWidth = width;
  182. target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;
  183. /* Disable unused features.
  184. */
  185. if (!width)
  186. target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
  187. if (!offset)
  188. target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
  189. if ( factor > MPT_ULTRA320 )
  190. noQas = 0;
  191. if (noQas && (pspi_data->noQas == 0)) {
  192. pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
  193. target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
  194. /* Disable QAS in a mixed configuration case
  195. */
  196. ddvprintk((KERN_INFO "Disabling QAS due to noQas=%02x on id=%d!\n", noQas, id));
  197. }
  198. }
  199. /**
  200. * mptspi_writeIOCPage4 - write IOC Page 4
  201. * @hd: Pointer to a SCSI Host Structure
  202. * @channel: channel number
  203. * @id: write IOC Page4 for this ID & Bus
  204. *
  205. * Return: -EAGAIN if unable to obtain a Message Frame
  206. * or 0 if success.
  207. *
  208. * Remark: We do not wait for a return, write pages sequentially.
  209. **/
  210. static int
  211. mptspi_writeIOCPage4(MPT_SCSI_HOST *hd, u8 channel , u8 id)
  212. {
  213. MPT_ADAPTER *ioc = hd->ioc;
  214. Config_t *pReq;
  215. IOCPage4_t *IOCPage4Ptr;
  216. MPT_FRAME_HDR *mf;
  217. dma_addr_t dataDma;
  218. u16 req_idx;
  219. u32 frameOffset;
  220. u32 flagsLength;
  221. int ii;
  222. /* Get a MF for this command.
  223. */
  224. if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
  225. dfailprintk((MYIOC_s_WARN_FMT "writeIOCPage4 : no msg frames!\n",
  226. ioc->name));
  227. return -EAGAIN;
  228. }
  229. /* Set the request and the data pointers.
  230. * Place data at end of MF.
  231. */
  232. pReq = (Config_t *)mf;
  233. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  234. frameOffset = ioc->req_sz - sizeof(IOCPage4_t);
  235. /* Complete the request frame (same for all requests).
  236. */
  237. pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  238. pReq->Reserved = 0;
  239. pReq->ChainOffset = 0;
  240. pReq->Function = MPI_FUNCTION_CONFIG;
  241. pReq->ExtPageLength = 0;
  242. pReq->ExtPageType = 0;
  243. pReq->MsgFlags = 0;
  244. for (ii=0; ii < 8; ii++) {
  245. pReq->Reserved2[ii] = 0;
  246. }
  247. IOCPage4Ptr = ioc->spi_data.pIocPg4;
  248. dataDma = ioc->spi_data.IocPg4_dma;
  249. ii = IOCPage4Ptr->ActiveSEP++;
  250. IOCPage4Ptr->SEP[ii].SEPTargetID = id;
  251. IOCPage4Ptr->SEP[ii].SEPBus = channel;
  252. pReq->Header = IOCPage4Ptr->Header;
  253. pReq->PageAddress = cpu_to_le32(id | (channel << 8 ));
  254. /* Add a SGE to the config request.
  255. */
  256. flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
  257. (IOCPage4Ptr->Header.PageLength + ii) * 4;
  258. mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
  259. ddvprintk((MYIOC_s_INFO_FMT
  260. "writeIOCPage4: MaxSEP=%d ActiveSEP=%d id=%d bus=%d\n",
  261. ioc->name, IOCPage4Ptr->MaxSEP, IOCPage4Ptr->ActiveSEP, id, channel));
  262. mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
  263. return 0;
  264. }
  265. /**
  266. * mptspi_initTarget - Target, LUN alloc/free functionality.
  267. * @hd: Pointer to MPT_SCSI_HOST structure
  268. * @vtarget: per target private data
  269. * @sdev: SCSI device
  270. *
  271. * NOTE: It's only SAFE to call this routine if data points to
  272. * sane & valid STANDARD INQUIRY data!
  273. *
  274. * Allocate and initialize memory for this target.
  275. * Save inquiry data.
  276. *
  277. **/
  278. static void
  279. mptspi_initTarget(MPT_SCSI_HOST *hd, VirtTarget *vtarget,
  280. struct scsi_device *sdev)
  281. {
  282. /* Is LUN supported? If so, upper 2 bits will be 0
  283. * in first byte of inquiry data.
  284. */
  285. if (sdev->inq_periph_qual != 0)
  286. return;
  287. if (vtarget == NULL)
  288. return;
  289. vtarget->type = sdev->type;
  290. if ((sdev->type == TYPE_PROCESSOR) && (hd->ioc->spi_data.Saf_Te)) {
  291. /* Treat all Processors as SAF-TE if
  292. * command line option is set */
  293. vtarget->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
  294. mptspi_writeIOCPage4(hd, vtarget->channel, vtarget->id);
  295. }else if ((sdev->type == TYPE_PROCESSOR) &&
  296. !(vtarget->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
  297. if (sdev->inquiry_len > 49 ) {
  298. if (sdev->inquiry[44] == 'S' &&
  299. sdev->inquiry[45] == 'A' &&
  300. sdev->inquiry[46] == 'F' &&
  301. sdev->inquiry[47] == '-' &&
  302. sdev->inquiry[48] == 'T' &&
  303. sdev->inquiry[49] == 'E' ) {
  304. vtarget->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
  305. mptspi_writeIOCPage4(hd, vtarget->channel, vtarget->id);
  306. }
  307. }
  308. }
  309. mptspi_setTargetNegoParms(hd, vtarget, sdev);
  310. }
  311. /**
  312. * mptspi_is_raid - Determines whether target is belonging to volume
  313. * @hd: Pointer to a SCSI HOST structure
  314. * @id: target device id
  315. *
  316. * Return:
  317. * non-zero = true
  318. * zero = false
  319. *
  320. */
  321. static int
  322. mptspi_is_raid(struct _MPT_SCSI_HOST *hd, u32 id)
  323. {
  324. int i, rc = 0;
  325. if (!hd->ioc->raid_data.pIocPg2)
  326. goto out;
  327. if (!hd->ioc->raid_data.pIocPg2->NumActiveVolumes)
  328. goto out;
  329. for (i=0; i < hd->ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  330. if (hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID == id) {
  331. rc = 1;
  332. goto out;
  333. }
  334. }
  335. out:
  336. return rc;
  337. }
  338. static int mptspi_target_alloc(struct scsi_target *starget)
  339. {
  340. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  341. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
  342. VirtTarget *vtarget;
  343. if (hd == NULL)
  344. return -ENODEV;
  345. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  346. if (!vtarget)
  347. return -ENOMEM;
  348. vtarget->ioc_id = hd->ioc->id;
  349. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  350. vtarget->id = (u8)starget->id;
  351. vtarget->channel = (u8)starget->channel;
  352. vtarget->starget = starget;
  353. starget->hostdata = vtarget;
  354. if (starget->channel == 1) {
  355. if (mptscsih_is_phys_disk(hd->ioc, 0, starget->id) == 0)
  356. return 0;
  357. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  358. /* The real channel for this device is zero */
  359. vtarget->channel = 0;
  360. /* The actual physdisknum (for RAID passthrough) */
  361. vtarget->id = mptscsih_raid_id_to_num(hd->ioc, 0,
  362. starget->id);
  363. }
  364. if (starget->channel == 0 &&
  365. mptspi_is_raid(hd, starget->id)) {
  366. vtarget->raidVolume = 1;
  367. ddvprintk((KERN_INFO
  368. "RAID Volume @ channel=%d id=%d\n", starget->channel,
  369. starget->id));
  370. }
  371. if (hd->ioc->spi_data.nvram &&
  372. hd->ioc->spi_data.nvram[starget->id] != MPT_HOST_NVRAM_INVALID) {
  373. u32 nvram = hd->ioc->spi_data.nvram[starget->id];
  374. spi_min_period(starget) = (nvram & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
  375. spi_max_width(starget) = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  376. } else {
  377. spi_min_period(starget) = hd->ioc->spi_data.minSyncFactor;
  378. spi_max_width(starget) = hd->ioc->spi_data.maxBusWidth;
  379. }
  380. spi_max_offset(starget) = hd->ioc->spi_data.maxSyncOffset;
  381. spi_offset(starget) = 0;
  382. mptspi_write_width(starget, 0);
  383. return 0;
  384. }
  385. static void
  386. mptspi_target_destroy(struct scsi_target *starget)
  387. {
  388. if (starget->hostdata)
  389. kfree(starget->hostdata);
  390. starget->hostdata = NULL;
  391. }
  392. /**
  393. * mptspi_print_write_nego - negotiation parameters debug info that is being sent
  394. * @hd: Pointer to a SCSI HOST structure
  395. * @starget: SCSI target
  396. * @ii: negotiation parameters
  397. *
  398. */
  399. static void
  400. mptspi_print_write_nego(struct _MPT_SCSI_HOST *hd, struct scsi_target *starget, u32 ii)
  401. {
  402. ddvprintk((MYIOC_s_INFO_FMT "id=%d Requested = 0x%08x"
  403. " ( %s factor = 0x%02x @ offset = 0x%02x %s%s%s%s%s%s%s%s)\n",
  404. hd->ioc->name, starget->id, ii,
  405. ii & MPI_SCSIDEVPAGE0_NP_WIDE ? "Wide ": "",
  406. ((ii >> 8) & 0xFF), ((ii >> 16) & 0xFF),
  407. ii & MPI_SCSIDEVPAGE0_NP_IU ? "IU ": "",
  408. ii & MPI_SCSIDEVPAGE0_NP_DT ? "DT ": "",
  409. ii & MPI_SCSIDEVPAGE0_NP_QAS ? "QAS ": "",
  410. ii & MPI_SCSIDEVPAGE0_NP_HOLD_MCS ? "HOLDMCS ": "",
  411. ii & MPI_SCSIDEVPAGE0_NP_WR_FLOW ? "WRFLOW ": "",
  412. ii & MPI_SCSIDEVPAGE0_NP_RD_STRM ? "RDSTRM ": "",
  413. ii & MPI_SCSIDEVPAGE0_NP_RTI ? "RTI ": "",
  414. ii & MPI_SCSIDEVPAGE0_NP_PCOMP_EN ? "PCOMP ": ""));
  415. }
  416. /**
  417. * mptspi_print_read_nego - negotiation parameters debug info that is being read
  418. * @hd: Pointer to a SCSI HOST structure
  419. * @starget: SCSI target
  420. * @ii: negotiation parameters
  421. *
  422. */
  423. static void
  424. mptspi_print_read_nego(struct _MPT_SCSI_HOST *hd, struct scsi_target *starget, u32 ii)
  425. {
  426. ddvprintk((MYIOC_s_INFO_FMT "id=%d Read = 0x%08x"
  427. " ( %s factor = 0x%02x @ offset = 0x%02x %s%s%s%s%s%s%s%s)\n",
  428. hd->ioc->name, starget->id, ii,
  429. ii & MPI_SCSIDEVPAGE0_NP_WIDE ? "Wide ": "",
  430. ((ii >> 8) & 0xFF), ((ii >> 16) & 0xFF),
  431. ii & MPI_SCSIDEVPAGE0_NP_IU ? "IU ": "",
  432. ii & MPI_SCSIDEVPAGE0_NP_DT ? "DT ": "",
  433. ii & MPI_SCSIDEVPAGE0_NP_QAS ? "QAS ": "",
  434. ii & MPI_SCSIDEVPAGE0_NP_HOLD_MCS ? "HOLDMCS ": "",
  435. ii & MPI_SCSIDEVPAGE0_NP_WR_FLOW ? "WRFLOW ": "",
  436. ii & MPI_SCSIDEVPAGE0_NP_RD_STRM ? "RDSTRM ": "",
  437. ii & MPI_SCSIDEVPAGE0_NP_RTI ? "RTI ": "",
  438. ii & MPI_SCSIDEVPAGE0_NP_PCOMP_EN ? "PCOMP ": ""));
  439. }
  440. static int mptspi_read_spi_device_pg0(struct scsi_target *starget,
  441. struct _CONFIG_PAGE_SCSI_DEVICE_0 *pass_pg0)
  442. {
  443. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  444. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
  445. struct _MPT_ADAPTER *ioc = hd->ioc;
  446. struct _CONFIG_PAGE_SCSI_DEVICE_0 *pg0;
  447. dma_addr_t pg0_dma;
  448. int size;
  449. struct _x_config_parms cfg;
  450. struct _CONFIG_PAGE_HEADER hdr;
  451. int err = -EBUSY;
  452. /* No SPI parameters for RAID devices */
  453. if (starget->channel == 0 &&
  454. mptspi_is_raid(hd, starget->id))
  455. return -1;
  456. size = ioc->spi_data.sdp0length * 4;
  457. /*
  458. if (ioc->spi_data.sdp0length & 1)
  459. size += size + 4;
  460. size += 2048;
  461. */
  462. pg0 = dma_alloc_coherent(&ioc->pcidev->dev, size, &pg0_dma, GFP_KERNEL);
  463. if (pg0 == NULL) {
  464. starget_printk(KERN_ERR, starget, "dma_alloc_coherent for parameters failed\n");
  465. return -EINVAL;
  466. }
  467. memset(&hdr, 0, sizeof(hdr));
  468. hdr.PageVersion = ioc->spi_data.sdp0version;
  469. hdr.PageLength = ioc->spi_data.sdp0length;
  470. hdr.PageNumber = 0;
  471. hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  472. memset(&cfg, 0, sizeof(cfg));
  473. cfg.cfghdr.hdr = &hdr;
  474. cfg.physAddr = pg0_dma;
  475. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  476. cfg.dir = 0;
  477. cfg.pageAddr = starget->id;
  478. if (mpt_config(ioc, &cfg)) {
  479. starget_printk(KERN_ERR, starget, "mpt_config failed\n");
  480. goto out_free;
  481. }
  482. err = 0;
  483. memcpy(pass_pg0, pg0, size);
  484. mptspi_print_read_nego(hd, starget, le32_to_cpu(pg0->NegotiatedParameters));
  485. out_free:
  486. dma_free_coherent(&ioc->pcidev->dev, size, pg0, pg0_dma);
  487. return err;
  488. }
  489. static u32 mptspi_getRP(struct scsi_target *starget)
  490. {
  491. u32 nego = 0;
  492. nego |= spi_iu(starget) ? MPI_SCSIDEVPAGE1_RP_IU : 0;
  493. nego |= spi_dt(starget) ? MPI_SCSIDEVPAGE1_RP_DT : 0;
  494. nego |= spi_qas(starget) ? MPI_SCSIDEVPAGE1_RP_QAS : 0;
  495. nego |= spi_hold_mcs(starget) ? MPI_SCSIDEVPAGE1_RP_HOLD_MCS : 0;
  496. nego |= spi_wr_flow(starget) ? MPI_SCSIDEVPAGE1_RP_WR_FLOW : 0;
  497. nego |= spi_rd_strm(starget) ? MPI_SCSIDEVPAGE1_RP_RD_STRM : 0;
  498. nego |= spi_rti(starget) ? MPI_SCSIDEVPAGE1_RP_RTI : 0;
  499. nego |= spi_pcomp_en(starget) ? MPI_SCSIDEVPAGE1_RP_PCOMP_EN : 0;
  500. nego |= (spi_period(starget) << MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD) & MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
  501. nego |= (spi_offset(starget) << MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET) & MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
  502. nego |= spi_width(starget) ? MPI_SCSIDEVPAGE1_RP_WIDE : 0;
  503. return nego;
  504. }
  505. static void mptspi_read_parameters(struct scsi_target *starget)
  506. {
  507. int nego;
  508. struct _CONFIG_PAGE_SCSI_DEVICE_0 pg0;
  509. mptspi_read_spi_device_pg0(starget, &pg0);
  510. nego = le32_to_cpu(pg0.NegotiatedParameters);
  511. spi_iu(starget) = (nego & MPI_SCSIDEVPAGE0_NP_IU) ? 1 : 0;
  512. spi_dt(starget) = (nego & MPI_SCSIDEVPAGE0_NP_DT) ? 1 : 0;
  513. spi_qas(starget) = (nego & MPI_SCSIDEVPAGE0_NP_QAS) ? 1 : 0;
  514. spi_wr_flow(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WR_FLOW) ? 1 : 0;
  515. spi_rd_strm(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RD_STRM) ? 1 : 0;
  516. spi_rti(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RTI) ? 1 : 0;
  517. spi_pcomp_en(starget) = (nego & MPI_SCSIDEVPAGE0_NP_PCOMP_EN) ? 1 : 0;
  518. spi_hold_mcs(starget) = (nego & MPI_SCSIDEVPAGE0_NP_HOLD_MCS) ? 1 : 0;
  519. spi_period(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_PERIOD;
  520. spi_offset(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_OFFSET;
  521. spi_width(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WIDE) ? 1 : 0;
  522. }
  523. static int
  524. mptscsih_quiesce_raid(MPT_SCSI_HOST *hd, int quiesce, u8 channel, u8 id)
  525. {
  526. MpiRaidActionRequest_t *pReq;
  527. MPT_FRAME_HDR *mf;
  528. /* Get and Populate a free Frame
  529. */
  530. if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
  531. ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
  532. hd->ioc->name));
  533. return -EAGAIN;
  534. }
  535. pReq = (MpiRaidActionRequest_t *)mf;
  536. if (quiesce)
  537. pReq->Action = MPI_RAID_ACTION_QUIESCE_PHYS_IO;
  538. else
  539. pReq->Action = MPI_RAID_ACTION_ENABLE_PHYS_IO;
  540. pReq->Reserved1 = 0;
  541. pReq->ChainOffset = 0;
  542. pReq->Function = MPI_FUNCTION_RAID_ACTION;
  543. pReq->VolumeID = id;
  544. pReq->VolumeBus = channel;
  545. pReq->PhysDiskNum = 0;
  546. pReq->MsgFlags = 0;
  547. pReq->Reserved2 = 0;
  548. pReq->ActionDataWord = 0; /* Reserved for this action */
  549. mpt_add_sge((char *)&pReq->ActionDataSGE,
  550. MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
  551. ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action=%x channel=%d id=%d\n",
  552. hd->ioc->name, pReq->Action, channel, id));
  553. hd->pLocal = NULL;
  554. hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
  555. hd->scandv_wait_done = 0;
  556. /* Save cmd pointer, for resource free if timeout or
  557. * FW reload occurs
  558. */
  559. hd->cmdPtr = mf;
  560. add_timer(&hd->timer);
  561. mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
  562. wait_event(hd->scandv_waitq, hd->scandv_wait_done);
  563. if ((hd->pLocal == NULL) || (hd->pLocal->completion != 0))
  564. return -1;
  565. return 0;
  566. }
  567. static void mptspi_dv_device(struct _MPT_SCSI_HOST *hd,
  568. struct scsi_device *sdev)
  569. {
  570. VirtTarget *vtarget = scsi_target(sdev)->hostdata;
  571. /* no DV on RAID devices */
  572. if (sdev->channel == 0 &&
  573. mptspi_is_raid(hd, sdev->id))
  574. return;
  575. /* If this is a piece of a RAID, then quiesce first */
  576. if (sdev->channel == 1 &&
  577. mptscsih_quiesce_raid(hd, 1, vtarget->channel, vtarget->id) < 0) {
  578. starget_printk(KERN_ERR, scsi_target(sdev),
  579. "Integrated RAID quiesce failed\n");
  580. return;
  581. }
  582. hd->spi_pending |= (1 << sdev->id);
  583. spi_dv_device(sdev);
  584. hd->spi_pending &= ~(1 << sdev->id);
  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. struct scsi_target *starget;
  1006. struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
  1007. u32 nego;
  1008. kfree(wqw);
  1009. if (hd->spi_pending) {
  1010. shost_for_each_device(sdev, hd->ioc->sh) {
  1011. if (hd->spi_pending & (1 << sdev->id))
  1012. continue;
  1013. starget = scsi_target(sdev);
  1014. nego = mptspi_getRP(starget);
  1015. pg1.RequestedParameters = cpu_to_le32(nego);
  1016. pg1.Reserved = 0;
  1017. pg1.Configuration = 0;
  1018. mptspi_write_spi_device_pg1(starget, &pg1);
  1019. }
  1020. } else {
  1021. shost_for_each_device(sdev, hd->ioc->sh)
  1022. mptspi_dv_device(hd, sdev);
  1023. }
  1024. }
  1025. static void
  1026. mptspi_dv_renegotiate(struct _MPT_SCSI_HOST *hd)
  1027. {
  1028. struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
  1029. if (!wqw)
  1030. return;
  1031. INIT_WORK(&wqw->work, mptspi_dv_renegotiate_work);
  1032. wqw->hd = hd;
  1033. schedule_work(&wqw->work);
  1034. }
  1035. /*
  1036. * spi module reset handler
  1037. */
  1038. static int
  1039. mptspi_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1040. {
  1041. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
  1042. int rc;
  1043. rc = mptscsih_ioc_reset(ioc, reset_phase);
  1044. if (reset_phase == MPT_IOC_POST_RESET)
  1045. mptspi_dv_renegotiate(hd);
  1046. return rc;
  1047. }
  1048. #ifdef CONFIG_PM
  1049. /*
  1050. * spi module resume handler
  1051. */
  1052. static int
  1053. mptspi_resume(struct pci_dev *pdev)
  1054. {
  1055. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1056. struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
  1057. int rc;
  1058. rc = mptscsih_resume(pdev);
  1059. mptspi_dv_renegotiate(hd);
  1060. return rc;
  1061. }
  1062. #endif
  1063. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1064. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1065. /*
  1066. * mptspi_probe - Installs scsi devices per bus.
  1067. * @pdev: Pointer to pci_dev structure
  1068. *
  1069. * Returns 0 for success, non-zero for failure.
  1070. *
  1071. */
  1072. static int
  1073. mptspi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1074. {
  1075. struct Scsi_Host *sh;
  1076. MPT_SCSI_HOST *hd;
  1077. MPT_ADAPTER *ioc;
  1078. unsigned long flags;
  1079. int ii;
  1080. int numSGE = 0;
  1081. int scale;
  1082. int ioc_cap;
  1083. int error=0;
  1084. int r;
  1085. if ((r = mpt_attach(pdev,id)) != 0)
  1086. return r;
  1087. ioc = pci_get_drvdata(pdev);
  1088. ioc->DoneCtx = mptspiDoneCtx;
  1089. ioc->TaskCtx = mptspiTaskCtx;
  1090. ioc->InternalCtx = mptspiInternalCtx;
  1091. /* Added sanity check on readiness of the MPT adapter.
  1092. */
  1093. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  1094. printk(MYIOC_s_WARN_FMT
  1095. "Skipping because it's not operational!\n",
  1096. ioc->name);
  1097. error = -ENODEV;
  1098. goto out_mptspi_probe;
  1099. }
  1100. if (!ioc->active) {
  1101. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  1102. ioc->name);
  1103. error = -ENODEV;
  1104. goto out_mptspi_probe;
  1105. }
  1106. /* Sanity check - ensure at least 1 port is INITIATOR capable
  1107. */
  1108. ioc_cap = 0;
  1109. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  1110. if (ioc->pfacts[ii].ProtocolFlags &
  1111. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  1112. ioc_cap ++;
  1113. }
  1114. if (!ioc_cap) {
  1115. printk(MYIOC_s_WARN_FMT
  1116. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  1117. ioc->name, ioc);
  1118. return 0;
  1119. }
  1120. sh = scsi_host_alloc(&mptspi_driver_template, sizeof(MPT_SCSI_HOST));
  1121. if (!sh) {
  1122. printk(MYIOC_s_WARN_FMT
  1123. "Unable to register controller with SCSI subsystem\n",
  1124. ioc->name);
  1125. error = -1;
  1126. goto out_mptspi_probe;
  1127. }
  1128. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1129. /* Attach the SCSI Host to the IOC structure
  1130. */
  1131. ioc->sh = sh;
  1132. sh->io_port = 0;
  1133. sh->n_io_port = 0;
  1134. sh->irq = 0;
  1135. /* set 16 byte cdb's */
  1136. sh->max_cmd_len = 16;
  1137. /* Yikes! This is important!
  1138. * Otherwise, by default, linux
  1139. * only scans target IDs 0-7!
  1140. * pfactsN->MaxDevices unreliable
  1141. * (not supported in early
  1142. * versions of the FW).
  1143. * max_id = 1 + actual max id,
  1144. * max_lun = 1 + actual last lun,
  1145. * see hosts.h :o(
  1146. */
  1147. sh->max_id = ioc->devices_per_bus;
  1148. sh->max_lun = MPT_LAST_LUN + 1;
  1149. /*
  1150. * If RAID Firmware Detected, setup virtual channel
  1151. */
  1152. if (ioc->ir_firmware)
  1153. sh->max_channel = 1;
  1154. else
  1155. sh->max_channel = 0;
  1156. sh->this_id = ioc->pfacts[0].PortSCSIID;
  1157. /* Required entry.
  1158. */
  1159. sh->unique_id = ioc->id;
  1160. /* Verify that we won't exceed the maximum
  1161. * number of chain buffers
  1162. * We can optimize: ZZ = req_sz/sizeof(SGE)
  1163. * For 32bit SGE's:
  1164. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  1165. * + (req_sz - 64)/sizeof(SGE)
  1166. * A slightly different algorithm is required for
  1167. * 64bit SGEs.
  1168. */
  1169. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  1170. if (sizeof(dma_addr_t) == sizeof(u64)) {
  1171. numSGE = (scale - 1) *
  1172. (ioc->facts.MaxChainDepth-1) + scale +
  1173. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  1174. sizeof(u32));
  1175. } else {
  1176. numSGE = 1 + (scale - 1) *
  1177. (ioc->facts.MaxChainDepth-1) + scale +
  1178. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  1179. sizeof(u32));
  1180. }
  1181. if (numSGE < sh->sg_tablesize) {
  1182. /* Reset this value */
  1183. dprintk((MYIOC_s_INFO_FMT
  1184. "Resetting sg_tablesize to %d from %d\n",
  1185. ioc->name, numSGE, sh->sg_tablesize));
  1186. sh->sg_tablesize = numSGE;
  1187. }
  1188. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1189. hd = (MPT_SCSI_HOST *) sh->hostdata;
  1190. hd->ioc = ioc;
  1191. /* SCSI needs scsi_cmnd lookup table!
  1192. * (with size equal to req_depth*PtrSz!)
  1193. */
  1194. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  1195. if (!hd->ScsiLookup) {
  1196. error = -ENOMEM;
  1197. goto out_mptspi_probe;
  1198. }
  1199. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  1200. ioc->name, hd->ScsiLookup));
  1201. /* Clear the TM flags
  1202. */
  1203. hd->tmPending = 0;
  1204. hd->tmState = TM_STATE_NONE;
  1205. hd->resetPending = 0;
  1206. hd->abortSCpnt = NULL;
  1207. /* Clear the pointer used to store
  1208. * single-threaded commands, i.e., those
  1209. * issued during a bus scan, dv and
  1210. * configuration pages.
  1211. */
  1212. hd->cmdPtr = NULL;
  1213. /* Initialize this SCSI Hosts' timers
  1214. * To use, set the timer expires field
  1215. * and add_timer
  1216. */
  1217. init_timer(&hd->timer);
  1218. hd->timer.data = (unsigned long) hd;
  1219. hd->timer.function = mptscsih_timer_expired;
  1220. ioc->spi_data.Saf_Te = mpt_saf_te;
  1221. hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
  1222. ddvprintk((MYIOC_s_INFO_FMT
  1223. "saf_te %x\n",
  1224. ioc->name,
  1225. mpt_saf_te));
  1226. ioc->spi_data.noQas = 0;
  1227. init_waitqueue_head(&hd->scandv_waitq);
  1228. hd->scandv_wait_done = 0;
  1229. hd->last_queue_full = 0;
  1230. hd->spi_pending = 0;
  1231. /* Some versions of the firmware don't support page 0; without
  1232. * that we can't get the parameters */
  1233. if (hd->ioc->spi_data.sdp0length != 0)
  1234. sh->transportt = mptspi_transport_template;
  1235. error = scsi_add_host (sh, &ioc->pcidev->dev);
  1236. if(error) {
  1237. dprintk((KERN_ERR MYNAM
  1238. "scsi_add_host failed\n"));
  1239. goto out_mptspi_probe;
  1240. }
  1241. /*
  1242. * issue internal bus reset
  1243. */
  1244. if (ioc->spi_data.bus_reset)
  1245. mptscsih_TMHandler(hd,
  1246. MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
  1247. 0, 0, 0, 0, 5);
  1248. scsi_scan_host(sh);
  1249. return 0;
  1250. out_mptspi_probe:
  1251. mptscsih_remove(pdev);
  1252. return error;
  1253. }
  1254. static struct pci_driver mptspi_driver = {
  1255. .name = "mptspi",
  1256. .id_table = mptspi_pci_table,
  1257. .probe = mptspi_probe,
  1258. .remove = __devexit_p(mptscsih_remove),
  1259. .shutdown = mptscsih_shutdown,
  1260. #ifdef CONFIG_PM
  1261. .suspend = mptscsih_suspend,
  1262. .resume = mptspi_resume,
  1263. #endif
  1264. };
  1265. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1266. /**
  1267. * mptspi_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
  1268. *
  1269. * Returns 0 for success, non-zero for failure.
  1270. */
  1271. static int __init
  1272. mptspi_init(void)
  1273. {
  1274. show_mptmod_ver(my_NAME, my_VERSION);
  1275. mptspi_transport_template = spi_attach_transport(&mptspi_transport_functions);
  1276. if (!mptspi_transport_template)
  1277. return -ENODEV;
  1278. mptspiDoneCtx = mpt_register(mptscsih_io_done, MPTSPI_DRIVER);
  1279. mptspiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSPI_DRIVER);
  1280. mptspiInternalCtx = mpt_register(mptscsih_scandv_complete, MPTSPI_DRIVER);
  1281. if (mpt_event_register(mptspiDoneCtx, mptspi_event_process) == 0) {
  1282. devtverboseprintk((KERN_INFO MYNAM
  1283. ": Registered for IOC event notifications\n"));
  1284. }
  1285. if (mpt_reset_register(mptspiDoneCtx, mptspi_ioc_reset) == 0) {
  1286. dprintk((KERN_INFO MYNAM
  1287. ": Registered for IOC reset notifications\n"));
  1288. }
  1289. return pci_register_driver(&mptspi_driver);
  1290. }
  1291. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1292. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1293. /**
  1294. * mptspi_exit - Unregisters MPT adapter(s)
  1295. */
  1296. static void __exit
  1297. mptspi_exit(void)
  1298. {
  1299. pci_unregister_driver(&mptspi_driver);
  1300. mpt_reset_deregister(mptspiDoneCtx);
  1301. dprintk((KERN_INFO MYNAM
  1302. ": Deregistered for IOC reset notifications\n"));
  1303. mpt_event_deregister(mptspiDoneCtx);
  1304. dprintk((KERN_INFO MYNAM
  1305. ": Deregistered for IOC event notifications\n"));
  1306. mpt_deregister(mptspiInternalCtx);
  1307. mpt_deregister(mptspiTaskCtx);
  1308. mpt_deregister(mptspiDoneCtx);
  1309. spi_release_transport(mptspi_transport_template);
  1310. }
  1311. module_init(mptspi_init);
  1312. module_exit(mptspi_exit);