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