scsi_transport_spi.c 43 KB

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