scsi_transport_spi.c 42 KB

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