scsi_transport_spi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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 <asm/semaphore.h>
  27. #include <scsi/scsi.h>
  28. #include "scsi_priv.h"
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_spi.h>
  35. #define SPI_PRINTK(x, l, f, a...) dev_printk(l, &(x)->dev, f , ##a)
  36. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  37. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  38. * on" attributes */
  39. #define SPI_HOST_ATTRS 1
  40. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  41. #define DV_LOOPS 3
  42. #define DV_TIMEOUT (10*HZ)
  43. #define DV_RETRIES 3 /* should only need at most
  44. * two cc/ua clears */
  45. /* Private data accessors (keep these out of the header file) */
  46. #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
  47. #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem)
  48. struct spi_internal {
  49. struct scsi_transport_template t;
  50. struct spi_function_template *f;
  51. /* The actual attributes */
  52. struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
  53. /* The array of null terminated pointers to attributes
  54. * needed by scsi_sysfs.c */
  55. struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
  56. struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
  57. struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
  58. };
  59. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  60. static const int ppr_to_ps[] = {
  61. /* The PPR values 0-6 are reserved, fill them in when
  62. * the committee defines them */
  63. -1, /* 0x00 */
  64. -1, /* 0x01 */
  65. -1, /* 0x02 */
  66. -1, /* 0x03 */
  67. -1, /* 0x04 */
  68. -1, /* 0x05 */
  69. -1, /* 0x06 */
  70. 3125, /* 0x07 */
  71. 6250, /* 0x08 */
  72. 12500, /* 0x09 */
  73. 25000, /* 0x0a */
  74. 30300, /* 0x0b */
  75. 50000, /* 0x0c */
  76. };
  77. /* The PPR values at which you calculate the period in ns by multiplying
  78. * by 4 */
  79. #define SPI_STATIC_PPR 0x0c
  80. static int sprint_frac(char *dest, int value, int denom)
  81. {
  82. int frac = value % denom;
  83. int result = sprintf(dest, "%d", value / denom);
  84. if (frac == 0)
  85. return result;
  86. dest[result++] = '.';
  87. do {
  88. denom /= 10;
  89. sprintf(dest + result, "%d", frac / denom);
  90. result++;
  91. frac %= denom;
  92. } while (frac);
  93. dest[result++] = '\0';
  94. return result;
  95. }
  96. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  97. enum dma_data_direction dir,
  98. void *buffer, unsigned bufflen,
  99. struct scsi_sense_hdr *sshdr)
  100. {
  101. int i, result;
  102. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  103. for(i = 0; i < DV_RETRIES; i++) {
  104. result = scsi_execute(sdev, cmd, dir, buffer, bufflen,
  105. sense, DV_TIMEOUT, /* retries */ 1,
  106. REQ_FAILFAST);
  107. if (result & DRIVER_SENSE) {
  108. struct scsi_sense_hdr sshdr_tmp;
  109. if (!sshdr)
  110. sshdr = &sshdr_tmp;
  111. if (scsi_normalize_sense(sense, sizeof(*sense),
  112. sshdr)
  113. && sshdr->sense_key == UNIT_ATTENTION)
  114. continue;
  115. }
  116. break;
  117. }
  118. return result;
  119. }
  120. static struct {
  121. enum spi_signal_type value;
  122. char *name;
  123. } signal_types[] = {
  124. { SPI_SIGNAL_UNKNOWN, "unknown" },
  125. { SPI_SIGNAL_SE, "SE" },
  126. { SPI_SIGNAL_LVD, "LVD" },
  127. { SPI_SIGNAL_HVD, "HVD" },
  128. };
  129. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  130. {
  131. int i;
  132. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  133. if (type == signal_types[i].value)
  134. return signal_types[i].name;
  135. }
  136. return NULL;
  137. }
  138. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  139. {
  140. int i, len;
  141. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  142. len = strlen(signal_types[i].name);
  143. if (strncmp(name, signal_types[i].name, len) == 0 &&
  144. (name[len] == '\n' || name[len] == '\0'))
  145. return signal_types[i].value;
  146. }
  147. return SPI_SIGNAL_UNKNOWN;
  148. }
  149. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  150. struct class_device *cdev)
  151. {
  152. struct Scsi_Host *shost = dev_to_shost(dev);
  153. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  154. return 0;
  155. }
  156. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  157. "spi_host",
  158. spi_host_setup,
  159. NULL,
  160. NULL);
  161. static int spi_host_match(struct attribute_container *cont,
  162. struct device *dev)
  163. {
  164. struct Scsi_Host *shost;
  165. struct spi_internal *i;
  166. if (!scsi_is_host_device(dev))
  167. return 0;
  168. shost = dev_to_shost(dev);
  169. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  170. != &spi_host_class.class)
  171. return 0;
  172. i = to_spi_internal(shost->transportt);
  173. return &i->t.host_attrs.ac == cont;
  174. }
  175. static int spi_device_configure(struct transport_container *tc,
  176. struct device *dev,
  177. struct class_device *cdev)
  178. {
  179. struct scsi_device *sdev = to_scsi_device(dev);
  180. struct scsi_target *starget = sdev->sdev_target;
  181. /* Populate the target capability fields with the values
  182. * gleaned from the device inquiry */
  183. spi_support_sync(starget) = scsi_device_sync(sdev);
  184. spi_support_wide(starget) = scsi_device_wide(sdev);
  185. spi_support_dt(starget) = scsi_device_dt(sdev);
  186. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  187. spi_support_ius(starget) = scsi_device_ius(sdev);
  188. spi_support_qas(starget) = scsi_device_qas(sdev);
  189. return 0;
  190. }
  191. static int spi_setup_transport_attrs(struct transport_container *tc,
  192. struct device *dev,
  193. struct class_device *cdev)
  194. {
  195. struct scsi_target *starget = to_scsi_target(dev);
  196. spi_period(starget) = -1; /* illegal value */
  197. spi_min_period(starget) = 0;
  198. spi_offset(starget) = 0; /* async */
  199. spi_max_offset(starget) = 255;
  200. spi_width(starget) = 0; /* narrow */
  201. spi_max_width(starget) = 1;
  202. spi_iu(starget) = 0; /* no IU */
  203. spi_dt(starget) = 0; /* ST */
  204. spi_qas(starget) = 0;
  205. spi_wr_flow(starget) = 0;
  206. spi_rd_strm(starget) = 0;
  207. spi_rti(starget) = 0;
  208. spi_pcomp_en(starget) = 0;
  209. spi_hold_mcs(starget) = 0;
  210. spi_dv_pending(starget) = 0;
  211. spi_initial_dv(starget) = 0;
  212. init_MUTEX(&spi_dv_sem(starget));
  213. return 0;
  214. }
  215. #define spi_transport_show_simple(field, format_string) \
  216. \
  217. static ssize_t \
  218. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  219. { \
  220. struct scsi_target *starget = transport_class_to_starget(cdev); \
  221. struct spi_transport_attrs *tp; \
  222. \
  223. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  224. return snprintf(buf, 20, format_string, tp->field); \
  225. }
  226. #define spi_transport_store_simple(field, format_string) \
  227. \
  228. static ssize_t \
  229. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  230. size_t count) \
  231. { \
  232. int val; \
  233. struct scsi_target *starget = transport_class_to_starget(cdev); \
  234. struct spi_transport_attrs *tp; \
  235. \
  236. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  237. val = simple_strtoul(buf, NULL, 0); \
  238. tp->field = val; \
  239. return count; \
  240. }
  241. #define spi_transport_show_function(field, format_string) \
  242. \
  243. static ssize_t \
  244. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  245. { \
  246. struct scsi_target *starget = transport_class_to_starget(cdev); \
  247. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  248. struct spi_transport_attrs *tp; \
  249. struct spi_internal *i = to_spi_internal(shost->transportt); \
  250. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  251. if (i->f->get_##field) \
  252. i->f->get_##field(starget); \
  253. return snprintf(buf, 20, format_string, tp->field); \
  254. }
  255. #define spi_transport_store_function(field, format_string) \
  256. static ssize_t \
  257. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  258. size_t count) \
  259. { \
  260. int val; \
  261. struct scsi_target *starget = transport_class_to_starget(cdev); \
  262. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  263. struct spi_internal *i = to_spi_internal(shost->transportt); \
  264. \
  265. val = simple_strtoul(buf, NULL, 0); \
  266. i->f->set_##field(starget, val); \
  267. return count; \
  268. }
  269. #define spi_transport_store_max(field, format_string) \
  270. static ssize_t \
  271. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  272. size_t count) \
  273. { \
  274. int val; \
  275. struct scsi_target *starget = transport_class_to_starget(cdev); \
  276. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  277. struct spi_internal *i = to_spi_internal(shost->transportt); \
  278. struct spi_transport_attrs *tp \
  279. = (struct spi_transport_attrs *)&starget->starget_data; \
  280. \
  281. val = simple_strtoul(buf, NULL, 0); \
  282. if (val > tp->max_##field) \
  283. val = tp->max_##field; \
  284. i->f->set_##field(starget, val); \
  285. return count; \
  286. }
  287. #define spi_transport_rd_attr(field, format_string) \
  288. spi_transport_show_function(field, format_string) \
  289. spi_transport_store_function(field, format_string) \
  290. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  291. show_spi_transport_##field, \
  292. store_spi_transport_##field);
  293. #define spi_transport_simple_attr(field, format_string) \
  294. spi_transport_show_simple(field, format_string) \
  295. spi_transport_store_simple(field, format_string) \
  296. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  297. show_spi_transport_##field, \
  298. store_spi_transport_##field);
  299. #define spi_transport_max_attr(field, format_string) \
  300. spi_transport_show_function(field, format_string) \
  301. spi_transport_store_max(field, format_string) \
  302. spi_transport_simple_attr(max_##field, format_string) \
  303. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  304. show_spi_transport_##field, \
  305. store_spi_transport_##field);
  306. /* The Parallel SCSI Tranport Attributes: */
  307. spi_transport_max_attr(offset, "%d\n");
  308. spi_transport_max_attr(width, "%d\n");
  309. spi_transport_rd_attr(iu, "%d\n");
  310. spi_transport_rd_attr(dt, "%d\n");
  311. spi_transport_rd_attr(qas, "%d\n");
  312. spi_transport_rd_attr(wr_flow, "%d\n");
  313. spi_transport_rd_attr(rd_strm, "%d\n");
  314. spi_transport_rd_attr(rti, "%d\n");
  315. spi_transport_rd_attr(pcomp_en, "%d\n");
  316. spi_transport_rd_attr(hold_mcs, "%d\n");
  317. /* we only care about the first child device so we return 1 */
  318. static int child_iter(struct device *dev, void *data)
  319. {
  320. struct scsi_device *sdev = to_scsi_device(dev);
  321. spi_dv_device(sdev);
  322. return 1;
  323. }
  324. static ssize_t
  325. store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
  326. {
  327. struct scsi_target *starget = transport_class_to_starget(cdev);
  328. device_for_each_child(&starget->dev, NULL, child_iter);
  329. return count;
  330. }
  331. static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  332. /* Translate the period into ns according to the current spec
  333. * for SDTR/PPR messages */
  334. static ssize_t
  335. show_spi_transport_period_helper(struct class_device *cdev, char *buf,
  336. int period)
  337. {
  338. int len, picosec;
  339. if (period < 0 || period > 0xff) {
  340. picosec = -1;
  341. } else if (period <= SPI_STATIC_PPR) {
  342. picosec = ppr_to_ps[period];
  343. } else {
  344. picosec = period * 4000;
  345. }
  346. if (picosec == -1) {
  347. len = sprintf(buf, "reserved");
  348. } else {
  349. len = sprint_frac(buf, picosec, 1000);
  350. }
  351. buf[len++] = '\n';
  352. buf[len] = '\0';
  353. return len;
  354. }
  355. static ssize_t
  356. store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
  357. size_t count, int *periodp)
  358. {
  359. int j, picosec, period = -1;
  360. char *endp;
  361. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  362. if (*endp == '.') {
  363. int mult = 100;
  364. do {
  365. endp++;
  366. if (!isdigit(*endp))
  367. break;
  368. picosec += (*endp - '0') * mult;
  369. mult /= 10;
  370. } while (mult > 0);
  371. }
  372. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  373. if (ppr_to_ps[j] < picosec)
  374. continue;
  375. period = j;
  376. break;
  377. }
  378. if (period == -1)
  379. period = picosec / 4000;
  380. if (period > 0xff)
  381. period = 0xff;
  382. *periodp = period;
  383. return count;
  384. }
  385. static ssize_t
  386. show_spi_transport_period(struct class_device *cdev, char *buf)
  387. {
  388. struct scsi_target *starget = transport_class_to_starget(cdev);
  389. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  390. struct spi_internal *i = to_spi_internal(shost->transportt);
  391. struct spi_transport_attrs *tp =
  392. (struct spi_transport_attrs *)&starget->starget_data;
  393. if (i->f->get_period)
  394. i->f->get_period(starget);
  395. return show_spi_transport_period_helper(cdev, buf, tp->period);
  396. }
  397. static ssize_t
  398. store_spi_transport_period(struct class_device *cdev, const char *buf,
  399. size_t count)
  400. {
  401. struct scsi_target *starget = transport_class_to_starget(cdev);
  402. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  403. struct spi_internal *i = to_spi_internal(shost->transportt);
  404. struct spi_transport_attrs *tp =
  405. (struct spi_transport_attrs *)&starget->starget_data;
  406. int period, retval;
  407. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  408. if (period < tp->min_period)
  409. period = tp->min_period;
  410. i->f->set_period(starget, period);
  411. return retval;
  412. }
  413. static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
  414. show_spi_transport_period,
  415. store_spi_transport_period);
  416. static ssize_t
  417. show_spi_transport_min_period(struct class_device *cdev, char *buf)
  418. {
  419. struct scsi_target *starget = transport_class_to_starget(cdev);
  420. struct spi_transport_attrs *tp =
  421. (struct spi_transport_attrs *)&starget->starget_data;
  422. return show_spi_transport_period_helper(cdev, buf, tp->min_period);
  423. }
  424. static ssize_t
  425. store_spi_transport_min_period(struct class_device *cdev, const char *buf,
  426. size_t count)
  427. {
  428. struct scsi_target *starget = transport_class_to_starget(cdev);
  429. struct spi_transport_attrs *tp =
  430. (struct spi_transport_attrs *)&starget->starget_data;
  431. return store_spi_transport_period_helper(cdev, buf, count,
  432. &tp->min_period);
  433. }
  434. static CLASS_DEVICE_ATTR(min_period, S_IRUGO | S_IWUSR,
  435. show_spi_transport_min_period,
  436. store_spi_transport_min_period);
  437. static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
  438. {
  439. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  440. struct spi_internal *i = to_spi_internal(shost->transportt);
  441. if (i->f->get_signalling)
  442. i->f->get_signalling(shost);
  443. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  444. }
  445. static ssize_t store_spi_host_signalling(struct class_device *cdev,
  446. const char *buf, size_t count)
  447. {
  448. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  449. struct spi_internal *i = to_spi_internal(shost->transportt);
  450. enum spi_signal_type type = spi_signal_to_value(buf);
  451. if (type != SPI_SIGNAL_UNKNOWN)
  452. i->f->set_signalling(shost, type);
  453. return count;
  454. }
  455. static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
  456. show_spi_host_signalling,
  457. store_spi_host_signalling);
  458. #define DV_SET(x, y) \
  459. if(i->f->set_##x) \
  460. i->f->set_##x(sdev->sdev_target, y)
  461. enum spi_compare_returns {
  462. SPI_COMPARE_SUCCESS,
  463. SPI_COMPARE_FAILURE,
  464. SPI_COMPARE_SKIP_TEST,
  465. };
  466. /* This is for read/write Domain Validation: If the device supports
  467. * an echo buffer, we do read/write tests to it */
  468. static enum spi_compare_returns
  469. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  470. u8 *ptr, const int retries)
  471. {
  472. int len = ptr - buffer;
  473. int j, k, r, result;
  474. unsigned int pattern = 0x0000ffff;
  475. struct scsi_sense_hdr sshdr;
  476. const char spi_write_buffer[] = {
  477. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  478. };
  479. const char spi_read_buffer[] = {
  480. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  481. };
  482. /* set up the pattern buffer. Doesn't matter if we spill
  483. * slightly beyond since that's where the read buffer is */
  484. for (j = 0; j < len; ) {
  485. /* fill the buffer with counting (test a) */
  486. for ( ; j < min(len, 32); j++)
  487. buffer[j] = j;
  488. k = j;
  489. /* fill the buffer with alternating words of 0x0 and
  490. * 0xffff (test b) */
  491. for ( ; j < min(len, k + 32); j += 2) {
  492. u16 *word = (u16 *)&buffer[j];
  493. *word = (j & 0x02) ? 0x0000 : 0xffff;
  494. }
  495. k = j;
  496. /* fill with crosstalk (alternating 0x5555 0xaaa)
  497. * (test c) */
  498. for ( ; j < min(len, k + 32); j += 2) {
  499. u16 *word = (u16 *)&buffer[j];
  500. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  501. }
  502. k = j;
  503. /* fill with shifting bits (test d) */
  504. for ( ; j < min(len, k + 32); j += 4) {
  505. u32 *word = (unsigned int *)&buffer[j];
  506. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  507. *word = pattern;
  508. pattern = (pattern << 1) | roll;
  509. }
  510. /* don't bother with random data (test e) */
  511. }
  512. for (r = 0; r < retries; r++) {
  513. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  514. buffer, len, &sshdr);
  515. if(result || !scsi_device_online(sdev)) {
  516. scsi_device_set_state(sdev, SDEV_QUIESCE);
  517. if (scsi_sense_valid(&sshdr)
  518. && sshdr.sense_key == ILLEGAL_REQUEST
  519. /* INVALID FIELD IN CDB */
  520. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  521. /* This would mean that the drive lied
  522. * to us about supporting an echo
  523. * buffer (unfortunately some Western
  524. * Digital drives do precisely this)
  525. */
  526. return SPI_COMPARE_SKIP_TEST;
  527. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", result);
  528. return SPI_COMPARE_FAILURE;
  529. }
  530. memset(ptr, 0, len);
  531. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  532. ptr, len, NULL);
  533. scsi_device_set_state(sdev, SDEV_QUIESCE);
  534. if (memcmp(buffer, ptr, len) != 0)
  535. return SPI_COMPARE_FAILURE;
  536. }
  537. return SPI_COMPARE_SUCCESS;
  538. }
  539. /* This is for the simplest form of Domain Validation: a read test
  540. * on the inquiry data from the device */
  541. static enum spi_compare_returns
  542. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  543. u8 *ptr, const int retries)
  544. {
  545. int r, result;
  546. const int len = sdev->inquiry_len;
  547. const char spi_inquiry[] = {
  548. INQUIRY, 0, 0, 0, len, 0
  549. };
  550. for (r = 0; r < retries; r++) {
  551. memset(ptr, 0, len);
  552. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  553. ptr, len, NULL);
  554. if(result || !scsi_device_online(sdev)) {
  555. scsi_device_set_state(sdev, SDEV_QUIESCE);
  556. return SPI_COMPARE_FAILURE;
  557. }
  558. /* If we don't have the inquiry data already, the
  559. * first read gets it */
  560. if (ptr == buffer) {
  561. ptr += len;
  562. --r;
  563. continue;
  564. }
  565. if (memcmp(buffer, ptr, len) != 0)
  566. /* failure */
  567. return SPI_COMPARE_FAILURE;
  568. }
  569. return SPI_COMPARE_SUCCESS;
  570. }
  571. static enum spi_compare_returns
  572. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  573. enum spi_compare_returns
  574. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  575. {
  576. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  577. struct scsi_target *starget = sdev->sdev_target;
  578. int period = 0, prevperiod = 0;
  579. enum spi_compare_returns retval;
  580. for (;;) {
  581. int newperiod;
  582. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  583. if (retval == SPI_COMPARE_SUCCESS
  584. || retval == SPI_COMPARE_SKIP_TEST)
  585. break;
  586. /* OK, retrain, fallback */
  587. if (i->f->get_iu)
  588. i->f->get_iu(starget);
  589. if (i->f->get_qas)
  590. i->f->get_qas(starget);
  591. if (i->f->get_period)
  592. i->f->get_period(sdev->sdev_target);
  593. /* Here's the fallback sequence; first try turning off
  594. * IU, then QAS (if we can control them), then finally
  595. * fall down the periods */
  596. if (i->f->set_iu && spi_iu(starget)) {
  597. SPI_PRINTK(starget, KERN_ERR, "Domain Validation Disabing Information Units\n");
  598. DV_SET(iu, 0);
  599. } else if (i->f->set_qas && spi_qas(starget)) {
  600. SPI_PRINTK(starget, KERN_ERR, "Domain Validation Disabing Quick Arbitration and Selection\n");
  601. DV_SET(qas, 0);
  602. } else {
  603. newperiod = spi_period(starget);
  604. period = newperiod > period ? newperiod : period;
  605. if (period < 0x0d)
  606. period++;
  607. else
  608. period += period >> 1;
  609. if (unlikely(period > 0xff || period == prevperiod)) {
  610. /* Total failure; set to async and return */
  611. SPI_PRINTK(starget, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
  612. DV_SET(offset, 0);
  613. return SPI_COMPARE_FAILURE;
  614. }
  615. SPI_PRINTK(starget, KERN_ERR, "Domain Validation detected failure, dropping back\n");
  616. DV_SET(period, period);
  617. prevperiod = period;
  618. }
  619. }
  620. return retval;
  621. }
  622. static int
  623. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  624. {
  625. int l, result;
  626. /* first off do a test unit ready. This can error out
  627. * because of reservations or some other reason. If it
  628. * fails, the device won't let us write to the echo buffer
  629. * so just return failure */
  630. const char spi_test_unit_ready[] = {
  631. TEST_UNIT_READY, 0, 0, 0, 0, 0
  632. };
  633. const char spi_read_buffer_descriptor[] = {
  634. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  635. };
  636. /* We send a set of three TURs to clear any outstanding
  637. * unit attention conditions if they exist (Otherwise the
  638. * buffer tests won't be happy). If the TUR still fails
  639. * (reservation conflict, device not ready, etc) just
  640. * skip the write tests */
  641. for (l = 0; ; l++) {
  642. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  643. NULL, 0, NULL);
  644. if(result) {
  645. if(l >= 3)
  646. return 0;
  647. } else {
  648. /* TUR succeeded */
  649. break;
  650. }
  651. }
  652. result = spi_execute(sdev, spi_read_buffer_descriptor,
  653. DMA_FROM_DEVICE, buffer, 4, NULL);
  654. if (result)
  655. /* Device has no echo buffer */
  656. return 0;
  657. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  658. }
  659. static void
  660. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  661. {
  662. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  663. struct scsi_target *starget = sdev->sdev_target;
  664. int len = sdev->inquiry_len;
  665. /* first set us up for narrow async */
  666. DV_SET(offset, 0);
  667. DV_SET(width, 0);
  668. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  669. != SPI_COMPARE_SUCCESS) {
  670. SPI_PRINTK(starget, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
  671. /* FIXME: should probably offline the device here? */
  672. return;
  673. }
  674. /* test width */
  675. if (i->f->set_width && spi_max_width(starget) &&
  676. scsi_device_wide(sdev)) {
  677. i->f->set_width(starget, 1);
  678. if (spi_dv_device_compare_inquiry(sdev, buffer,
  679. buffer + len,
  680. DV_LOOPS)
  681. != SPI_COMPARE_SUCCESS) {
  682. SPI_PRINTK(starget, KERN_ERR, "Wide Transfers Fail\n");
  683. i->f->set_width(starget, 0);
  684. }
  685. }
  686. if (!i->f->set_period)
  687. return;
  688. /* device can't handle synchronous */
  689. if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
  690. return;
  691. /* see if the device has an echo buffer. If it does we can
  692. * do the SPI pattern write tests */
  693. len = 0;
  694. if (scsi_device_dt(sdev))
  695. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  696. retry:
  697. /* now set up to the maximum */
  698. DV_SET(offset, spi_max_offset(starget));
  699. DV_SET(period, spi_min_period(starget));
  700. /* try QAS requests; this should be harmless to set if the
  701. * target supports it */
  702. if (scsi_device_qas(sdev))
  703. DV_SET(qas, 1);
  704. /* Also try IU transfers */
  705. if (scsi_device_ius(sdev))
  706. DV_SET(iu, 1);
  707. if (spi_min_period(starget) < 9) {
  708. /* This u320 (or u640). Ignore the coupled parameters
  709. * like DT and IU, but set the optional ones */
  710. DV_SET(rd_strm, 1);
  711. DV_SET(wr_flow, 1);
  712. DV_SET(rti, 1);
  713. if (spi_min_period(starget) == 8)
  714. DV_SET(pcomp_en, 1);
  715. }
  716. if (len == 0) {
  717. SPI_PRINTK(starget, KERN_INFO, "Domain Validation skipping write tests\n");
  718. spi_dv_retrain(sdev, buffer, buffer + len,
  719. spi_dv_device_compare_inquiry);
  720. return;
  721. }
  722. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  723. SPI_PRINTK(starget, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  724. len = SPI_MAX_ECHO_BUFFER_SIZE;
  725. }
  726. if (spi_dv_retrain(sdev, buffer, buffer + len,
  727. spi_dv_device_echo_buffer)
  728. == SPI_COMPARE_SKIP_TEST) {
  729. /* OK, the stupid drive can't do a write echo buffer
  730. * test after all, fall back to the read tests */
  731. len = 0;
  732. goto retry;
  733. }
  734. }
  735. /** spi_dv_device - Do Domain Validation on the device
  736. * @sdev: scsi device to validate
  737. *
  738. * Performs the domain validation on the given device in the
  739. * current execution thread. Since DV operations may sleep,
  740. * the current thread must have user context. Also no SCSI
  741. * related locks that would deadlock I/O issued by the DV may
  742. * be held.
  743. */
  744. void
  745. spi_dv_device(struct scsi_device *sdev)
  746. {
  747. struct scsi_target *starget = sdev->sdev_target;
  748. u8 *buffer;
  749. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  750. if (unlikely(scsi_device_get(sdev)))
  751. return;
  752. buffer = kmalloc(len, GFP_KERNEL);
  753. if (unlikely(!buffer))
  754. goto out_put;
  755. memset(buffer, 0, len);
  756. /* We need to verify that the actual device will quiesce; the
  757. * later target quiesce is just a nice to have */
  758. if (unlikely(scsi_device_quiesce(sdev)))
  759. goto out_free;
  760. scsi_target_quiesce(starget);
  761. spi_dv_pending(starget) = 1;
  762. down(&spi_dv_sem(starget));
  763. SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n");
  764. spi_dv_device_internal(sdev, buffer);
  765. SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n");
  766. up(&spi_dv_sem(starget));
  767. spi_dv_pending(starget) = 0;
  768. scsi_target_resume(starget);
  769. spi_initial_dv(starget) = 1;
  770. out_free:
  771. kfree(buffer);
  772. out_put:
  773. scsi_device_put(sdev);
  774. }
  775. EXPORT_SYMBOL(spi_dv_device);
  776. struct work_queue_wrapper {
  777. struct work_struct work;
  778. struct scsi_device *sdev;
  779. };
  780. static void
  781. spi_dv_device_work_wrapper(void *data)
  782. {
  783. struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
  784. struct scsi_device *sdev = wqw->sdev;
  785. kfree(wqw);
  786. spi_dv_device(sdev);
  787. spi_dv_pending(sdev->sdev_target) = 0;
  788. scsi_device_put(sdev);
  789. }
  790. /**
  791. * spi_schedule_dv_device - schedule domain validation to occur on the device
  792. * @sdev: The device to validate
  793. *
  794. * Identical to spi_dv_device() above, except that the DV will be
  795. * scheduled to occur in a workqueue later. All memory allocations
  796. * are atomic, so may be called from any context including those holding
  797. * SCSI locks.
  798. */
  799. void
  800. spi_schedule_dv_device(struct scsi_device *sdev)
  801. {
  802. struct work_queue_wrapper *wqw =
  803. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  804. if (unlikely(!wqw))
  805. return;
  806. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  807. kfree(wqw);
  808. return;
  809. }
  810. /* Set pending early (dv_device doesn't check it, only sets it) */
  811. spi_dv_pending(sdev->sdev_target) = 1;
  812. if (unlikely(scsi_device_get(sdev))) {
  813. kfree(wqw);
  814. spi_dv_pending(sdev->sdev_target) = 0;
  815. return;
  816. }
  817. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
  818. wqw->sdev = sdev;
  819. schedule_work(&wqw->work);
  820. }
  821. EXPORT_SYMBOL(spi_schedule_dv_device);
  822. /**
  823. * spi_display_xfer_agreement - Print the current target transfer agreement
  824. * @starget: The target for which to display the agreement
  825. *
  826. * Each SPI port is required to maintain a transfer agreement for each
  827. * other port on the bus. This function prints a one-line summary of
  828. * the current agreement; more detailed information is available in sysfs.
  829. */
  830. void spi_display_xfer_agreement(struct scsi_target *starget)
  831. {
  832. struct spi_transport_attrs *tp;
  833. tp = (struct spi_transport_attrs *)&starget->starget_data;
  834. if (tp->offset > 0 && tp->period > 0) {
  835. unsigned int picosec, kb100;
  836. char *scsi = "FAST-?";
  837. char tmp[8];
  838. if (tp->period <= SPI_STATIC_PPR) {
  839. picosec = ppr_to_ps[tp->period];
  840. switch (tp->period) {
  841. case 7: scsi = "FAST-320"; break;
  842. case 8: scsi = "FAST-160"; break;
  843. case 9: scsi = "FAST-80"; break;
  844. case 10:
  845. case 11: scsi = "FAST-40"; break;
  846. case 12: scsi = "FAST-20"; break;
  847. }
  848. } else {
  849. picosec = tp->period * 4000;
  850. if (tp->period < 25)
  851. scsi = "FAST-20";
  852. else if (tp->period < 50)
  853. scsi = "FAST-10";
  854. else
  855. scsi = "FAST-5";
  856. }
  857. kb100 = (10000000 + picosec / 2) / picosec;
  858. if (tp->width)
  859. kb100 *= 2;
  860. sprint_frac(tmp, picosec, 1000);
  861. dev_info(&starget->dev,
  862. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  863. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  864. tp->dt ? "DT" : "ST",
  865. tp->iu ? " IU" : "",
  866. tp->qas ? " QAS" : "",
  867. tp->rd_strm ? " RDSTRM" : "",
  868. tp->rti ? " RTI" : "",
  869. tp->wr_flow ? " WRFLOW" : "",
  870. tp->pcomp_en ? " PCOMP" : "",
  871. tp->hold_mcs ? " HMCS" : "",
  872. tmp, tp->offset);
  873. } else {
  874. dev_info(&starget->dev, "%sasynchronous.\n",
  875. tp->width ? "wide " : "");
  876. }
  877. }
  878. EXPORT_SYMBOL(spi_display_xfer_agreement);
  879. #define SETUP_ATTRIBUTE(field) \
  880. i->private_attrs[count] = class_device_attr_##field; \
  881. if (!i->f->set_##field) { \
  882. i->private_attrs[count].attr.mode = S_IRUGO; \
  883. i->private_attrs[count].store = NULL; \
  884. } \
  885. i->attrs[count] = &i->private_attrs[count]; \
  886. if (i->f->show_##field) \
  887. count++
  888. #define SETUP_RELATED_ATTRIBUTE(field, rel_field) \
  889. i->private_attrs[count] = class_device_attr_##field; \
  890. if (!i->f->set_##rel_field) { \
  891. i->private_attrs[count].attr.mode = S_IRUGO; \
  892. i->private_attrs[count].store = NULL; \
  893. } \
  894. i->attrs[count] = &i->private_attrs[count]; \
  895. if (i->f->show_##rel_field) \
  896. count++
  897. #define SETUP_HOST_ATTRIBUTE(field) \
  898. i->private_host_attrs[count] = class_device_attr_##field; \
  899. if (!i->f->set_##field) { \
  900. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  901. i->private_host_attrs[count].store = NULL; \
  902. } \
  903. i->host_attrs[count] = &i->private_host_attrs[count]; \
  904. count++
  905. static int spi_device_match(struct attribute_container *cont,
  906. struct device *dev)
  907. {
  908. struct scsi_device *sdev;
  909. struct Scsi_Host *shost;
  910. struct spi_internal *i;
  911. if (!scsi_is_sdev_device(dev))
  912. return 0;
  913. sdev = to_scsi_device(dev);
  914. shost = sdev->host;
  915. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  916. != &spi_host_class.class)
  917. return 0;
  918. /* Note: this class has no device attributes, so it has
  919. * no per-HBA allocation and thus we don't need to distinguish
  920. * the attribute containers for the device */
  921. i = to_spi_internal(shost->transportt);
  922. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  923. return 0;
  924. return 1;
  925. }
  926. static int spi_target_match(struct attribute_container *cont,
  927. struct device *dev)
  928. {
  929. struct Scsi_Host *shost;
  930. struct scsi_target *starget;
  931. struct spi_internal *i;
  932. if (!scsi_is_target_device(dev))
  933. return 0;
  934. shost = dev_to_shost(dev->parent);
  935. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  936. != &spi_host_class.class)
  937. return 0;
  938. i = to_spi_internal(shost->transportt);
  939. starget = to_scsi_target(dev);
  940. if (i->f->deny_binding && i->f->deny_binding(starget))
  941. return 0;
  942. return &i->t.target_attrs.ac == cont;
  943. }
  944. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  945. "spi_transport",
  946. spi_setup_transport_attrs,
  947. NULL,
  948. NULL);
  949. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  950. spi_device_match,
  951. spi_device_configure);
  952. struct scsi_transport_template *
  953. spi_attach_transport(struct spi_function_template *ft)
  954. {
  955. struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
  956. GFP_KERNEL);
  957. int count = 0;
  958. if (unlikely(!i))
  959. return NULL;
  960. memset(i, 0, sizeof(struct spi_internal));
  961. i->t.target_attrs.ac.class = &spi_transport_class.class;
  962. i->t.target_attrs.ac.attrs = &i->attrs[0];
  963. i->t.target_attrs.ac.match = spi_target_match;
  964. transport_container_register(&i->t.target_attrs);
  965. i->t.target_size = sizeof(struct spi_transport_attrs);
  966. i->t.host_attrs.ac.class = &spi_host_class.class;
  967. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  968. i->t.host_attrs.ac.match = spi_host_match;
  969. transport_container_register(&i->t.host_attrs);
  970. i->t.host_size = sizeof(struct spi_host_attrs);
  971. i->f = ft;
  972. SETUP_ATTRIBUTE(period);
  973. SETUP_RELATED_ATTRIBUTE(min_period, period);
  974. SETUP_ATTRIBUTE(offset);
  975. SETUP_RELATED_ATTRIBUTE(max_offset, offset);
  976. SETUP_ATTRIBUTE(width);
  977. SETUP_RELATED_ATTRIBUTE(max_width, width);
  978. SETUP_ATTRIBUTE(iu);
  979. SETUP_ATTRIBUTE(dt);
  980. SETUP_ATTRIBUTE(qas);
  981. SETUP_ATTRIBUTE(wr_flow);
  982. SETUP_ATTRIBUTE(rd_strm);
  983. SETUP_ATTRIBUTE(rti);
  984. SETUP_ATTRIBUTE(pcomp_en);
  985. SETUP_ATTRIBUTE(hold_mcs);
  986. /* if you add an attribute but forget to increase SPI_NUM_ATTRS
  987. * this bug will trigger */
  988. BUG_ON(count > SPI_NUM_ATTRS);
  989. i->attrs[count++] = &class_device_attr_revalidate;
  990. i->attrs[count] = NULL;
  991. count = 0;
  992. SETUP_HOST_ATTRIBUTE(signalling);
  993. BUG_ON(count > SPI_HOST_ATTRS);
  994. i->host_attrs[count] = NULL;
  995. return &i->t;
  996. }
  997. EXPORT_SYMBOL(spi_attach_transport);
  998. void spi_release_transport(struct scsi_transport_template *t)
  999. {
  1000. struct spi_internal *i = to_spi_internal(t);
  1001. transport_container_unregister(&i->t.target_attrs);
  1002. transport_container_unregister(&i->t.host_attrs);
  1003. kfree(i);
  1004. }
  1005. EXPORT_SYMBOL(spi_release_transport);
  1006. static __init int spi_transport_init(void)
  1007. {
  1008. int error = transport_class_register(&spi_transport_class);
  1009. if (error)
  1010. return error;
  1011. error = anon_transport_class_register(&spi_device_class);
  1012. return transport_class_register(&spi_host_class);
  1013. }
  1014. static void __exit spi_transport_exit(void)
  1015. {
  1016. transport_class_unregister(&spi_transport_class);
  1017. anon_transport_class_unregister(&spi_device_class);
  1018. transport_class_unregister(&spi_host_class);
  1019. }
  1020. MODULE_AUTHOR("Martin Hicks");
  1021. MODULE_DESCRIPTION("SPI Transport Attributes");
  1022. MODULE_LICENSE("GPL");
  1023. module_init(spi_transport_init);
  1024. module_exit(spi_transport_exit);