scsi_transport_spi.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ctype.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/mutex.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/slab.h>
  29. #include <scsi/scsi.h>
  30. #include "scsi_priv.h"
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_transport.h>
  36. #include <scsi/scsi_transport_spi.h>
  37. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  38. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  39. * on" attributes */
  40. #define SPI_HOST_ATTRS 1
  41. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  42. #define DV_LOOPS 3
  43. #define DV_TIMEOUT (10*HZ)
  44. #define DV_RETRIES 3 /* should only need at most
  45. * two cc/ua clears */
  46. /* Our blacklist flags */
  47. enum {
  48. SPI_BLIST_NOIUS = 0x1,
  49. };
  50. /* blacklist table, modelled on scsi_devinfo.c */
  51. static struct {
  52. char *vendor;
  53. char *model;
  54. unsigned flags;
  55. } spi_static_device_list[] __initdata = {
  56. {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
  57. {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
  58. {NULL, NULL, 0}
  59. };
  60. /* Private data accessors (keep these out of the header file) */
  61. #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  62. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  63. struct spi_internal {
  64. struct scsi_transport_template t;
  65. struct spi_function_template *f;
  66. };
  67. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  68. static const int ppr_to_ps[] = {
  69. /* The PPR values 0-6 are reserved, fill them in when
  70. * the committee defines them */
  71. -1, /* 0x00 */
  72. -1, /* 0x01 */
  73. -1, /* 0x02 */
  74. -1, /* 0x03 */
  75. -1, /* 0x04 */
  76. -1, /* 0x05 */
  77. -1, /* 0x06 */
  78. 3125, /* 0x07 */
  79. 6250, /* 0x08 */
  80. 12500, /* 0x09 */
  81. 25000, /* 0x0a */
  82. 30300, /* 0x0b */
  83. 50000, /* 0x0c */
  84. };
  85. /* The PPR values at which you calculate the period in ns by multiplying
  86. * by 4 */
  87. #define SPI_STATIC_PPR 0x0c
  88. static int sprint_frac(char *dest, int value, int denom)
  89. {
  90. int frac = value % denom;
  91. int result = sprintf(dest, "%d", value / denom);
  92. if (frac == 0)
  93. return result;
  94. dest[result++] = '.';
  95. do {
  96. denom /= 10;
  97. sprintf(dest + result, "%d", frac / denom);
  98. result++;
  99. frac %= denom;
  100. } while (frac);
  101. dest[result++] = '\0';
  102. return result;
  103. }
  104. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  105. enum dma_data_direction dir,
  106. void *buffer, unsigned bufflen,
  107. struct scsi_sense_hdr *sshdr)
  108. {
  109. int i, result;
  110. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  111. for(i = 0; i < DV_RETRIES; i++) {
  112. result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
  113. sense, DV_TIMEOUT, /* retries */ 1,
  114. REQ_FAILFAST_DEV |
  115. REQ_FAILFAST_TRANSPORT |
  116. REQ_FAILFAST_DRIVER,
  117. NULL);
  118. if (driver_byte(result) & DRIVER_SENSE) {
  119. struct scsi_sense_hdr sshdr_tmp;
  120. if (!sshdr)
  121. sshdr = &sshdr_tmp;
  122. if (scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE,
  123. sshdr)
  124. && sshdr->sense_key == UNIT_ATTENTION)
  125. continue;
  126. }
  127. break;
  128. }
  129. return result;
  130. }
  131. static struct {
  132. enum spi_signal_type value;
  133. char *name;
  134. } signal_types[] = {
  135. { SPI_SIGNAL_UNKNOWN, "unknown" },
  136. { SPI_SIGNAL_SE, "SE" },
  137. { SPI_SIGNAL_LVD, "LVD" },
  138. { SPI_SIGNAL_HVD, "HVD" },
  139. };
  140. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  141. {
  142. int i;
  143. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  144. if (type == signal_types[i].value)
  145. return signal_types[i].name;
  146. }
  147. return NULL;
  148. }
  149. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  150. {
  151. int i, len;
  152. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  153. len = strlen(signal_types[i].name);
  154. if (strncmp(name, signal_types[i].name, len) == 0 &&
  155. (name[len] == '\n' || name[len] == '\0'))
  156. return signal_types[i].value;
  157. }
  158. return SPI_SIGNAL_UNKNOWN;
  159. }
  160. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  161. struct device *cdev)
  162. {
  163. struct Scsi_Host *shost = dev_to_shost(dev);
  164. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  165. return 0;
  166. }
  167. static int spi_host_configure(struct transport_container *tc,
  168. struct device *dev,
  169. struct device *cdev);
  170. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  171. "spi_host",
  172. spi_host_setup,
  173. NULL,
  174. spi_host_configure);
  175. static int spi_host_match(struct attribute_container *cont,
  176. struct device *dev)
  177. {
  178. struct Scsi_Host *shost;
  179. if (!scsi_is_host_device(dev))
  180. return 0;
  181. shost = dev_to_shost(dev);
  182. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  183. != &spi_host_class.class)
  184. return 0;
  185. return &shost->transportt->host_attrs.ac == cont;
  186. }
  187. static int spi_target_configure(struct transport_container *tc,
  188. struct device *dev,
  189. struct device *cdev);
  190. static int spi_device_configure(struct transport_container *tc,
  191. struct device *dev,
  192. struct device *cdev)
  193. {
  194. struct scsi_device *sdev = to_scsi_device(dev);
  195. struct scsi_target *starget = sdev->sdev_target;
  196. unsigned bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
  197. &sdev->inquiry[16],
  198. SCSI_DEVINFO_SPI);
  199. /* Populate the target capability fields with the values
  200. * gleaned from the device inquiry */
  201. spi_support_sync(starget) = scsi_device_sync(sdev);
  202. spi_support_wide(starget) = scsi_device_wide(sdev);
  203. spi_support_dt(starget) = scsi_device_dt(sdev);
  204. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  205. spi_support_ius(starget) = scsi_device_ius(sdev);
  206. if (bflags & SPI_BLIST_NOIUS) {
  207. dev_info(dev, "Information Units disabled by blacklist\n");
  208. spi_support_ius(starget) = 0;
  209. }
  210. spi_support_qas(starget) = scsi_device_qas(sdev);
  211. return 0;
  212. }
  213. static int spi_setup_transport_attrs(struct transport_container *tc,
  214. struct device *dev,
  215. struct device *cdev)
  216. {
  217. struct scsi_target *starget = to_scsi_target(dev);
  218. spi_period(starget) = -1; /* illegal value */
  219. spi_min_period(starget) = 0;
  220. spi_offset(starget) = 0; /* async */
  221. spi_max_offset(starget) = 255;
  222. spi_width(starget) = 0; /* narrow */
  223. spi_max_width(starget) = 1;
  224. spi_iu(starget) = 0; /* no IU */
  225. spi_max_iu(starget) = 1;
  226. spi_dt(starget) = 0; /* ST */
  227. spi_qas(starget) = 0;
  228. spi_max_qas(starget) = 1;
  229. spi_wr_flow(starget) = 0;
  230. spi_rd_strm(starget) = 0;
  231. spi_rti(starget) = 0;
  232. spi_pcomp_en(starget) = 0;
  233. spi_hold_mcs(starget) = 0;
  234. spi_dv_pending(starget) = 0;
  235. spi_dv_in_progress(starget) = 0;
  236. spi_initial_dv(starget) = 0;
  237. mutex_init(&spi_dv_mutex(starget));
  238. return 0;
  239. }
  240. #define spi_transport_show_simple(field, format_string) \
  241. \
  242. static ssize_t \
  243. show_spi_transport_##field(struct device *dev, \
  244. struct device_attribute *attr, char *buf) \
  245. { \
  246. struct scsi_target *starget = transport_class_to_starget(dev); \
  247. struct spi_transport_attrs *tp; \
  248. \
  249. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  250. return snprintf(buf, 20, format_string, tp->field); \
  251. }
  252. #define spi_transport_store_simple(field, format_string) \
  253. \
  254. static ssize_t \
  255. store_spi_transport_##field(struct device *dev, \
  256. struct device_attribute *attr, \
  257. const char *buf, size_t count) \
  258. { \
  259. int val; \
  260. struct scsi_target *starget = transport_class_to_starget(dev); \
  261. struct spi_transport_attrs *tp; \
  262. \
  263. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  264. val = simple_strtoul(buf, NULL, 0); \
  265. tp->field = val; \
  266. return count; \
  267. }
  268. #define spi_transport_show_function(field, format_string) \
  269. \
  270. static ssize_t \
  271. show_spi_transport_##field(struct device *dev, \
  272. struct device_attribute *attr, char *buf) \
  273. { \
  274. struct scsi_target *starget = transport_class_to_starget(dev); \
  275. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  276. struct spi_transport_attrs *tp; \
  277. struct spi_internal *i = to_spi_internal(shost->transportt); \
  278. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  279. if (i->f->get_##field) \
  280. i->f->get_##field(starget); \
  281. return snprintf(buf, 20, format_string, tp->field); \
  282. }
  283. #define spi_transport_store_function(field, format_string) \
  284. static ssize_t \
  285. store_spi_transport_##field(struct device *dev, \
  286. struct device_attribute *attr, \
  287. const char *buf, size_t count) \
  288. { \
  289. int val; \
  290. struct scsi_target *starget = transport_class_to_starget(dev); \
  291. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  292. struct spi_internal *i = to_spi_internal(shost->transportt); \
  293. \
  294. if (!i->f->set_##field) \
  295. return -EINVAL; \
  296. val = simple_strtoul(buf, NULL, 0); \
  297. i->f->set_##field(starget, val); \
  298. return count; \
  299. }
  300. #define spi_transport_store_max(field, format_string) \
  301. static ssize_t \
  302. store_spi_transport_##field(struct device *dev, \
  303. struct device_attribute *attr, \
  304. const char *buf, size_t count) \
  305. { \
  306. int val; \
  307. struct scsi_target *starget = transport_class_to_starget(dev); \
  308. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  309. struct spi_internal *i = to_spi_internal(shost->transportt); \
  310. struct spi_transport_attrs *tp \
  311. = (struct spi_transport_attrs *)&starget->starget_data; \
  312. \
  313. if (i->f->set_##field) \
  314. return -EINVAL; \
  315. val = simple_strtoul(buf, NULL, 0); \
  316. if (val > tp->max_##field) \
  317. val = tp->max_##field; \
  318. i->f->set_##field(starget, val); \
  319. return count; \
  320. }
  321. #define spi_transport_rd_attr(field, format_string) \
  322. spi_transport_show_function(field, format_string) \
  323. spi_transport_store_function(field, format_string) \
  324. static DEVICE_ATTR(field, S_IRUGO, \
  325. show_spi_transport_##field, \
  326. store_spi_transport_##field);
  327. #define spi_transport_simple_attr(field, format_string) \
  328. spi_transport_show_simple(field, format_string) \
  329. spi_transport_store_simple(field, format_string) \
  330. static DEVICE_ATTR(field, S_IRUGO, \
  331. show_spi_transport_##field, \
  332. store_spi_transport_##field);
  333. #define spi_transport_max_attr(field, format_string) \
  334. spi_transport_show_function(field, format_string) \
  335. spi_transport_store_max(field, format_string) \
  336. spi_transport_simple_attr(max_##field, format_string) \
  337. static DEVICE_ATTR(field, S_IRUGO, \
  338. show_spi_transport_##field, \
  339. store_spi_transport_##field);
  340. /* The Parallel SCSI Tranport Attributes: */
  341. spi_transport_max_attr(offset, "%d\n");
  342. spi_transport_max_attr(width, "%d\n");
  343. spi_transport_max_attr(iu, "%d\n");
  344. spi_transport_rd_attr(dt, "%d\n");
  345. spi_transport_max_attr(qas, "%d\n");
  346. spi_transport_rd_attr(wr_flow, "%d\n");
  347. spi_transport_rd_attr(rd_strm, "%d\n");
  348. spi_transport_rd_attr(rti, "%d\n");
  349. spi_transport_rd_attr(pcomp_en, "%d\n");
  350. spi_transport_rd_attr(hold_mcs, "%d\n");
  351. /* we only care about the first child device that's a real SCSI device
  352. * so we return 1 to terminate the iteration when we find it */
  353. static int child_iter(struct device *dev, void *data)
  354. {
  355. if (!scsi_is_sdev_device(dev))
  356. return 0;
  357. spi_dv_device(to_scsi_device(dev));
  358. return 1;
  359. }
  360. static ssize_t
  361. store_spi_revalidate(struct device *dev, struct device_attribute *attr,
  362. const char *buf, size_t count)
  363. {
  364. struct scsi_target *starget = transport_class_to_starget(dev);
  365. device_for_each_child(&starget->dev, NULL, child_iter);
  366. return count;
  367. }
  368. static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  369. /* Translate the period into ns according to the current spec
  370. * for SDTR/PPR messages */
  371. static int period_to_str(char *buf, int period)
  372. {
  373. int len, picosec;
  374. if (period < 0 || period > 0xff) {
  375. picosec = -1;
  376. } else if (period <= SPI_STATIC_PPR) {
  377. picosec = ppr_to_ps[period];
  378. } else {
  379. picosec = period * 4000;
  380. }
  381. if (picosec == -1) {
  382. len = sprintf(buf, "reserved");
  383. } else {
  384. len = sprint_frac(buf, picosec, 1000);
  385. }
  386. return len;
  387. }
  388. static ssize_t
  389. show_spi_transport_period_helper(char *buf, int period)
  390. {
  391. int len = period_to_str(buf, period);
  392. buf[len++] = '\n';
  393. buf[len] = '\0';
  394. return len;
  395. }
  396. static ssize_t
  397. store_spi_transport_period_helper(struct device *dev, const char *buf,
  398. size_t count, int *periodp)
  399. {
  400. int j, picosec, period = -1;
  401. char *endp;
  402. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  403. if (*endp == '.') {
  404. int mult = 100;
  405. do {
  406. endp++;
  407. if (!isdigit(*endp))
  408. break;
  409. picosec += (*endp - '0') * mult;
  410. mult /= 10;
  411. } while (mult > 0);
  412. }
  413. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  414. if (ppr_to_ps[j] < picosec)
  415. continue;
  416. period = j;
  417. break;
  418. }
  419. if (period == -1)
  420. period = picosec / 4000;
  421. if (period > 0xff)
  422. period = 0xff;
  423. *periodp = period;
  424. return count;
  425. }
  426. static ssize_t
  427. show_spi_transport_period(struct device *dev,
  428. struct device_attribute *attr, char *buf)
  429. {
  430. struct scsi_target *starget = transport_class_to_starget(dev);
  431. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  432. struct spi_internal *i = to_spi_internal(shost->transportt);
  433. struct spi_transport_attrs *tp =
  434. (struct spi_transport_attrs *)&starget->starget_data;
  435. if (i->f->get_period)
  436. i->f->get_period(starget);
  437. return show_spi_transport_period_helper(buf, tp->period);
  438. }
  439. static ssize_t
  440. store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
  441. const char *buf, size_t count)
  442. {
  443. struct scsi_target *starget = transport_class_to_starget(cdev);
  444. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  445. struct spi_internal *i = to_spi_internal(shost->transportt);
  446. struct spi_transport_attrs *tp =
  447. (struct spi_transport_attrs *)&starget->starget_data;
  448. int period, retval;
  449. if (!i->f->set_period)
  450. return -EINVAL;
  451. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  452. if (period < tp->min_period)
  453. period = tp->min_period;
  454. i->f->set_period(starget, period);
  455. return retval;
  456. }
  457. static DEVICE_ATTR(period, S_IRUGO,
  458. show_spi_transport_period,
  459. store_spi_transport_period);
  460. static ssize_t
  461. show_spi_transport_min_period(struct device *cdev,
  462. struct device_attribute *attr, char *buf)
  463. {
  464. struct scsi_target *starget = transport_class_to_starget(cdev);
  465. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  466. struct spi_internal *i = to_spi_internal(shost->transportt);
  467. struct spi_transport_attrs *tp =
  468. (struct spi_transport_attrs *)&starget->starget_data;
  469. if (!i->f->set_period)
  470. return -EINVAL;
  471. return show_spi_transport_period_helper(buf, tp->min_period);
  472. }
  473. static ssize_t
  474. store_spi_transport_min_period(struct device *cdev,
  475. struct device_attribute *attr,
  476. const char *buf, size_t count)
  477. {
  478. struct scsi_target *starget = transport_class_to_starget(cdev);
  479. struct spi_transport_attrs *tp =
  480. (struct spi_transport_attrs *)&starget->starget_data;
  481. return store_spi_transport_period_helper(cdev, buf, count,
  482. &tp->min_period);
  483. }
  484. static DEVICE_ATTR(min_period, S_IRUGO,
  485. show_spi_transport_min_period,
  486. store_spi_transport_min_period);
  487. static ssize_t show_spi_host_signalling(struct device *cdev,
  488. struct device_attribute *attr,
  489. char *buf)
  490. {
  491. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  492. struct spi_internal *i = to_spi_internal(shost->transportt);
  493. if (i->f->get_signalling)
  494. i->f->get_signalling(shost);
  495. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  496. }
  497. static ssize_t store_spi_host_signalling(struct device *dev,
  498. struct device_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. struct Scsi_Host *shost = transport_class_to_shost(dev);
  502. struct spi_internal *i = to_spi_internal(shost->transportt);
  503. enum spi_signal_type type = spi_signal_to_value(buf);
  504. if (!i->f->set_signalling)
  505. return -EINVAL;
  506. if (type != SPI_SIGNAL_UNKNOWN)
  507. i->f->set_signalling(shost, type);
  508. return count;
  509. }
  510. static DEVICE_ATTR(signalling, S_IRUGO,
  511. show_spi_host_signalling,
  512. store_spi_host_signalling);
  513. #define DV_SET(x, y) \
  514. if(i->f->set_##x) \
  515. i->f->set_##x(sdev->sdev_target, y)
  516. enum spi_compare_returns {
  517. SPI_COMPARE_SUCCESS,
  518. SPI_COMPARE_FAILURE,
  519. SPI_COMPARE_SKIP_TEST,
  520. };
  521. /* This is for read/write Domain Validation: If the device supports
  522. * an echo buffer, we do read/write tests to it */
  523. static enum spi_compare_returns
  524. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  525. u8 *ptr, const int retries)
  526. {
  527. int len = ptr - buffer;
  528. int j, k, r, result;
  529. unsigned int pattern = 0x0000ffff;
  530. struct scsi_sense_hdr sshdr;
  531. const char spi_write_buffer[] = {
  532. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  533. };
  534. const char spi_read_buffer[] = {
  535. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  536. };
  537. /* set up the pattern buffer. Doesn't matter if we spill
  538. * slightly beyond since that's where the read buffer is */
  539. for (j = 0; j < len; ) {
  540. /* fill the buffer with counting (test a) */
  541. for ( ; j < min(len, 32); j++)
  542. buffer[j] = j;
  543. k = j;
  544. /* fill the buffer with alternating words of 0x0 and
  545. * 0xffff (test b) */
  546. for ( ; j < min(len, k + 32); j += 2) {
  547. u16 *word = (u16 *)&buffer[j];
  548. *word = (j & 0x02) ? 0x0000 : 0xffff;
  549. }
  550. k = j;
  551. /* fill with crosstalk (alternating 0x5555 0xaaa)
  552. * (test c) */
  553. for ( ; j < min(len, k + 32); j += 2) {
  554. u16 *word = (u16 *)&buffer[j];
  555. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  556. }
  557. k = j;
  558. /* fill with shifting bits (test d) */
  559. for ( ; j < min(len, k + 32); j += 4) {
  560. u32 *word = (unsigned int *)&buffer[j];
  561. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  562. *word = pattern;
  563. pattern = (pattern << 1) | roll;
  564. }
  565. /* don't bother with random data (test e) */
  566. }
  567. for (r = 0; r < retries; r++) {
  568. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  569. buffer, len, &sshdr);
  570. if(result || !scsi_device_online(sdev)) {
  571. scsi_device_set_state(sdev, SDEV_QUIESCE);
  572. if (scsi_sense_valid(&sshdr)
  573. && sshdr.sense_key == ILLEGAL_REQUEST
  574. /* INVALID FIELD IN CDB */
  575. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  576. /* This would mean that the drive lied
  577. * to us about supporting an echo
  578. * buffer (unfortunately some Western
  579. * Digital drives do precisely this)
  580. */
  581. return SPI_COMPARE_SKIP_TEST;
  582. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  583. return SPI_COMPARE_FAILURE;
  584. }
  585. memset(ptr, 0, len);
  586. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  587. ptr, len, NULL);
  588. scsi_device_set_state(sdev, SDEV_QUIESCE);
  589. if (memcmp(buffer, ptr, len) != 0)
  590. return SPI_COMPARE_FAILURE;
  591. }
  592. return SPI_COMPARE_SUCCESS;
  593. }
  594. /* This is for the simplest form of Domain Validation: a read test
  595. * on the inquiry data from the device */
  596. static enum spi_compare_returns
  597. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  598. u8 *ptr, const int retries)
  599. {
  600. int r, result;
  601. const int len = sdev->inquiry_len;
  602. const char spi_inquiry[] = {
  603. INQUIRY, 0, 0, 0, len, 0
  604. };
  605. for (r = 0; r < retries; r++) {
  606. memset(ptr, 0, len);
  607. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  608. ptr, len, NULL);
  609. if(result || !scsi_device_online(sdev)) {
  610. scsi_device_set_state(sdev, SDEV_QUIESCE);
  611. return SPI_COMPARE_FAILURE;
  612. }
  613. /* If we don't have the inquiry data already, the
  614. * first read gets it */
  615. if (ptr == buffer) {
  616. ptr += len;
  617. --r;
  618. continue;
  619. }
  620. if (memcmp(buffer, ptr, len) != 0)
  621. /* failure */
  622. return SPI_COMPARE_FAILURE;
  623. }
  624. return SPI_COMPARE_SUCCESS;
  625. }
  626. static enum spi_compare_returns
  627. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  628. enum spi_compare_returns
  629. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  630. {
  631. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  632. struct scsi_target *starget = sdev->sdev_target;
  633. int period = 0, prevperiod = 0;
  634. enum spi_compare_returns retval;
  635. for (;;) {
  636. int newperiod;
  637. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  638. if (retval == SPI_COMPARE_SUCCESS
  639. || retval == SPI_COMPARE_SKIP_TEST)
  640. break;
  641. /* OK, retrain, fallback */
  642. if (i->f->get_iu)
  643. i->f->get_iu(starget);
  644. if (i->f->get_qas)
  645. i->f->get_qas(starget);
  646. if (i->f->get_period)
  647. i->f->get_period(sdev->sdev_target);
  648. /* Here's the fallback sequence; first try turning off
  649. * IU, then QAS (if we can control them), then finally
  650. * fall down the periods */
  651. if (i->f->set_iu && spi_iu(starget)) {
  652. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
  653. DV_SET(iu, 0);
  654. } else if (i->f->set_qas && spi_qas(starget)) {
  655. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
  656. DV_SET(qas, 0);
  657. } else {
  658. newperiod = spi_period(starget);
  659. period = newperiod > period ? newperiod : period;
  660. if (period < 0x0d)
  661. period++;
  662. else
  663. period += period >> 1;
  664. if (unlikely(period > 0xff || period == prevperiod)) {
  665. /* Total failure; set to async and return */
  666. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  667. DV_SET(offset, 0);
  668. return SPI_COMPARE_FAILURE;
  669. }
  670. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  671. DV_SET(period, period);
  672. prevperiod = period;
  673. }
  674. }
  675. return retval;
  676. }
  677. static int
  678. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  679. {
  680. int l, result;
  681. /* first off do a test unit ready. This can error out
  682. * because of reservations or some other reason. If it
  683. * fails, the device won't let us write to the echo buffer
  684. * so just return failure */
  685. const char spi_test_unit_ready[] = {
  686. TEST_UNIT_READY, 0, 0, 0, 0, 0
  687. };
  688. const char spi_read_buffer_descriptor[] = {
  689. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  690. };
  691. /* We send a set of three TURs to clear any outstanding
  692. * unit attention conditions if they exist (Otherwise the
  693. * buffer tests won't be happy). If the TUR still fails
  694. * (reservation conflict, device not ready, etc) just
  695. * skip the write tests */
  696. for (l = 0; ; l++) {
  697. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  698. NULL, 0, NULL);
  699. if(result) {
  700. if(l >= 3)
  701. return 0;
  702. } else {
  703. /* TUR succeeded */
  704. break;
  705. }
  706. }
  707. result = spi_execute(sdev, spi_read_buffer_descriptor,
  708. DMA_FROM_DEVICE, buffer, 4, NULL);
  709. if (result)
  710. /* Device has no echo buffer */
  711. return 0;
  712. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  713. }
  714. static void
  715. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  716. {
  717. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  718. struct scsi_target *starget = sdev->sdev_target;
  719. struct Scsi_Host *shost = sdev->host;
  720. int len = sdev->inquiry_len;
  721. int min_period = spi_min_period(starget);
  722. int max_width = spi_max_width(starget);
  723. /* first set us up for narrow async */
  724. DV_SET(offset, 0);
  725. DV_SET(width, 0);
  726. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  727. != SPI_COMPARE_SUCCESS) {
  728. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  729. /* FIXME: should probably offline the device here? */
  730. return;
  731. }
  732. if (!spi_support_wide(starget)) {
  733. spi_max_width(starget) = 0;
  734. max_width = 0;
  735. }
  736. /* test width */
  737. if (i->f->set_width && max_width) {
  738. i->f->set_width(starget, 1);
  739. if (spi_dv_device_compare_inquiry(sdev, buffer,
  740. buffer + len,
  741. DV_LOOPS)
  742. != SPI_COMPARE_SUCCESS) {
  743. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  744. i->f->set_width(starget, 0);
  745. /* Make sure we don't force wide back on by asking
  746. * for a transfer period that requires it */
  747. max_width = 0;
  748. if (min_period < 10)
  749. min_period = 10;
  750. }
  751. }
  752. if (!i->f->set_period)
  753. return;
  754. /* device can't handle synchronous */
  755. if (!spi_support_sync(starget) && !spi_support_dt(starget))
  756. return;
  757. /* len == -1 is the signal that we need to ascertain the
  758. * presence of an echo buffer before trying to use it. len ==
  759. * 0 means we don't have an echo buffer */
  760. len = -1;
  761. retry:
  762. /* now set up to the maximum */
  763. DV_SET(offset, spi_max_offset(starget));
  764. DV_SET(period, min_period);
  765. /* try QAS requests; this should be harmless to set if the
  766. * target supports it */
  767. if (spi_support_qas(starget) && spi_max_qas(starget)) {
  768. DV_SET(qas, 1);
  769. } else {
  770. DV_SET(qas, 0);
  771. }
  772. if (spi_support_ius(starget) && spi_max_iu(starget) &&
  773. min_period < 9) {
  774. /* This u320 (or u640). Set IU transfers */
  775. DV_SET(iu, 1);
  776. /* Then set the optional parameters */
  777. DV_SET(rd_strm, 1);
  778. DV_SET(wr_flow, 1);
  779. DV_SET(rti, 1);
  780. if (min_period == 8)
  781. DV_SET(pcomp_en, 1);
  782. } else {
  783. DV_SET(iu, 0);
  784. }
  785. /* now that we've done all this, actually check the bus
  786. * signal type (if known). Some devices are stupid on
  787. * a SE bus and still claim they can try LVD only settings */
  788. if (i->f->get_signalling)
  789. i->f->get_signalling(shost);
  790. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  791. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  792. !spi_support_dt(starget)) {
  793. DV_SET(dt, 0);
  794. } else {
  795. DV_SET(dt, 1);
  796. }
  797. /* set width last because it will pull all the other
  798. * parameters down to required values */
  799. DV_SET(width, max_width);
  800. /* Do the read only INQUIRY tests */
  801. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  802. spi_dv_device_compare_inquiry);
  803. /* See if we actually managed to negotiate and sustain DT */
  804. if (i->f->get_dt)
  805. i->f->get_dt(starget);
  806. /* see if the device has an echo buffer. If it does we can do
  807. * the SPI pattern write tests. Because of some broken
  808. * devices, we *only* try this on a device that has actually
  809. * negotiated DT */
  810. if (len == -1 && spi_dt(starget))
  811. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  812. if (len <= 0) {
  813. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  814. return;
  815. }
  816. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  817. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  818. len = SPI_MAX_ECHO_BUFFER_SIZE;
  819. }
  820. if (spi_dv_retrain(sdev, buffer, buffer + len,
  821. spi_dv_device_echo_buffer)
  822. == SPI_COMPARE_SKIP_TEST) {
  823. /* OK, the stupid drive can't do a write echo buffer
  824. * test after all, fall back to the read tests */
  825. len = 0;
  826. goto retry;
  827. }
  828. }
  829. /** spi_dv_device - Do Domain Validation on the device
  830. * @sdev: scsi device to validate
  831. *
  832. * Performs the domain validation on the given device in the
  833. * current execution thread. Since DV operations may sleep,
  834. * the current thread must have user context. Also no SCSI
  835. * related locks that would deadlock I/O issued by the DV may
  836. * be held.
  837. */
  838. void
  839. spi_dv_device(struct scsi_device *sdev)
  840. {
  841. struct scsi_target *starget = sdev->sdev_target;
  842. u8 *buffer;
  843. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  844. if (unlikely(scsi_device_get(sdev)))
  845. return;
  846. if (unlikely(spi_dv_in_progress(starget)))
  847. return;
  848. spi_dv_in_progress(starget) = 1;
  849. buffer = kzalloc(len, GFP_KERNEL);
  850. if (unlikely(!buffer))
  851. goto out_put;
  852. /* We need to verify that the actual device will quiesce; the
  853. * later target quiesce is just a nice to have */
  854. if (unlikely(scsi_device_quiesce(sdev)))
  855. goto out_free;
  856. scsi_target_quiesce(starget);
  857. spi_dv_pending(starget) = 1;
  858. mutex_lock(&spi_dv_mutex(starget));
  859. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  860. spi_dv_device_internal(sdev, buffer);
  861. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  862. mutex_unlock(&spi_dv_mutex(starget));
  863. spi_dv_pending(starget) = 0;
  864. scsi_target_resume(starget);
  865. spi_initial_dv(starget) = 1;
  866. out_free:
  867. kfree(buffer);
  868. out_put:
  869. spi_dv_in_progress(starget) = 0;
  870. scsi_device_put(sdev);
  871. }
  872. EXPORT_SYMBOL(spi_dv_device);
  873. struct work_queue_wrapper {
  874. struct work_struct work;
  875. struct scsi_device *sdev;
  876. };
  877. static void
  878. spi_dv_device_work_wrapper(struct work_struct *work)
  879. {
  880. struct work_queue_wrapper *wqw =
  881. container_of(work, struct work_queue_wrapper, work);
  882. struct scsi_device *sdev = wqw->sdev;
  883. kfree(wqw);
  884. spi_dv_device(sdev);
  885. spi_dv_pending(sdev->sdev_target) = 0;
  886. scsi_device_put(sdev);
  887. }
  888. /**
  889. * spi_schedule_dv_device - schedule domain validation to occur on the device
  890. * @sdev: The device to validate
  891. *
  892. * Identical to spi_dv_device() above, except that the DV will be
  893. * scheduled to occur in a workqueue later. All memory allocations
  894. * are atomic, so may be called from any context including those holding
  895. * SCSI locks.
  896. */
  897. void
  898. spi_schedule_dv_device(struct scsi_device *sdev)
  899. {
  900. struct work_queue_wrapper *wqw =
  901. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  902. if (unlikely(!wqw))
  903. return;
  904. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  905. kfree(wqw);
  906. return;
  907. }
  908. /* Set pending early (dv_device doesn't check it, only sets it) */
  909. spi_dv_pending(sdev->sdev_target) = 1;
  910. if (unlikely(scsi_device_get(sdev))) {
  911. kfree(wqw);
  912. spi_dv_pending(sdev->sdev_target) = 0;
  913. return;
  914. }
  915. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  916. wqw->sdev = sdev;
  917. schedule_work(&wqw->work);
  918. }
  919. EXPORT_SYMBOL(spi_schedule_dv_device);
  920. /**
  921. * spi_display_xfer_agreement - Print the current target transfer agreement
  922. * @starget: The target for which to display the agreement
  923. *
  924. * Each SPI port is required to maintain a transfer agreement for each
  925. * other port on the bus. This function prints a one-line summary of
  926. * the current agreement; more detailed information is available in sysfs.
  927. */
  928. void spi_display_xfer_agreement(struct scsi_target *starget)
  929. {
  930. struct spi_transport_attrs *tp;
  931. tp = (struct spi_transport_attrs *)&starget->starget_data;
  932. if (tp->offset > 0 && tp->period > 0) {
  933. unsigned int picosec, kb100;
  934. char *scsi = "FAST-?";
  935. char tmp[8];
  936. if (tp->period <= SPI_STATIC_PPR) {
  937. picosec = ppr_to_ps[tp->period];
  938. switch (tp->period) {
  939. case 7: scsi = "FAST-320"; break;
  940. case 8: scsi = "FAST-160"; break;
  941. case 9: scsi = "FAST-80"; break;
  942. case 10:
  943. case 11: scsi = "FAST-40"; break;
  944. case 12: scsi = "FAST-20"; break;
  945. }
  946. } else {
  947. picosec = tp->period * 4000;
  948. if (tp->period < 25)
  949. scsi = "FAST-20";
  950. else if (tp->period < 50)
  951. scsi = "FAST-10";
  952. else
  953. scsi = "FAST-5";
  954. }
  955. kb100 = (10000000 + picosec / 2) / picosec;
  956. if (tp->width)
  957. kb100 *= 2;
  958. sprint_frac(tmp, picosec, 1000);
  959. dev_info(&starget->dev,
  960. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  961. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  962. tp->dt ? "DT" : "ST",
  963. tp->iu ? " IU" : "",
  964. tp->qas ? " QAS" : "",
  965. tp->rd_strm ? " RDSTRM" : "",
  966. tp->rti ? " RTI" : "",
  967. tp->wr_flow ? " WRFLOW" : "",
  968. tp->pcomp_en ? " PCOMP" : "",
  969. tp->hold_mcs ? " HMCS" : "",
  970. tmp, tp->offset);
  971. } else {
  972. dev_info(&starget->dev, "%sasynchronous\n",
  973. tp->width ? "wide " : "");
  974. }
  975. }
  976. EXPORT_SYMBOL(spi_display_xfer_agreement);
  977. int spi_populate_width_msg(unsigned char *msg, int width)
  978. {
  979. msg[0] = EXTENDED_MESSAGE;
  980. msg[1] = 2;
  981. msg[2] = EXTENDED_WDTR;
  982. msg[3] = width;
  983. return 4;
  984. }
  985. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  986. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  987. {
  988. msg[0] = EXTENDED_MESSAGE;
  989. msg[1] = 3;
  990. msg[2] = EXTENDED_SDTR;
  991. msg[3] = period;
  992. msg[4] = offset;
  993. return 5;
  994. }
  995. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  996. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  997. int width, int options)
  998. {
  999. msg[0] = EXTENDED_MESSAGE;
  1000. msg[1] = 6;
  1001. msg[2] = EXTENDED_PPR;
  1002. msg[3] = period;
  1003. msg[4] = 0;
  1004. msg[5] = offset;
  1005. msg[6] = width;
  1006. msg[7] = options;
  1007. return 8;
  1008. }
  1009. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  1010. #ifdef CONFIG_SCSI_CONSTANTS
  1011. static const char * const one_byte_msgs[] = {
  1012. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  1013. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  1014. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  1015. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  1016. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  1017. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  1018. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  1019. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  1020. };
  1021. static const char * const two_byte_msgs[] = {
  1022. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  1023. /* 0x23 */ "Ignore Wide Residue", "ACA"
  1024. };
  1025. static const char * const extended_msgs[] = {
  1026. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  1027. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  1028. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  1029. };
  1030. static void print_nego(const unsigned char *msg, int per, int off, int width)
  1031. {
  1032. if (per) {
  1033. char buf[20];
  1034. period_to_str(buf, msg[per]);
  1035. printk("period = %s ns ", buf);
  1036. }
  1037. if (off)
  1038. printk("offset = %d ", msg[off]);
  1039. if (width)
  1040. printk("width = %d ", 8 << msg[width]);
  1041. }
  1042. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  1043. {
  1044. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  1045. msg[msb+3];
  1046. printk("%s = %d ", desc, ptr);
  1047. }
  1048. int spi_print_msg(const unsigned char *msg)
  1049. {
  1050. int len = 1, i;
  1051. if (msg[0] == EXTENDED_MESSAGE) {
  1052. len = 2 + msg[1];
  1053. if (len == 2)
  1054. len += 256;
  1055. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1056. printk ("%s ", extended_msgs[msg[2]]);
  1057. else
  1058. printk ("Extended Message, reserved code (0x%02x) ",
  1059. (int) msg[2]);
  1060. switch (msg[2]) {
  1061. case EXTENDED_MODIFY_DATA_POINTER:
  1062. print_ptr(msg, 3, "pointer");
  1063. break;
  1064. case EXTENDED_SDTR:
  1065. print_nego(msg, 3, 4, 0);
  1066. break;
  1067. case EXTENDED_WDTR:
  1068. print_nego(msg, 0, 0, 3);
  1069. break;
  1070. case EXTENDED_PPR:
  1071. print_nego(msg, 3, 5, 6);
  1072. break;
  1073. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1074. print_ptr(msg, 3, "out");
  1075. print_ptr(msg, 7, "in");
  1076. break;
  1077. default:
  1078. for (i = 2; i < len; ++i)
  1079. printk("%02x ", msg[i]);
  1080. }
  1081. /* Identify */
  1082. } else if (msg[0] & 0x80) {
  1083. printk("Identify disconnect %sallowed %s %d ",
  1084. (msg[0] & 0x40) ? "" : "not ",
  1085. (msg[0] & 0x20) ? "target routine" : "lun",
  1086. msg[0] & 0x7);
  1087. /* Normal One byte */
  1088. } else if (msg[0] < 0x1f) {
  1089. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1090. printk("%s ", one_byte_msgs[msg[0]]);
  1091. else
  1092. printk("reserved (%02x) ", msg[0]);
  1093. } else if (msg[0] == 0x55) {
  1094. printk("QAS Request ");
  1095. /* Two byte */
  1096. } else if (msg[0] <= 0x2f) {
  1097. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1098. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1099. msg[1]);
  1100. else
  1101. printk("reserved two byte (%02x %02x) ",
  1102. msg[0], msg[1]);
  1103. len = 2;
  1104. } else
  1105. printk("reserved ");
  1106. return len;
  1107. }
  1108. EXPORT_SYMBOL(spi_print_msg);
  1109. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1110. int spi_print_msg(const unsigned char *msg)
  1111. {
  1112. int len = 1, i;
  1113. if (msg[0] == EXTENDED_MESSAGE) {
  1114. len = 2 + msg[1];
  1115. if (len == 2)
  1116. len += 256;
  1117. for (i = 0; i < len; ++i)
  1118. printk("%02x ", msg[i]);
  1119. /* Identify */
  1120. } else if (msg[0] & 0x80) {
  1121. printk("%02x ", msg[0]);
  1122. /* Normal One byte */
  1123. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1124. printk("%02x ", msg[0]);
  1125. /* Two byte */
  1126. } else if (msg[0] <= 0x2f) {
  1127. printk("%02x %02x", msg[0], msg[1]);
  1128. len = 2;
  1129. } else
  1130. printk("%02x ", msg[0]);
  1131. return len;
  1132. }
  1133. EXPORT_SYMBOL(spi_print_msg);
  1134. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1135. static int spi_device_match(struct attribute_container *cont,
  1136. struct device *dev)
  1137. {
  1138. struct scsi_device *sdev;
  1139. struct Scsi_Host *shost;
  1140. struct spi_internal *i;
  1141. if (!scsi_is_sdev_device(dev))
  1142. return 0;
  1143. sdev = to_scsi_device(dev);
  1144. shost = sdev->host;
  1145. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1146. != &spi_host_class.class)
  1147. return 0;
  1148. /* Note: this class has no device attributes, so it has
  1149. * no per-HBA allocation and thus we don't need to distinguish
  1150. * the attribute containers for the device */
  1151. i = to_spi_internal(shost->transportt);
  1152. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1153. return 0;
  1154. return 1;
  1155. }
  1156. static int spi_target_match(struct attribute_container *cont,
  1157. struct device *dev)
  1158. {
  1159. struct Scsi_Host *shost;
  1160. struct scsi_target *starget;
  1161. struct spi_internal *i;
  1162. if (!scsi_is_target_device(dev))
  1163. return 0;
  1164. shost = dev_to_shost(dev->parent);
  1165. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1166. != &spi_host_class.class)
  1167. return 0;
  1168. i = to_spi_internal(shost->transportt);
  1169. starget = to_scsi_target(dev);
  1170. if (i->f->deny_binding && i->f->deny_binding(starget))
  1171. return 0;
  1172. return &i->t.target_attrs.ac == cont;
  1173. }
  1174. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1175. "spi_transport",
  1176. spi_setup_transport_attrs,
  1177. NULL,
  1178. spi_target_configure);
  1179. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1180. spi_device_match,
  1181. spi_device_configure);
  1182. static struct attribute *host_attributes[] = {
  1183. &dev_attr_signalling.attr,
  1184. NULL
  1185. };
  1186. static struct attribute_group host_attribute_group = {
  1187. .attrs = host_attributes,
  1188. };
  1189. static int spi_host_configure(struct transport_container *tc,
  1190. struct device *dev,
  1191. struct device *cdev)
  1192. {
  1193. struct kobject *kobj = &cdev->kobj;
  1194. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1195. struct spi_internal *si = to_spi_internal(shost->transportt);
  1196. struct attribute *attr = &dev_attr_signalling.attr;
  1197. int rc = 0;
  1198. if (si->f->set_signalling)
  1199. rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
  1200. return rc;
  1201. }
  1202. /* returns true if we should be showing the variable. Also
  1203. * overloads the return by setting 1<<1 if the attribute should
  1204. * be writeable */
  1205. #define TARGET_ATTRIBUTE_HELPER(name) \
  1206. (si->f->show_##name ? S_IRUGO : 0) | \
  1207. (si->f->set_##name ? S_IWUSR : 0)
  1208. static mode_t target_attribute_is_visible(struct kobject *kobj,
  1209. struct attribute *attr, int i)
  1210. {
  1211. struct device *cdev = container_of(kobj, struct device, kobj);
  1212. struct scsi_target *starget = transport_class_to_starget(cdev);
  1213. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1214. struct spi_internal *si = to_spi_internal(shost->transportt);
  1215. if (attr == &dev_attr_period.attr &&
  1216. spi_support_sync(starget))
  1217. return TARGET_ATTRIBUTE_HELPER(period);
  1218. else if (attr == &dev_attr_min_period.attr &&
  1219. spi_support_sync(starget))
  1220. return TARGET_ATTRIBUTE_HELPER(period);
  1221. else if (attr == &dev_attr_offset.attr &&
  1222. spi_support_sync(starget))
  1223. return TARGET_ATTRIBUTE_HELPER(offset);
  1224. else if (attr == &dev_attr_max_offset.attr &&
  1225. spi_support_sync(starget))
  1226. return TARGET_ATTRIBUTE_HELPER(offset);
  1227. else if (attr == &dev_attr_width.attr &&
  1228. spi_support_wide(starget))
  1229. return TARGET_ATTRIBUTE_HELPER(width);
  1230. else if (attr == &dev_attr_max_width.attr &&
  1231. spi_support_wide(starget))
  1232. return TARGET_ATTRIBUTE_HELPER(width);
  1233. else if (attr == &dev_attr_iu.attr &&
  1234. spi_support_ius(starget))
  1235. return TARGET_ATTRIBUTE_HELPER(iu);
  1236. else if (attr == &dev_attr_max_iu.attr &&
  1237. spi_support_ius(starget))
  1238. return TARGET_ATTRIBUTE_HELPER(iu);
  1239. else if (attr == &dev_attr_dt.attr &&
  1240. spi_support_dt(starget))
  1241. return TARGET_ATTRIBUTE_HELPER(dt);
  1242. else if (attr == &dev_attr_qas.attr &&
  1243. spi_support_qas(starget))
  1244. return TARGET_ATTRIBUTE_HELPER(qas);
  1245. else if (attr == &dev_attr_max_qas.attr &&
  1246. spi_support_qas(starget))
  1247. return TARGET_ATTRIBUTE_HELPER(qas);
  1248. else if (attr == &dev_attr_wr_flow.attr &&
  1249. spi_support_ius(starget))
  1250. return TARGET_ATTRIBUTE_HELPER(wr_flow);
  1251. else if (attr == &dev_attr_rd_strm.attr &&
  1252. spi_support_ius(starget))
  1253. return TARGET_ATTRIBUTE_HELPER(rd_strm);
  1254. else if (attr == &dev_attr_rti.attr &&
  1255. spi_support_ius(starget))
  1256. return TARGET_ATTRIBUTE_HELPER(rti);
  1257. else if (attr == &dev_attr_pcomp_en.attr &&
  1258. spi_support_ius(starget))
  1259. return TARGET_ATTRIBUTE_HELPER(pcomp_en);
  1260. else if (attr == &dev_attr_hold_mcs.attr &&
  1261. spi_support_ius(starget))
  1262. return TARGET_ATTRIBUTE_HELPER(hold_mcs);
  1263. else if (attr == &dev_attr_revalidate.attr)
  1264. return S_IWUSR;
  1265. return 0;
  1266. }
  1267. static struct attribute *target_attributes[] = {
  1268. &dev_attr_period.attr,
  1269. &dev_attr_min_period.attr,
  1270. &dev_attr_offset.attr,
  1271. &dev_attr_max_offset.attr,
  1272. &dev_attr_width.attr,
  1273. &dev_attr_max_width.attr,
  1274. &dev_attr_iu.attr,
  1275. &dev_attr_max_iu.attr,
  1276. &dev_attr_dt.attr,
  1277. &dev_attr_qas.attr,
  1278. &dev_attr_max_qas.attr,
  1279. &dev_attr_wr_flow.attr,
  1280. &dev_attr_rd_strm.attr,
  1281. &dev_attr_rti.attr,
  1282. &dev_attr_pcomp_en.attr,
  1283. &dev_attr_hold_mcs.attr,
  1284. &dev_attr_revalidate.attr,
  1285. NULL
  1286. };
  1287. static struct attribute_group target_attribute_group = {
  1288. .attrs = target_attributes,
  1289. .is_visible = target_attribute_is_visible,
  1290. };
  1291. static int spi_target_configure(struct transport_container *tc,
  1292. struct device *dev,
  1293. struct device *cdev)
  1294. {
  1295. struct kobject *kobj = &cdev->kobj;
  1296. /* force an update based on parameters read from the device */
  1297. sysfs_update_group(kobj, &target_attribute_group);
  1298. return 0;
  1299. }
  1300. struct scsi_transport_template *
  1301. spi_attach_transport(struct spi_function_template *ft)
  1302. {
  1303. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1304. GFP_KERNEL);
  1305. if (unlikely(!i))
  1306. return NULL;
  1307. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1308. i->t.target_attrs.ac.grp = &target_attribute_group;
  1309. i->t.target_attrs.ac.match = spi_target_match;
  1310. transport_container_register(&i->t.target_attrs);
  1311. i->t.target_size = sizeof(struct spi_transport_attrs);
  1312. i->t.host_attrs.ac.class = &spi_host_class.class;
  1313. i->t.host_attrs.ac.grp = &host_attribute_group;
  1314. i->t.host_attrs.ac.match = spi_host_match;
  1315. transport_container_register(&i->t.host_attrs);
  1316. i->t.host_size = sizeof(struct spi_host_attrs);
  1317. i->f = ft;
  1318. return &i->t;
  1319. }
  1320. EXPORT_SYMBOL(spi_attach_transport);
  1321. void spi_release_transport(struct scsi_transport_template *t)
  1322. {
  1323. struct spi_internal *i = to_spi_internal(t);
  1324. transport_container_unregister(&i->t.target_attrs);
  1325. transport_container_unregister(&i->t.host_attrs);
  1326. kfree(i);
  1327. }
  1328. EXPORT_SYMBOL(spi_release_transport);
  1329. static __init int spi_transport_init(void)
  1330. {
  1331. int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
  1332. "SCSI Parallel Transport Class");
  1333. if (!error) {
  1334. int i;
  1335. for (i = 0; spi_static_device_list[i].vendor; i++)
  1336. scsi_dev_info_list_add_keyed(1, /* compatible */
  1337. spi_static_device_list[i].vendor,
  1338. spi_static_device_list[i].model,
  1339. NULL,
  1340. spi_static_device_list[i].flags,
  1341. SCSI_DEVINFO_SPI);
  1342. }
  1343. error = transport_class_register(&spi_transport_class);
  1344. if (error)
  1345. return error;
  1346. error = anon_transport_class_register(&spi_device_class);
  1347. return transport_class_register(&spi_host_class);
  1348. }
  1349. static void __exit spi_transport_exit(void)
  1350. {
  1351. transport_class_unregister(&spi_transport_class);
  1352. anon_transport_class_unregister(&spi_device_class);
  1353. transport_class_unregister(&spi_host_class);
  1354. scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
  1355. }
  1356. MODULE_AUTHOR("Martin Hicks");
  1357. MODULE_DESCRIPTION("SPI Transport Attributes");
  1358. MODULE_LICENSE("GPL");
  1359. module_init(spi_transport_init);
  1360. module_exit(spi_transport_exit);