scsi_transport_spi.c 38 KB

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